http://poj.org/problem?id=1006
https://gist.github.com/yyecust/562016f2cdbe6f0649d23d13b8bd94ba
中国剩余定理模板题
#include <cstdio>
using namespace std;
int nextInt()
{
int x;
scanf("%d", &x);
return x;
}
int extend_gcd(int a, int b, int &x, int &y)
{
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
int r = extend_gcd(b, a % b, y, x);
y -= x * (a / b);
return r;
}
}
int CRT(int a[], int m[], int n)
{
int M = 1;
for (int i = 0; i < n; i++) M*=m[i];
int ans = 0;
for (int i = 0; i < n; i++) {
int x, y;
int tm = M / m[i];
extend_gcd(tm, m[i], x, y);
ans = ans + (tm * x * a[i]) % M;
}
return (ans + M) % M;
}
int solve(int p, int e, int i, int d)
{
int a[3] = {p, e, i};
int m[3] = {23, 28, 33};
int M = m[0] * m[1] * m[2];
int ans = CRT(a, m, 3) - d;
if (ans <= 0) ans += M;
return ans;
}
int main()
{
//freopen("in.txt", "r", stdin);
int t = 0;
for (;;) {
int p = nextInt(), e = nextInt(), i = nextInt(), d = nextInt();
if (p == -1) break;
printf("Case %d: the next triple peak occurs in %d days.\n", ++t, solve(p, e, i, d));
}
}