标签:++ main mod col cst stream nbsp ring triple
题意:
$\begin{array}{l}
res = {r_1}\bmod {m_1}\\
res = {r_2}\bmod {m_2}\\
res = {r_3}\bmod {m_3}
\end{array}$
解题关键:由于模数互质,直接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 } 15 ll inv(ll t,ll mod){ extgcd(t,mod,x,y);return (x+mod)%mod;} 16 ll crt(int n,ll *a,ll *m){ 17 ll M=1,ret=0; 18 for(int i=0;i<n;i++) M*=m[i]; 19 for(int i=0;i<n;i++){ 20 ll w=M/m[i]; 21 ret+=w*inv(w,m[i])*a[i]; 22 ret%=M; 23 } 24 return (ret+M)%M; 25 } 26 int main(){ 27 ll a,Case=1; 28 m[0]=23,m[1]=28,m[2]=33; 29 while(cin>>r[0]>>r[1]>>r[2]>>a){ 30 if(a==-1&&r[0]==-1&&r[1]==-1&&r[2]==-1) break; 31 printf("Case %d: ",Case++); 32 ll tmp=23*28*33; 33 ll ans=((crt(3,r,m)-a)%tmp+tmp)%tmp; 34 if(ans==0) ans+=tmp; 35 printf("the next triple peak occurs in %lld days.\n",ans); 36 } 37 }
标签:++ main mod col cst stream nbsp ring triple
原文地址:http://www.cnblogs.com/elpsycongroo/p/7617236.html