题目链接
思路:很容易可以看出用中国剩余定理,又23,28,33三个数互质,因此选用互素版的中国剩余定理
ac:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=3;
int m[N]={23,28,33};
int a[N];
int exgcd(int a,int b,int &x,int &y)
{
if(!b){
x=1,y=0;
return a;
}
int d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
int CRT(int a[],int m[],int n)
{
int M=23*28*33;
int ans=0,t,x,y;
for(int i=0;i<n;i++){
t=M/m[i];
exgcd(t,m[i],x,y);
ans=(ans+a[i]*x*t)%M;
}
return (ans%M+M)%M;
}
int main()
{
int p,e,i,d;
int kase=0;
while(~scanf("%d%d%d%d",&p,&e,&i,&d)&&p!=-1){
a[0]=p,a[1]=e,a[2]=i;
int ans=CRT(a,m,3);
if(ans==d)ans=d+23*28*33;
else {
ans=ans-d;
while(ans<0)ans+=23*28*33;
}
printf("Case %d: the next triple peak occurs in %d days.\n",++kase,ans);
}
return 0;
}