题目大意
有 个循环周期,周期天数分别为 。对于某一年,已知某年这 个周期的某一峰值分别是当年的第 天, 问从第 天开始到最近一个满足 个周期都达到峰值的日期还有多少天。
中国剩余定理入门题。
设答案为 ,有:
将每个式子写成 的形式。
#include <cstdio>
int m[5] = {0, 23, 28, 33}, a[5], n = 3;
void exgcd(int a, int b, int &x, int &y) {
if (b == 0) { x = 1, y = 0; return; }
exgcd(b, a % b, y, x); y -= a / b * x;
}
int crt() {
int t = 1, x, y, res = 0;
for (int i = 1; i <= n; ++i) t *= m[i];
for (int i = 1; i <= n; ++i) { //找到一个数是其他模数的倍数,且在模m[i]的意义下等于1
exgcd(t / m[i], m[i], x, y); //x * t / m[i] % m[i] = 1
res = (res + x * t / m[i] * a[i]) % t;
}
return (res + t) % t;
}
int main() {
int cas, d; scanf("%d", &cas);
while (~scanf("%d%d%d%d", &a[1], &a[2], &a[3], &d)) {
if (a[1] == -1) break;
a[1] %= 23, a[2] %= 28, a[3] %= 33;
int ans = crt() - d;
if (ans <= 0) ans += 21252;
printf("Case %d: the next triple peak occurs in %d days.\n", cas++, ans);
}
return 0;
}