HDU 1212 Big Number(数论)

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2407    Accepted Submission(s): 1639

Problem Description

As we know, Big Number is always troublesome. But it's really important in our ACM. And today, your task is to write a program to calculate A mod B.
To make the problem easier, I promise that B will be smaller than 100000.
Is it too hard? No, I work it out in 10 minutes, and my program contains less than 25 lines.

Input

The input contains several test cases. Each test case consists of two positive integers A and B. The length of A will not exceed 1000, and B will be smaller than 100000. Process to the end of file.

Output

For each test case, you have to ouput the result of A mod B.

Sample Input

2 3

12 7

152455856554521 3250

Sample Output

2

5

1521

Author

Ignatius.L

Source

杭电ACM省赛集训队选拔赛之热身赛

 

Recommend

Eddy

Statistic | Submit | Discuss | Note

解题报告:这是一道数论题!就是求AmodB,但是A是大数,想了好半天,也没有想出神马思路,就借鉴了一下网上“大牛”们的思路

下面就是我在网上找的简单方法:

用递推的思想,就可以总结出下面的公式;

举例:

12345 9

余数等于(12340%9+5%9)%9;

而12340 9

(12300%9+40%9)%9;

依次...

最后(10000%9+2000%9)%9;

而10000%9=(1%9*10000)%9

2000%9=(2%9*1000)%9

即(1*10+2)%9*1000%9;

即可得到:for(i=0;i<len;i++)

{

sum=sum*10+s[i]-'0';

sum=sum%9;

}

我的代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1010;
char str[N];
int main()
{
int ans,i,len,m;
while(scanf("%s%d",str,&m)!=EOF)
{
len=strlen(str);
ans=0;
for(i=0;i<len;i++)
{
ans=ans*10+str[i]-'0';
ans=ans%m;

}
printf("%d\n",ans);
}
return 0;
}



原文地址:https://www.cnblogs.com/lidaojian/p/2310446.html