http://poj.org/problem?id=1006
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(long long i=a;i<=b;++i) //by war //2019.8.8 using namespace std; long long p,e,i,d,ans,m,x,y,cnt; long long a[10],b[10]; void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } void exgcd(long long a,long long b,long long &x,long long &y){ if(!b){ x=1;y=0; return ; } exgcd(b,a%b,y,x); y-=a/b*x; } signed main(){ a[1]=23;a[2]=28;a[3]=33; a[0]=a[1]*a[2]*a[3]; while(cin>>b[1]>>b[2]>>b[3]>>d&&(b[1]!=-1)){ ans=0; For(i,1,3){ m=a[0]/a[i]; exgcd(m,a[i],x,y); x=(x%a[i]+a[i])%a[i]; ans=(ans+b[i]*m*x)%a[0]; } ans=((ans-d)%a[0]+a[0])%a[0]; if(ans==0) ans=a[0]; cout<<"Case "<<++cnt<<": the next triple peak occurs in "<<ans<<" days."<<endl; } return 0; }