HDU 1212 Big Number(同余定理)

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


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
 
/************************************************************************/

题意:简单来说,该题就是要我们计算A mod B的值,A是大数,而B则是一个小于100000的数。

解题思路:因为A是大数,直接取模肯定是不行了,但是同余定理中有一条可以派上用场

若a≡b(mod m), c≡d (mod m),则
ac≡bd (mod m).

所以我们不妨将A(形如a1a2a3…an)表示成(((a1*10+a2)*10+a3)*10+…)*10+an再取模即可

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 1005;
const int inf = 2147483647;
const int mod = 2009;
char s[N];
int main()
{
    int n,m,i;
    while(~scanf("%s%d",s,&m))
    {
        n=0;
        for(i=0;s[i]!='\0';i++)
           n=(n*10+s[i]-'0')%m;
        printf("%d\n",n);
    }
    return 0;
}
菜鸟成长记


你可能感兴趣的:(ACM,同余定理)