杭电1212 Big Number

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 + b) % p = (a % p + b % p) % p (1)   //用到了这个

(a - b) % p = (a % p - b % p) % p (2)   

(a * b) % p = (a % p * b % p) % p (3)   

ab % p = ((a % p)b) % p (4)   

结合率:

 ((a+b) % p + c) % p = (a + (b+c) % p) % p (5)   

((a*b) % p * c)% p = (a * (b*c) % p) % p (6)  

 

交换率: (a + b) % p = (b+a) % p (7)   

(a * b) % p = (b * a) % p (8)   

分配率: ((a +b)% p * c) % p = ((a * c) % p + (b * c) % p) % p (9) 

  

重要定理

若a≡b (% p),则对于任意的c,都有(a + c) ≡ (b + c) (%p);(10)   

若a≡b (% p),则对于任意的c,都有(a * c) ≡ (b * c) (%p);(11)   

若a≡b (% p),c≡d (% p),则 (a + c) ≡ (b + d) (%p),(a - c) ≡ (b - d) (%p),   (a * c) ≡ (b * d) (%p),(a / c) ≡ (b / d) (%p); (12)   

若a≡b (% p),则对于任意的c,都有ac≡ bc (%p); (13)




#include<stdio.h>
#include<string.h>
#define max 11000
int main()
{
	int i,n,len;
	char str[max];
	while(~scanf("%s %d",str,&n))
	{
		len=strlen(str);
		int res=0;
		for(i=0;i<len;++i)
		{
			res=(res*10+(str[i]-'0')%n)%n;//其实可以类比除法运算
		}
		printf("%d\n",res);
	}
	return 0;
}


你可能感兴趣的:(杭电1212 Big Number)