poj 1006 && hdoj 1370 Biorhythms 【CRT】
来源:互联网 发布:数据接口开发 编辑:程序博客网 时间:2024/04/30 14:55
Biorhythms
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 122930 Accepted: 38781
Description
Some people believe that there are three cycles in a person's life that start the day he or she is born. These three cycles are the physical, emotional, and intellectual cycles, and they have periods of lengths 23, 28, and 33 days, respectively. There is one peak in each period of a cycle. At the peak of a cycle, a person performs at his or her best in the corresponding field (physical, emotional or mental). For example, if it is the mental curve, thought processes will be sharper and concentration will be easier.
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak.
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak.
Input
You will be given a number of cases. The input for each case consists of one line of four integers p, e, i, and d. The values p, e, and i are the number of days from the beginning of the current year at which the physical, emotional, and intellectual cycles peak, respectively. The value d is the given date and may be smaller than any of p, e, or i. All values are non-negative and at most 365, and you may assume that a triple peak will occur within 21252 days of the given date. The end of input is indicated by a line in which p = e = i = d = -1.
Output
For each test case, print the case number followed by a message indicating the number of days to the next triple peak, in the form:
Case 1: the next triple peak occurs in 1234 days.
Use the plural form ``days'' even if the answer is 1.
Case 1: the next triple peak occurs in 1234 days.
Use the plural form ``days'' even if the answer is 1.
Sample Input
0 0 0 00 0 0 1005 20 34 3254 5 6 7283 102 23 320203 301 203 40-1 -1 -1 -1
Sample Output
Case 1: the next triple peak occurs in 21252 days.Case 2: the next triple peak occurs in 21152 days.Case 3: the next triple peak occurs in 19575 days.Case 4: the next triple peak occurs in 16994 days.Case 5: the next triple peak occurs in 8910 days.Case 6: the next triple peak occurs in 10789 days.
poj有译文,原文右上角。
用exgcd求出乘法逆,剩下的CRT模板都用不到。
求乘法逆代码:
#include <cstdio>#define LL long longLL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b);}LL exgcd(LL a, LL b, LL &d, LL &x, LL &y){ if(b == 0) {d = a, x = 1, y = 0;} else { exgcd(b, a%b, d, y, x); y -= x * (a / b); }}LL inv(LL a, LL n){ LL d, x, y; exgcd(a, n, d, x, y); return d == 1 ? (x + n) % n : -1;}int main(){ printf("%lld\n", inv(28*33, 23)); printf("%lld\n", inv(23*33, 28)); printf("%lld\n", inv(23*28, 33)); return 0;}
AC代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int main(){ int k = 1; int M1 = 28 * 33 * 6; int M2 = 23 * 33 * 19; int M3 = 23 * 28 * 2; int LCM = 28 * 33 * 23; int p, e, i, d; //printf("%d %d %d %d\n", M1, M2, M3, LCM); while(scanf("%d%d%d%d", &p, &e, &i, &d) != EOF) { if(p == -1 && e == -1 && i == -1 && d == -1) break; int ans = (M1*p + M2*e + M3*i - d + LCM) % LCM; if(ans == 0) ans = LCM; printf("Case %d: the next triple peak occurs in %d days.\n", k++, ans); } return 0;}
0 0
- poj 1006 && hdoj 1370 Biorhythms 【CRT】
- POJ 1006-Biorhythms(CRT)
- 【POJ 1006 】Biorhythms 【CRT】
- 【POJ】1006 - Biorhythms(CRT)
- 【Poj 1006】生理周期Biorhythms--CRT
- 【数论】 HDOJ 1370 && POJ 1006 && ZOJ 1160 Biorhythms
- 【POJ 1006】【CRT(中国剩余定理)模板题】Biorhythms
- hdoj-1370-Biorhythms
- 中国剩余定理(CRT 孙子定理)——Biorhythms(POJ 1006)
- POJ 1006 && HDU 1370 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
- Android Dialog的十种类型汇总
- opencv3累计概率霍夫变换-HoughLinesP函数
- Django跨站伪造请求保护措施设置方法
- 面向对象的三个特征
- opencv3实现多种重映射
- poj 1006 && hdoj 1370 Biorhythms 【CRT】
- Android lint 自动检测并删除无用资源
- opencv3实现重映射-remap函数
- 在Unity3D中加载外部图片的两种方法
- 程序员的发展之道---海贼王(山治)
- 10月16日
- opencv3直方图均衡化
- 快速幂模版
- 【图像处理】双线性插值