20160321 POJ1006 Biorhythms (中国剩余定理)
来源:互联网 发布:百度云免费域名 编辑:程序博客网 时间:2024/05/16 16:02
题意:
人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。对于每个周期,我们会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。你的任务是给定一个从当年第一天开始数的天数,输出从给定时间开始(不包括给定时间)下一次三个高峰落在同一天的时间(距给定时间的天数)。
思路:
即求
x≡p(mod23)
x≡e(mod28)
x≡i(mod33)
这个方程组的解,中国剩余定理模板走你。
#include <iostream>using namespace std;int exgcd(int a,int b,int &x,int &y){ if(b==0) { x=1; y=0; return a; } else { int r=exgcd(b,a%b,y,x); y-=x*(a/b); return r; }}int CRT(int a[],int m[],int n){ int M=1; for(int i=0;i<n;i++) M*=m[i]; int ret=0; for(int i=0;i<n;i++) { int x,y; int tm=M/m[i]; exgcd(tm,m[i],x,y); ret=(ret+tm*x*a[i])%M; } //cout<<M<<endl; return (ret+M)%M;}int main(){ int a[3],x,c=1; int k[3]={23,28,33}; while(cin>>a[0]>>a[1]>>a[2]>>x) { if(a[0]==-1) break; for(int i=0;i<3;i++) a[i]=a[i]%k[i]; int ans=CRT(a,k,3); if(ans<=x) ans+=21252; cout<<"Case "<<c++<<": the next triple peak occurs in "<<ans-x<<" days."<<endl; } return 0;}
0 0
- 20160321 POJ1006 Biorhythms (中国剩余定理)
- Biorhythms(poj1006+中国剩余定理)
- poj1006 Biorhythms(中国剩余定理)
- [POJ1006]Biorhythms(中国剩余定理)
- [POJ1006]Biorhythms(中国剩余定理)
- poj1006 Biorhythms(中国剩余定理)
- POJ1006-Biorhythms .中国剩余定理
- Poj1006 Biorhythms (中国剩余定理)
- Biorhythms(poj1006中国剩余定理)
- poj1006 Biorhythms 中国剩余定理
- POJ1006 - Biorhythms - 中国剩余定理
- POJ1006 Biorhythms 中国剩余定理
- POJ1006 Biorhythms【中国剩余定理】
- POJ1006 Biorhythms(中国剩余定理)
- poj1006 Biorhythms中国剩余定理
- poj1006--Biorhythms (中国剩余定理)
- POJ1006 Biorhythms(生理周期,中国剩余定理详述)
- poj1006:Biorhythms(中国剩余定理,扩展欧几里得)
- 中国剩余定理
- UVA 624 CD(01背包+记录路径)
- 程序猿,你累吗?
- Java开发之String与StringBuffer
- 设计一个最优算法来查找n个元素数组中的最大值和最小值
- 20160321 POJ1006 Biorhythms (中国剩余定理)
- 如何使用ssh框架实现用户的注销功能
- eclipse中如何查看sdk
- AfxGetMainWnd() return NULL
- return
- jumpserver 安装
- 3.5Android控件架构与自定义控件详解之自定义View(三)
- kali安装搜狗输入法
- 百钱百鸡