Biorhythms(poj1006中国剩余定理)
2013-10-27 11:07
417 查看
题意:三个高峰周期分别为23,28,33,给你一个d这一年过了几天,p,e,i表示前一次三个高峰距d过了多少天,求三个高峰在d之后的第几天出现
思路:中国剩余定理 :/article/10243993.html
根据题意列同与方程
d = p mod 23
d = e mod 28
d = i mod 33
解同余方程得出最小的解x,题目要求d之后的天数所以求通解直到大于d,计算x-d就是结果
思路:中国剩余定理 :/article/10243993.html
根据题意列同与方程
d = p mod 23
d = e mod 28
d = i mod 33
解同余方程得出最小的解x,题目要求d之后的天数所以求通解直到大于d,计算x-d就是结果
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; typedef long long int64; int64 Exgcd(int64 a,int64 b,int64 &x,int64 &y) { if(b == 0) { x = 1,y = 0; return a; } else { int64 r = Exgcd(b,a%b,x,y); int64 temp = x; x = y,y = temp - a/b*y; return r; } } int main() { int64 d,p,e,i,cas = 1; while(scanf("%I64d%I64d%I64d%I64d",&p,&e,&i,&d) != EOF) { if(d == -1 && p == -1 && e == -1 && i == -1) break; int64 M = 23*28*33; int64 A[5] = {23,28,33}; int64 C[5] = {p,e,i}; int64 a,b,r,x,y,ans = 0; for(int i = 0; i < 3; i++) { int64 Mi = M / A[i]; r = Exgcd(Mi,A[i],x,y); b = A[i] / r; x = x / r * C[i]; x = ( x % b + b ) % b; ans = (ans + x*Mi) % M; } while(ans <= d) ans += M; printf("Case %I64d: the next triple peak occurs in %I64d days.\n",cas++,ans-d); } return 0; }
相关文章推荐
- POJ 1006 Biorhythms 中国剩余定理
- POJ 1006 Biorhythms 中国剩余定理
- POJ 1006 Biorhythms 中国剩余定理学习
- POJ 1006 Biorhythms【中国剩余定理】
- POJ 1006 Biorhythms (数论-中国剩余定理)
- 【中国剩余定理 && 扩展欧几里德】 POJ - 1006 Biorhythms
- poj1006 Biorhythms<中国剩余定理>
- POJ 1006 Biorhythms 中国剩余定理
- 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms
- 20140714 「初等数论 - 中国剩余定理」 POJ 1006 Biorhythms
- POJ 1006 Biorhythms 中国剩余定理
- poj1006 Biorhythms中国剩余定理
- poj 1006 Biorhythms 中国剩余定理
- POJ 1006 Biorhythms 中国剩余定理
- POJ 1006-Biorhythms,中国剩余定理,学信安的路过!
- POJ1006_Biorhythms【中国剩余定理】
- POJ 1006 Biorhythms 中国剩余定理
- poj1006_Biorhythms_扩展欧几里得_中国剩余定理
- POJ 1006 Biorhythms (数论-中国剩余定理)
- POJ 1006 Biorhythms【中国剩余定理】