九度 oj 题目1142:Biorhythms
来源:互联网 发布:ubuntu官网下载地址 编辑:程序博客网 时间:2024/06/05 15:23
http://ac.jobdu.com/problem.php?pid=1142
此题利用了中国剩余定理
参考了
1.http://www.cnblogs.com/My-Sunshine/p/4830388.html
2.http://blog.csdn.net/sinat_30126425/article/details/52154948
代码来自于:参考2
#include <stdio.h>#define ll long long #define ld long double static ll m[3],a[3];void extend_Euclide(ll a, ll b, ll &x, ll&y){ if (b == 0) { x = 1; y = 0; return; } extend_Euclide(b,a%b,x,y); ll tmp = x; x = y; y = tmp - (a/b)*y;} ll CRT(){ ll M = 1; ll ans = 0; for (int i = 0; i < 3; ++i) { M*=m[i]; } for (int i = 0; i < 3; ++i) { ll x,y; ll Mi = M/m[i]; extend_Euclide(Mi,m[i],x,y); ans = (ans + Mi*x*a[i])%M; } if(ans<0) ans+=M; return ans; } int main(){ // freopen("in/1142.in","r",stdin); int p,e,i,d,nCase=1; m[0]=23,m[1]=28,m[2]=33; while(scanf("%d%d%d%d",&p,&e,&i,&d) !=EOF){ if(p==-1 && e==-1 && i ==-1 && d==-1) break; a[0] = p,a[1] = e,a[2] = i; ll k = CRT(); if(k==0) k = 21252; if(k < d) k+=21252; d = (int) k-d; printf("Case %d: the next triple peak occurs in %d days.\n",nCase++,d); } }
暴力也可解:
#include <stdio.h>int main(){ int p,e,i,d; int nCase = 0; while(scanf("%d%d%d%d",&p,&e,&i,&d)!=EOF){ nCase++; if(p==e && p==i && p==d && p==-1) break; for (int n = 1; n <=21252 ; ++n) { if((n+d-p)%23 == 0 && (n+d-e)%28 == 0&& (n+d-i)%33 == 0){ printf("Case %d: the next triple peak occurs in %d days.\n",nCase,n); break; } } } }
0 0
- 九度 oj 题目1142:Biorhythms
- 九度OJ 1142:Biorhythms(生理周期) (中国剩余定理)
- 九度OJ 题目1029
- 九度OJ 题目1018
- 九度OJ 题目1002
- 题目1142:Biorhythms
- 题目1142:Biorhythms
- 题目1142:Biorhythms
- 九度OJ 题目1058:反序输出
- 九度OJ 题目1062:分段函数
- 九度OJ 题目1063:整数和
- 九度OJ 题目1066:字符串排序
- 九度OJ 题目1089:数字反转
- 九度OJ 题目1098:字母统计
- 九度OJ 题目1032:ZOJ
- 九度OJ 题目1115:数字求和
- 九度OJ 题目1022:游船出租
- 九度OJ 题目1206:字符串连接
- [global::]是什么形式?什么意思?
- ASCII码排序
- 自动从数据库提取数据并发送邮件
- RSA加密Socket传输文件、签名
- 如何将图片转换成BYTE
- 九度 oj 题目1142:Biorhythms
- elasticsearch5 插件ik安装
- sql查询 group by 数字类型+null 为null 处理 ,sum()的结果作为条件再次查询。
- Thymeleaf 之 内置对象、定义变量、URL参数及标签自定义属性
- 多条数据填充List
- GIS空间索引
- ajax如何截取302响应
- About Sublime Text for Mac
- 又是一年了