板子题:
#include<string.h>
#include<stdio.h>
using namespace std;
#define LL long long
#define N 100005
void ex_gcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){
d=a,x=1,y=0;
}else{
ex_gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
LL inv(LL t,LL p){
LL d,x,y;
ex_gcd(t,p,d,x,y);
return d == 1?(x%p+p)%p:-1;
}
LL China(int n,LL *a,LL *m){
LL M = 1,ret = 0;
for(int i=0;i<n;i++)M*=m[i];
for(int i=0;i<n;i++){
LL w = M/m[i];
ret = (ret+w*inv(w,m[i])*a[i])%M;
}
return ret;
}
int main(){
LL p[3],r[3],d,ans,Mod = 21252;
int icase=0;
p[0]=23,p[1]=28,p[2]=33;
while(~scanf("%lld %lld %lld %lld",&r[0],&r[1],&r[2],&d)){
if(r[0]==-1&&r[1]==-1&&r[2]==-1&&d==-1)break;
ans = (China(3,r,p)-d+Mod)%Mod;
if(ans == 0)ans=Mod;
printf("Case %d: the next triple peak occurs in %lld days.\n",++icase,ans);
}
}