高精度算法
(a * b) % c = ((a % c) * (b %c)) % c
(a + b)% c = ((a % c) + (b % c)) % c
10000位大的数字可以分开算:
比如:
m=123
123 = (1*10 + 2)*10 + 3
m%n = 123%n = (((1%n * 10%n +2%n)%n * 10%n) % n + 3%n)%n
以下是一些代码;
#include <iostream>
#include<string>
#include<iomanip>
using namespace std;
int main()
{
string m;
long n, d;
int i;
while (cin >> m >> n)
{
d = 0;
for (i = 0; i < m.size(); i++)
{
d = ((d * 10) % n + m[i] - '0') % n;
// d*10是不超过10*n的,m[i] - '0'是不超过10的,除非10*n或者n+9溢出,否则整个计算过程都不会溢出
}
cout << d << endl;
}
return 0;
}