题意:
$left{ {egin{array}{*{20}{l}}
{res = {r_1}\,mod \,{m_1}}\
{res = {r_2}\,mod \,{m_2}}\
{res = {r_3}\,mod \,{m_3}}
end{array}}
ight.$
解题关键:由于模数互质,直接crt即可。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cmath> 6 #include<iostream> 7 using namespace std; 8 typedef long long ll; 9 ll x,y,r[10],m[10],n=3; 10 ll extgcd(ll a,ll b,ll &x,ll &y){ 11 ll d=a; 12 if(b) d=extgcd(b,a%b,y,x),y-=a/b*x; 13 else x=1,y=0; 14 return d; 15 } 16 ll inv(ll t,ll mod){ extgcd(t,mod,x,y);return (x+mod)%mod;} 17 ll crt(int n,ll *r,ll *m){ 18 ll M=1,ret=0; 19 for(int i=0;i<n;i++) M*=m[i]; 20 for(int i=0;i<n;i++){ 21 ll w=M/m[i]; 22 ret+=w*inv(w,m[i])*r[i]; 23 ret%=M; 24 } 25 return (ret+M)%M; 26 } 27 int main(){ 28 ll a,Case=1; 29 m[0]=23,m[1]=28,m[2]=33; 30 while(cin>>r[0]>>r[1]>>r[2]>>a){ 31 if(a==-1&&r[0]==-1&&r[1]==-1&&r[2]==-1) break; 32 printf("Case %d: ",Case++); 33 ll tmp=23*28*33; 34 ll ans=((crt(3,r,m)-a)%tmp+tmp)%tmp; 35 if(ans==0) ans+=tmp; 36 printf("the next triple peak occurs in %lld days. ",ans); 37 } 38 }