CSUOJ 1858 Biorhythms 中国剩余定理

1858: Biorhythms

Submit Page   Summary   Time Limit: 1 Sec     Memory Limit: 256 Mb     Submitted: 69     Solved: 37


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.

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) separated by spaces. 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 cycle peaks, 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 are guaranteed that a triple peak will occur within 21252 days of the given date. A line in which all four values are -1 indicates the end of input.

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 x: the next triple peak occurs in y days.
where x and y are replaced by the appropriate values.

Sample Input

0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 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.

Hint

Source

1999 Pacific NW Regional Contest

http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1858

多校补题

直接套中国剩余定理的模板

#include <iostream>
#include <cstdio>
#include <algorithm>

#define LL long long

using namespace std;

LL gcd(LL a, LL b, LL &x, LL &y) {
    if(b == 0) {
        x =1, y = 0;
        return a;
    }
    LL r = gcd(b, a % b, x, y);
    LL t  = y;
    y = x - a / b * y;
    x = t;
    return r;
}

LL multi(LL a, LL b, LL mod) {
    LL ret = 0;
    while(b) {
        if(b & 1) {
            ret = ret + a;
            if(ret >= mod) ret -= mod;
        }
        a = a + a;
        if(a >= mod) a -= mod;
        b >>= 1;
    }
    return ret;
}

LL crt(int n, LL m[], LL a[]) {
    LL M = 1, d, y, x = 0;
    for(int i = 0; i < n; i++) M *= m[i];
    for(int i = 0; i < n; i++) {
        LL w = M / m[i];
        d = gcd(m[i], w, d, y);
        y = (y % m[i] + m[i]) % m[i];
        x = ((x + multi(multi(a[i], w, M), y, M)) % M + M) % M;
    }
    return x;
}

int main()
{
    int p, e, i, d;
    int cnt = 1;
    LL a[5], m[5];
    while(~scanf("%d%d%d%d", &p, &e, &i, &d)){
        if(p == e && e == i && i == d && d == -1) break;
        m[0] = 23;
        m[1] = 28;
        m[2] = 33;
        a[0] = p;
        a[1] = e;
        a[2] = i;
        int ans = crt(3, m, a);
        if(ans <= d) ans += 21252;
        printf("Case %d: the next triple peak occurs in %d days.\n", cnt++, ans - d);

    }
    return 0;
}

/**********************************************************************
	Problem: 1858
	User: luoggg
	Language: C++
	Result: AC
	Time:0 ms
	Memory:1700 kb
**********************************************************************/

  

时间: 2024-12-25 01:02:35

CSUOJ 1858 Biorhythms 中国剩余定理的相关文章

POJ 1006:Biorhythms 中国剩余定理

Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 121194   Accepted: 38157 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,

北大ACM1006——Biorhythms~~中国剩余定理

中国剩余定理,百度一下,就有它的定义与证明. 这里我就讲一个例子就好了. 题目的意思就是给你p,e,i,d.(n + d)% 23 = p,(n + d) % 28 = e,(n + d) % 33 = i.求最小n. 将n+d看成一个整体,m =n + d. 要求m: 先使 28 * 33 * a % 23 = 1,求出a,x = 28 * 33 * a: 使 23 * 33 * b % 28 = 1,求出b,y = 23 * 33 * b: 使23 * 28 * c % 33 = 1,求出c

数论E - Biorhythms(中国剩余定理,一水)

E - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status 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

转载----POJ 1006 中国剩余定理

本文为转载,源地址:   http://blog.csdn.net/dongfengkuayue/article/details/6461298 POJ 1006   Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78980   Accepted: 23740 Description Some people believe that there are three cycles in a perso

gcd,扩展欧几里得,中国剩余定理

1.gcd: int gcd(int a,int b){ return b==0?a:gcd(b,a%b); } 2.中国剩余定理: 题目:学生A依次给n个整数a[],学生B相应给n个正整数m[]且两两互素,老师提出问题:有一正整数ans,对于每一对数,都有:(ans-a[i])mod m[i]=0.求此数最小为多少. 输入样例: 1 10 2 3 1 2 3 2 3 5 8 1 2 3 4 5 6 7 8 97 89 67 61 59 53 47 88 12 1 2 3 4 5 6 7 8 9

HDU 1573 X问题 中国剩余定理

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题意:求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], -, X mod a[i] = b[i], - (0 < a[i] <= 10). 思路:中国剩余定理的模板题,如果找不到这样的数或者最小的X大于N,输出零. 代码: #include <iostream> #include

同余 模算术 中国剩余定理

相关知识点: 1.a≡b(modc),a,b关于模c同余  ,即a modc=b mod c , 等价于a%c=b 2.如果a,b互质(a,b)=1,则可得a关于模b的逆 ax≡1(modb) 3.关于余数的定理: 定理1 :如果被除数加上(或减去)除数的整数倍,除数不变,则余数不变. 定理2 :如果被除数扩大(或缩小)几倍,除数不变,则余数也扩大(或缩小)同样的倍数. 定理3: 如果整数a除以自然数b(b≠0),余数r仍不小于b,则r除以b的余数等于a除以b所得余数.(余数和被除数关于除数同余

hihocode 九十七周 中国剩余定理

题目1 : 数论六·模线性方程组 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:今天我听到一个挺有意思的故事! 小Hi:什么故事啊? 小Ho:说秦末,刘邦的将军韩信带领1500名士兵经历了一场战斗,战死四百余人.韩信为了清点人数让士兵站成三人一排,多出来两人:站成五人一排,多出来四人:站成七人一排,多出来六人.韩信立刻就知道了剩余人数为1049人. 小Hi:韩信点兵嘛,这个故事很有名的. 小Ho:我觉得这里面一定有什么巧妙的计算方法!不然韩信不可能这么快计

POJ 1006 中国剩余定理

[题意]: 给定p,e,i,d,求解 (x + d) % 23 = p (x + d) % 28 = e(x + d) % 33 = i x最小正整数值 [知识点]: 中国剩余定理 [题解]: 典型的 xmodmi = ai模型,其中mi间两两互素.但该题式子较少,也可以直接自己化简带入值. [代码]: 1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <ctime> 5 #inc