POJ 1006 Biorhythms(中国剩余定理互质版本)
来源:互联网 发布:石头剪刀布 网络歌手 编辑:程序博客网 时间:2024/06/05 18:35
http://poj.org/problem?id=1006
X mod a1 = b1
X mod a2 = b2
……
X mod an = bn
如果所有a之间两两互质,求X
1、HDOJ要用__int64,并且避免几种类型数据混合使用。
2、extend_gcd一定要在CRT前面,因为CRT包含了extend_gcd。同理主函数一定是放在最后的,因为它包含所有子函数。
3、注意一下传数组参数的方法
实参都是a, 形参
1) a[]
2) int* a
4、除去后面的数据特殊处理,ans算出来的是所有满足条件的X中的最小非负整数解
5、a mod b = (a % b + b) % b %计算出来的可能是负数,取决于a的正负,但是mod算出来的一定是正的。a mod b = r a = b * q + r (b > 0 and 0 <= r < b),r就是满足x mod b = r的最小非负整数 注意 b>0。当a>0时,mod 和 %就无所谓了。
6、如果a mod b = c,那么(a+kb) mod b = c
如果a mod b = c,那么(a*k) mod b = c
#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define LL __int64using namespace std;LL n, k, m[100], a[100], T, cnt, D, ans;//模数为m,余数为a, X % m = a//m互素LL extend_gcd(LL a, LL b, LL &x, LL &y){ if(a == 0 && b == 0) return -1; if(b ==0 ) { x = 1; y = 0; return a; } LL d = extend_gcd(b,a%b,y,x); y -= a/b*x; return d;}LL CRT(LL a[], LL m[], LL n){ LL M = 1; LL ans = 0; for(int i = 0; i < n; i++) M *= m[i]; for(int i = 0; i < n; i++) { LL x, y; LL Mi = M / m[i]; extend_gcd(Mi, m[i], x, y); ans = ((ans + Mi * x * a[i]) % M + M) % M; } ans = ((ans - D) % M + M) % M; ans = ans % 21252; if (ans == 0) ans = 21252; return ans;}int main(){ scanf("%I64d", &T); m[0] = 23; m[1] = 28; m[2] = 33; while (T--) { cnt = 0; while (~scanf("%I64d%I64d%I64d%I64d", &a[0], &a[1], &a[2], &D) && a[0] != -1) { cnt++; LL ans = CRT(a, m, 3); printf("Case %I64d: the next triple peak occurs in %I64d days.\n", cnt, ans); } } return 0;}
0 0
- 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(中国剩余定理)
- 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 (中国剩余定理)
- Java并发编程:Callable、Future和FutureTask
- MFC程序查看内存泄露方法
- 关于String ,StringBuffer的性能
- ORACLE中BFILE字段的使用研究
- U-boot启动过程
- POJ 1006 Biorhythms(中国剩余定理互质版本)
- 4.交叉编译
- js中Math方法的小技巧
- linux上访问页面
- Swift的一些基本属性5 (for循环)
- Android Studio 小技巧合集
- 欢迎使用CSDN-markdown编辑器test
- Kindeditor上传本地图片成功后不回显,弹出层也不关闭,解决办法
- 使用Hierarchy Viewer进行UI性能优化