題目鏈接:uva 756 - Biorhythms
題目大意:三個周期,23,28,33,輸入為分別為在新一年中(三個周期均從0開始),出現周期中峰值的一天,以及當前的日子,問說需要經過多少天,才能使得三個峰值的在同一天。
解題思路:裸的中國剩余定理。
#include
#include
typedef long long ll;
const int maxn = 5;
const ll m[maxn] = {23,28,33};
ll M, t[maxn], D;
bool init () {
bool flag = false;
for (int i = 0; i < 3; i++) {
scanf("%lld", &t[i]);
if (t[i] != -1)
flag = true;
}
scanf("%lld", &D);
if (D != -1)
flag = true;
return flag;
}
void gcd (ll a, ll b, ll& d, ll& x, ll& y) {
if (b == 0) {
d = a;
x = 1;
y = 0;
} else {
gcd(b, a%b, d, y, x);
y -= (a/b) * x;
}
}
ll china (ll* a, int n) {
M = 1;
for (int i = 0; i < n; i++)
M *= m[i];
ll ans = 0;
for (int i = 0; i < n; i++) {
ll w = M/m[i], d, x, y;
gcd(m[i], w, d, x, y);
ans = (ans + y*w*a[i]) % M;
}
ans = (ans + M) % M;
return ans;
}
int main () {
int cas = 1;
while (init()) {
ll ans = china(t,3);
ll t = ans;
while (ans < D || ans == 0)
ans += M;
printf("Case %d: the next triple peak occurs in %lld days.\n", cas++, ans - D);
}
return 0;
}