poj 1006 中国剩余定理解同余方程
2014-11-01 11:52
274 查看
其实画个图就明白了,
![](http://images.cnitblog.com/blog/638051/201411/011150486127761.png)
该问题就是求同余方程组的解:
n+d≡p (mod 23)
n+d≡e (mod 28)
n+d≡i (mod 33)
![](http://images.cnitblog.com/blog/638051/201411/011150486127761.png)
该问题就是求同余方程组的解:
n+d≡p (mod 23)
n+d≡e (mod 28)
n+d≡i (mod 33)
#include "iostream" using namespace std; int a[5],m[5]; int p,e,i,d,ans; int extend_gcd(int a,int b,int &x,int &y){ if (b==0){ x=1;y=0; return a; } else{ int r=extend_gcd(b,a%b,y,x); y=y-x*(a/b); return r; } } int CRT(int a[],int m[],int n) { int M=1; for (int i=1;i<=n;i++) M*=m[i]; int ret=0; for (int i=1;i<=n;i++) { int x,y; int tm=M/m[i]; extend_gcd(tm,m[i],x,y); ret=(ret+tm*x*a[i])%M; } return (ret+M)%M; } int main() { int T=0; while (cin>>p>>e>>i>>d) { T++; if ((p==-1)&&(e==-1)&&(i==-1)&&(d==-1)) break; a[1]=p; a[2]=e; a[3]=i; m[1]=23; m[2]=28; m[3]=33; ans=CRT(a,m,3); ans=ans-d; if (ans<0) ans+=21252; ans=ans%21252; if (ans==0) ans=21252; //Case 1: the next triple peak occurs in 1234 days. cout<<"Case "<<T<<": the next triple peak occurs in "<<ans<<" days."<<endl; } return 0; }
相关文章推荐
- poj 1006+hdu 1788(中国剩余定理求解同余方程组)
- POJ1006生理周期----【模板】CRT中国剩余定理即孙子定理即求解一次同余方程
- POJ 1006 Biorhythms 中国剩余定理
- POJ1006 UVA756 UVALive5421 Biorhythms【中国剩余定理】
- POJ 1006 ——Biorhythms 中国剩余定理模板
- poj1006中国剩余定理
- POJ 1006 Biorhythms --中国剩余定理(互质的)
- poj 1006 中国剩余定理模板
- poj 1006 Biorhythms 中国剩余定理
- poj1006 Biorhythms<中国剩余定理>
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
- POJ 1006 Biorhythms [中国剩余定理] 超短代码
- poj 1006中国剩余定理的应用
- hdu 1370 || poj 1006 简单的中国剩余定理或者暴力
- 中国剩余定理 ( POJ 1006 )
- Poj 1006 中国剩余定理
- POJ 1006 Biorhythms (数论-中国剩余定理)
- poj 1006 中国剩余定理模板
- poj1006--中国剩余定理
- poj1006中国剩余定理