问题描述:
人的体力,情感,智力得周期分别为23,28,33.一个周期内有一天为峰值。现在给出三个日期,分别对应于体力智力情感出现峰值的日期。再给出一个起始日期,要求从这一天开始,算出再过多少天后三个峰值会重新出现。
分析:
任意峰值之间相差整数倍,假设一年的第N天达到峰值,则下次峰值时间为N+kT;则三个峰值同时出现的那一天满足:
S=N1+k1*T1=N2+k2*T2=N3+k3*T3
利用中国剩余定理直接求出S,S-起始天数即为所求。
代码:
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define LL long long int
using namespace std;
LL c[4],m[4];
void exgcd(LL a,LL b,LL &x,LL &y){
if (b==0){
x=1;
y=0;
return ;
}
exgcd(b,a%b,x,y);
int t=x;
x=y;
y=t-(a/b)*y;
}
LL CRT(){
LL ans=0;
LL M=1;
for (LL i=1;i<=3;i++){
M*=m[i];
}
for (LL i=1;i<=3;i++){
LL Mi=M/m[i];
LL x,y;
exgcd(Mi,m[i],x,y);
ans=(ans+c[i]*Mi*x)%M;
}
if (ans<0)ans+=M;
return ans;
}
int main(){
LL d;
LL cas=0;
m[1]=23,m[2]=28,m[3]=33;
while (scanf("%lld%lld%lld%lld",&c[1],&c[2],&c[3],&d)!=EOF){
if (c[1]==-1&&c[2]==-1&&c[3]==-1&&d==-1)break;
LL fun=CRT();
if (fun==0)fun=21252;
if (fun<=d)fun+=21252;
d=fun-d;
printf("Case %lld: the next triple peak occurs in %lld days.\n",++cas,d);
}
}