杭电1212 Big Number

来源:互联网 发布:淘宝入门教程 编辑:程序博客网 时间:2024/04/28 20:20
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 312 7152455856554521 3250
 

Sample Output
251521
 


//首先你应该了解

模运算的基本性质


运算规则

模运算与基本四则运算有些相似,但是除法例外。其规则如下:   

(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 11000int 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;}


0 0
原创粉丝点击