题意:
人自出生起就有体力,情感和智力三个生理周期,分别为23,28和33天。现在给出三个日期,分别对应于体力,情感,智力出现峰值的日期,然后按照周期依次出现。然后再给出一个起始日期,要求从这一天开始,算出最少再过多少天后三个峰值同时出现。
分析:
CRT入门:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int MAXN = 1e3 + 10;
int a[5], m[5];
void ex_gcd(int a, int b, int &x, int &y) {
if(!b) {
x = 1;
y = 0;
return ;
}
ex_gcd(b, a % b, x, y);
int tmp = x;
x = y;
y = tmp - (a / b) * y;
}
//板子
int CRT(int * a, int * m, int n) {
int M = 1, ans = 0;
for(int i = 1; i <= n; i++) {
M *= m[i];
}
for(int i = 1; i <= n; i++) {
int x, y;
int Mi = M / m[i];
ex_gcd(Mi, m[i], x, y);
ans = (ans + Mi * a[i] * x) % M;
}
return ans;
}
int main() {
int p, e, i, d;
while(scanf("%d%d%d%d", &p, &e, &i, &d) && p != -1) {
int x1 = 23, x2 = 28, x3 = 33;
int M = x1 * x2 * x3;
a[1] = p % x1;
a[2] = e % x2;
a[3] = i % x3;
m[1] = x1;
m[2] = x2;
m[3] = x3;
int ans = CRT(a, m, 3);
if(ans <= d) ans += M;
static int tt = 1;
printf("Case %d: the next triple peak occurs in %d days.\n", tt++, ans - d);
}
return 0;
}