【poj 1006】Biorhythms 中国剩余定理
2016-11-13 20:48
405 查看
http://blog.csdn.net/acdreamers/article/details/8050018
中国剩余定理:
x≡a1(mod m1)
x≡a2(mod m2)
x≡a3(mod m3)
x≡a4(mod m4)
x≡a5(mod m5)
.................
其中m1,m2,m3,m4.......之间互质,那么我们令M=m1*m2*m3*4.......在%M的意义下x有唯一解
X≡(a1*M1*M1-1 +a2*M2*M2-1+.....ai*Mi*Mi-1)
Mi-1
是对于的mi逆元
好了,思考这一道题,题目化简成数学公式就是求:(n+d)%23=p (n+d)%28=i (n+d)%33=e
求解n,为了方便我们将n+d表示成x再化简就是 : x≡p(mod 23)
x≡i(mod 28) x≡e(mod 33)
直接套用模板就好了
#include<cstdio>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
LL P,E,I,D;
LL m[]={0,23,28,33},M=21252ll,a[5];
void exgcd(LL a,LL b,LL& g,LL& x,LL& y){
if(b==0){g=a;x=1,y=0;}
else{
exgcd(b,a%b,g,y,x);
y-=x*(a/b);
}
}
LL CRT(){
LL ans=0;
LL x,y,g,mi;
for(LL i=1;i<=3;i++){
mi=M/m[i];
exgcd(mi,m[i],g,x,y);
ans=(ans+a[i]*mi*x)%M;
}
return ans;
}
int main(){
LL cas=0;
while(scanf("%lld%lld%lld%lld",&P,&E,&I,&D)&&(P+E+I+D)>=0){
a[1]=P,a[2]=E,a[3]=I;
LL ans=CRT();
while(ans<=D)ans+=M;
printf("Case %lld: the next triple peak occurs in %lld days.\n",++cas,ans-D);
}
return 0;
}
中国剩余定理:
x≡a1(mod m1)
x≡a2(mod m2)
x≡a3(mod m3)
x≡a4(mod m4)
x≡a5(mod m5)
.................
其中m1,m2,m3,m4.......之间互质,那么我们令M=m1*m2*m3*4.......在%M的意义下x有唯一解
X≡(a1*M1*M1-1 +a2*M2*M2-1+.....ai*Mi*Mi-1)
Mi-1
是对于的mi逆元
好了,思考这一道题,题目化简成数学公式就是求:(n+d)%23=p (n+d)%28=i (n+d)%33=e
求解n,为了方便我们将n+d表示成x再化简就是 : x≡p(mod 23)
x≡i(mod 28) x≡e(mod 33)
直接套用模板就好了
#include<cstdio>
#include<cstring>
#include<iostream>
#define LL long long
using namespace std;
LL P,E,I,D;
LL m[]={0,23,28,33},M=21252ll,a[5];
void exgcd(LL a,LL b,LL& g,LL& x,LL& y){
if(b==0){g=a;x=1,y=0;}
else{
exgcd(b,a%b,g,y,x);
y-=x*(a/b);
}
}
LL CRT(){
LL ans=0;
LL x,y,g,mi;
for(LL i=1;i<=3;i++){
mi=M/m[i];
exgcd(mi,m[i],g,x,y);
ans=(ans+a[i]*mi*x)%M;
}
return ans;
}
int main(){
LL cas=0;
while(scanf("%lld%lld%lld%lld",&P,&E,&I,&D)&&(P+E+I+D)>=0){
a[1]=P,a[2]=E,a[3]=I;
LL ans=CRT();
while(ans<=D)ans+=M;
printf("Case %lld: the next triple peak occurs in %lld days.\n",++cas,ans-D);
}
return 0;
}
相关文章推荐
- POJ 1006 Biorhythms (数论-中国剩余定理)
- POJ 1006 Biorhythms (数论-中国剩余定理)
- 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms
- poj 1006 Biorhythms 中国剩余定理
- poj1006 Biorhythms<中国剩余定理>
- POJ 1006 Biorhythms --中国剩余定理(互质的)
- POJ 1006 Biorhythms [中国剩余定理]
- POJ 1006 Biorhythms [中国剩余定理] 超短代码
- POJ 1006 ——Biorhythms 中国剩余定理模板
- POJ 1006 Biorhythms 中国剩余定理学习
- POJ 1006 Biorhythms 中国剩余定理
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
- Biorhythms(poj1006中国剩余定理)
- POJ 1006 Biorhythms 中国剩余定理
- POJ1006 UVA756 UVALive5421 Biorhythms【中国剩余定理】
- 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms
- POJ 1006---Biorhythms【中国剩余定理】
- poj 1006 Biorhythms 中国剩余定理
- POJ 1006-Biorhythms,中国剩余定理,学信安的路过!
- poj 1006 Biorhythms 中国剩余定理