poj1006 Biorhythms中国剩余定理
2017-06-22 23:28
411 查看
题意:人类从一开始有三个生物周期:体力、情绪和智力,周期分别为23、28和33天因而三个生物周期产生的高潮不同,已知每个生物周期上次高潮距离年初的天数d,以及从年初开始的一个天数(起始时间),问你下次生物周期高潮同时产生距离起始时间的天数
设下次高潮距离年初时间为x,m1=23,m2=28,m3=33 赏赐高潮产生的时间为b1,b2,b3
则由题意知:xi≡bi(mod mi)
由于m1,m2,m3互素,故用中国剩余定理即可
注意x可能小于d
#include<cstdio>
#include<iostream>
using namespace std;
int x,y;
int Extended(int a,int b){
if(b==0){
x=1;
y=0;
return a;
}
int d=Extended(b,a%b);
int temp=x;
x=y;
y=temp-a/b*y;
return d;
}
int main(){
int cas=0;
int M=23*28*33;
int p,e,i,d;
while((cin>>p>>e>>i>>d)&&(p!=-1||e!=-1||i!=-1||d!=-1)){
int a[4]={p,e,i};
int m[4]={23,28,33};
int ans=0;
for(int i=0;i<3;i++){
int m0=M/m[i];
Extended(m0,m[i]);
ans=(ans+a[i]*m0*x)%M;
}
if(ans<=d)
ans+=M;
ans=ans-d;
cout<<"Case "<<++cas<<": the next triple peak occurs in "<<ans<<" days."<<endl;
}
}
设下次高潮距离年初时间为x,m1=23,m2=28,m3=33 赏赐高潮产生的时间为b1,b2,b3
则由题意知:xi≡bi(mod mi)
由于m1,m2,m3互素,故用中国剩余定理即可
注意x可能小于d
#include<cstdio>
#include<iostream>
using namespace std;
int x,y;
int Extended(int a,int b){
if(b==0){
x=1;
y=0;
return a;
}
int d=Extended(b,a%b);
int temp=x;
x=y;
y=temp-a/b*y;
return d;
}
int main(){
int cas=0;
int M=23*28*33;
int p,e,i,d;
while((cin>>p>>e>>i>>d)&&(p!=-1||e!=-1||i!=-1||d!=-1)){
int a[4]={p,e,i};
int m[4]={23,28,33};
int ans=0;
for(int i=0;i<3;i++){
int m0=M/m[i];
Extended(m0,m[i]);
ans=(ans+a[i]*m0*x)%M;
}
if(ans<=d)
ans+=M;
ans=ans-d;
cout<<"Case "<<++cas<<": the next triple peak occurs in "<<ans<<" days."<<endl;
}
}
相关文章推荐
- POJ 1006 Biorhythms [中国剩余定理]
- poj1006 Biorhythms<中国剩余定理>
- POJ 1006 Biorhythms --中国剩余定理(互质的)
- POJ 1006 Biorhythms [中国剩余定理] 超短代码
- POJ 1006 ——Biorhythms 中国剩余定理模板
- POJ 1006 Biorhythms 中国剩余定理
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
- POJ 1006 Biorhythms 中国剩余定理学习
- Biorhythms(poj1006中国剩余定理)
- POJ1006 UVA756 UVALive5421 Biorhythms【中国剩余定理】
- 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms
- POJ 1006---Biorhythms【中国剩余定理】
- POJ 1006 Biorhythms 中国剩余定理
- poj 1006 Biorhythms 中国剩余定理
- POJ 1006 Biorhythms(中国剩余定理 模板)
- 【中国剩余定理 && 扩展欧几里德】 POJ - 1006 Biorhythms
- POJ 1006-Biorhythms,中国剩余定理,学信安的路过!
- poj 1006 Biorhythms 中国剩余定理
- POJ 1006 Biorhythms 中国剩余定理
- POJ 1006 Biorhythms 中国剩余定理