poj 1006中国剩余定理的应用
2016-08-09 09:33
447 查看
这道题主要讲的是人有是哪个生理周期,分别是身体,情感和智力,这三个周期,他们的为其时间是23,28和33.已经知道这一年里从第一天开始第一个生理周期出现的时间分别是P,e,i这三天,现在要求计算d天后多少天这三个周期同时出现?当然结果保证在21252天内的某一天出现,注意这里是求d天后过多少天这些周期同时出现,是从d天后之后,还会过多少天,所以结果应该要减去d
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int m[4],M,a[4];
template <typename T>
T exgcd(int a,int b,int &x,int &y)
{
if(!b){x=1;y=0;return a;}
else
{
T d=exgcd<T>(b,a%b,y,x);
y-=a/b*x;
return d;
}
}
//template<typename T>
int china(int n)//只适用于针对那些模互质的情况
//在这里就是指x=a[i]%m[i]中的m[i]分别互质
{
int ans=0;
M=1;
for(int i=0;i<n;i++)
M*=m[i];
int x,y,d;
for(int i=0;i<n;i++)
{
int Mi=M/m[i];
d=exgcd<int>(Mi,m[i],x,y);
ans=(ans+Mi*x*a[i])%M;
}
if(ans<0)
ans+=M;
return ans;
}
int main()
{
int d,time=1;
while(~scanf("%d%d%d%d",&a[0],&a[1],&a[2],&d))
{
if(a[0]==-1&&a[1]==-1&&a[2]==-1&&d==-1)break;
m[0]=23;m[1]=28;m[2]=33;
int ans=china(3);
while(ans<=d)ans+=M;
printf("Case %d: the next triple peak occurs in %d days.\n",time++,ans-d);//注意祥和里就行了,其他的就都是模板了!
}
}
相关文章推荐
- poj 1006(中国剩余定理的应用)
- poj 1006 Biorhythms(中国剩余定理的应用)
- poj 1006 中国剩余定理
- POJ1006 中国剩余定理
- 每日ACM小题--POJ1006——中国剩余定理的完美诠释 2013年11月04(附录POJ_1005水题一枚)
- POJ1006-Biorhythms .中国剩余定理
- POJ 1006 中国剩余定理(孙子定理) ——数论
- POJ 1006 中国剩余定理
- POJ 1006 中国剩余定理
- POJ-1006 Biorhythms 中国剩余定理
- POJ 1006 Biorhythms 中国剩余定理
- poj 1006 Biorhythms 中国剩余定理
- POJ 1006 中国剩余定理
- POJ1006 中国剩余定理
- poj1006--中国剩余定理
- poj 1006 Biorhythms 中国剩余定理
- POJ 1006 中国剩余定理?不用那么故弄玄虚吧。。。
- POJ1006 中国剩余定理
- poj1006中国剩余定理
- poj1006 Biorhythms<中国剩余定理>