您的位置:首页 > 其它

POJ1006-Biorhythms 中国剩余定理

2018-03-23 22:09 411 查看
Biorhythms
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 143074 Accepted: 46000
DescriptionSome 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. 
InputYou 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.OutputFor 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.
链接

一、题意

        人的物理、情感、智力三个属性都有各自的周期,即属性两次到达峰值之间的时间间隔。以天为单位分别为P=23、E=28、I=33。现在给定这三个属性为峰值的天p,e,i和今天d,问需要经过多少天,三个属性能够同时到达峰值。

二、思路

        可以利用中国剩余定理。设三个属性在d'同时达到峰值,则d'必定满足d' = a*P+p = b*E + e = c*I + i,其中a,b,c都是常熟。这正是中国剩余定理的条件:模P余p,模E余e,模I余i。
        最后注意返回的答案大于零小于等于21252。

三、代码

#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;
typedef long long ll;
const int MAXN = 100100;
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int INF = 2000000000;//0x7fffffff
const double EPS = 1e-9;
const double PI = 3.14159265358979;
const int dir_4r[] = { -1, 1, 0, 0 };
const int dir_4c[] = { 0, 0, -1, 1 };
const int dir_8r[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
const int dir_8c[] = { -1, 0, 1, -1, 1, -1, 0, 1 };

int exGCD(int a, int b, int &x, int &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
int xx, yy;
int gcd = exGCD(b, a%b, xx, yy);
x = yy;
y = xx - a / b * yy;
return gcd;
}

//求ans≡a[](mod m[])
int CRT(int *a, int *m, int n) {
int M = 1;
int ans = 0;
for (int i = 0; i < n; ++i)
M *= m[i];
for (int i = 0; i < n; ++i) {
int x, y;
int Mi = M / m[i];
exGCD(Mi, m[i], x, y);
ans = (ans + Mi * x * a[i]) % M;
}
if (ans < 0)
ans += M;
return ans;
}

int a[10], m[10], d;

int main() {
m[0] = 23;
m[1] = 28;
m[2] = 33;

int kase = 0;
while (scanf("%d %d %d %d", &(a[0]), &(a[1]), &(a[2]), &d)) {
if (a[0] == -1)
break;

int ans = CRT(a, m, 3) - d;
if (ans <= 0)
ans += m[0] * m[1] * m[2];
printf("Case %d: the next triple peak occurs in %d days.\n", ++kase, ans);
}

//system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: