数论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 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. 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.

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.

刚接触中国剩余定理,感觉好神奇啊,x = ( M1*a1*c1 + M2*a2*c2 + M3*a3*c3 。。 )% M ;

M = a1 * a2 *a3.......an ;  要求ai必须互素,不互素的话不能使用中国剩余定理

Mi = M / ai ;    Mi*ai ≡ 1 (mod mi) ;

承认解释不了原因。。。。。。 求大牛解释。。。。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
void gcd(int a,int b,int &d,int &x,int &y)
{
    if(b == 0)
    {
        d = a ;
        x = 1 ;
        y = 0 ;
    }
    else
    {
        gcd(b,a%b,d,y,x);
        x = -x ; y = -y ;
        y += a/b*x ;
    }
    return ;
}
int main()
{
    int temp = 0 , i , a , b , c , num , d , x , y , M , ans ;
    while(scanf("%d %d %d %d", &a, &b, &c, &num)!=EOF)
    {
        if(a + b + c + num == -4)
            break;
        a %= 23 ; b %= 28 ; c %= 33 ;
        ans = 0 ;
        M = 23*28*33 ;
        gcd(M/23,23,d,x,y);
        x = (x%23+23)%23 ;
        ans += M/23*x*a ;
        gcd(M/28,28,d,x,y);
        x = (x%28+28)%28 ;
        ans += M/28*x*b ;
        gcd(M/33,33,d,x,y);
        x = (x%33+33)%33;
        ans += M/33*x*c ;
        ans %= M ;
        if(ans-num <= 0)
            ans += M ;
        printf("Case %d: the next triple peak occurs in %d days.\n", ++temp, ans-num);
    }
    return 0;
}

数论E - Biorhythms(中国剩余定理,一水),布布扣,bubuko.com

时间: 2024-12-17 22:59:06

数论E - 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

acm数论之旅--中国剩余定理

中国剩余定理,又名孙子定理o(*≧▽≦)ツ 能求解什么问题呢? 问题: 一堆物品 3个3个分剩2个 5个5个分剩3个 7个7个分剩2个 问这个物品有多少个 解这题,我们需要构造一个答案 我们需要构造这个答案 5*7*inv(5*7,  3) % 3  =  1 3*7*inv(3*7,  5) % 5  =  1 3*5*inv(3*5,  7) % 7  =  1 这3个式子对不对,别告诉我逆元你忘了(*′?`*),忘了的人请翻阅前几章复习 然后两边同乘你需要的数 2 * 5*7*inv(5*

数论总结之 中国剩余定理

背景 我!终于学会孙子定理了!!!!!!!!!!!!!!!!!!!!!!!!!!! 好难啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! QAQ 中国剩余定理 中国剩余定理又名孙子定理. 在了解中国剩余定理之前,我先放出之前终止了我很长时间的懵逼的一段话. 中国剩余定理介绍 在<孙子算经>中有这样一个问题:“今有物不知其数,三三数之剩二(除以3余2),五五数之剩三(除以5余3),七七数之剩二(除以7余2),问物几何?”这个问题称为“孙子问题”,该问题的一般解法国际上称为

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 cycl

【数学】【数论】拓展中国剩余定理

写在前面 记录了个人的学习过程,同时方便复习 拓展中国剩余定理 [?]中国剩余定理仅仅适用于每个模数两两互质的情况 但是最常见的情况还是所有模数不满足两两互质 这样的话,中国剩余定理就不再适用 需要使用拓展中国剩余定理 中国剩余定理的思路是整体处理 但是当模数不满足两两互素时,就不能处理其中关键的一步: (详见[?]中国剩余定理) 不满足两两互素,即某些模数之间的最大公因数不是1,解出的方程余数不为1 所以拓展中国剩余定理使用的是另一种思路: 逐个处理,直至出现无解情况或者结束! 原文地址:ht

【数论】不定方程&amp;&amp;中国剩余定理

要求逆元,首先要知道什么是不定方程. 已知a,b,c,求解x,y,形如ax + by = c 的方程就是不定方程. 不定方程有两种解的情况: 1.无解 2.存在且有无限的解 那么,如何判断解的情况呢? 这时候,只需要拿出gcd就可以了, 若gcd(a,b) | c,则方程存在解,为什么呢 因为我们要使用扩展欧几里得来求不定方程,我们都知道欧几里得是求 ax + by = gcd(a,b) 中的 x,y的,因此如果我们要把c代换成gcd(a,b)的话,c一定是gcd(a,b)的整数倍,因此gcd(

同余 模算术 中国剩余定理

相关知识点: 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:我觉得这里面一定有什么巧妙的计算方法!不然韩信不可能这么快计