题目大意:大整数取模问题。
解题思路:将大数转化为字符串处理,然后问题可以等价于各个位数上的分别取模。详见code。
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=1212
code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1000+10;
char n[MAXN]; //大数一般都转化为字符串处理
int m;
int main(){
while(scanf("%s%d",n,&m)!=EOF){
int len=strlen(n);
int ans=0;
for(int i=0;i<len;i++) //每步取模:1234=((1*10+2)*10+3)*10+4
ans=(int)(((long long)ans*10+(n[i]-'0'))%m); //防止乘法溢出,转化为longlong
printf("%d\n",ans);
}
return 0;
}