标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5930 Accepted Submission(s): 4146
/*例如10000对m求余: * 10000%m * ==(10%m*1000%m)%m * ==(10%m*(10%m*100%m)%m)%m * ==(10%m*(10%m*(10%m*10%m)%m)%m)%m * 用代码表示就是: * 假设10000是字符串长度是len */ /* * 如123对m求余 * 123%m * ==((12%m*10%m)%m+3%m)%m * ==(((10%m+2%m)%m*10%m)%m+3%m)%m * ==((((1%m*10%m)%m+2%m)%m*10%m)%m+3%m)%m */ gets(str); int ans=0; for(i=0;i<len;i++) { ans=ans*10+str[i]; ans=ans%m; }
详细的模运算请参考:http://blog.csdn.net/chocolate_22/article/details/6458029
#include<stdio.h> #include<string.h> #define MAX 1100 int main() { int n,m,j,i,s,t; char p[MAX]; while(scanf("%s",p)!=EOF) { scanf("%d",&n); int l=strlen(p); s=0; for(i=0;i<l;i++) { s=s*10+p[i]-‘0‘; s=s%n; } printf("%d\n",s); } return 0; }
如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4674392.html