• POJ 1006 Biorhythms


    https://cn.vjudge.net/problem/POJ-1006

    题目

    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.
    

    题解

    以前看到这题觉得好神奇啊!维基百科一查,原来是伪科学……

    孙子定理= =

    [xequiv b_ipmod{a_i},1leqslant i leqslant n]

    套用公式

    [xequiv sum_{i=1}^n M_iM'_ib_ipmod{prod_{i=1}^n a_i}]

    其中

    [ m_iM_i=m=prod_{i=1}^n a_i, M_iM'_iequiv 1 pmod{a_i}]

    推导过程见《数论讲义》= =

    这个式子是构造出来的,对于$i e j$,可以得到,$M_iM'_ib_iequiv 0 pmod{a_j}$

    这个答案是模$prod_{i=1}^n a_i$意义下的唯一解,可以用完全剩余系的规律推出(若对于$a_i$的完全剩余系中有两个不同的整数$x,y$满足$xequiv ypmod{a_i}$,则可以得到$xequiv ypmod{prod_{i=1}^n a_i}$,这又是整数唯一分解定理和同余与整除互相转换得出的……)

    最大的收获就是一定要打好基础再看= =不然看不懂,就像之前买了一本数学一本通然后第二题就劝退一样……

    对于$M_iM'_iequiv 1 pmod{a_i}$

    因为$GCD(M_i,a_i)=1mid 1$,因此有模某个数意义下的唯一解,将同余化为一次不定方程,利用EXGCD得出一组解

    越来越说不清楚……我太垃圾了

    AC代码

    #include<cstdio>
    #include<cstdlib>
    #include<cctype>
    #include<cstring>
    #include<algorithm>
    #include<set>
    #define REP(r,x,y) for(register int r=(x); r<(y); r++)
    #define REPE(r,x,y) for(register int r=(x); r<=(y); r++)
    #define PERE(r,x,y) for(register int r=(x); r>=(y); r--)
    #ifdef sahdsg
    #define DBG(...) printf(__VA_ARGS__),fflush(stdout)
    #else
    #define DBG(...) (void)0
    #endif
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    char ch; int si;
    template<class T>
    inline void read(T &x) {
        x=0; si=1; for(ch=getchar();!isdigit(ch) && ch!='-';ch=getchar());
        if(ch=='-'){si=-1,ch=getchar();} for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
        x*=si;
    }
    //template<class T, class...A> inline void read(T &x, A&...a){read(x); read(a...);}
    
    inline int exgcd(int a, int b, int &x, int &y) {
        if(b==0) {x=1, y=0; return a;}
        int d=exgcd(b,a%b,y,x);
        y-=a/b*x;
        return d;
    }
    
    
    int main() {
        int p,e,i,d;
        read(p); read(e); read(i); read(d);
        int kase=0;
        while(~p) {
            const int M[]={924, 759, 644};
            const int M_[]={6, -9, 2};
            int k = M[0]*M_[0]*p + M[1]*M_[1]*e + M[2]*M_[2]*i;
            int x = (k-d)%(23*28*33);
            if(x<=0) x+=23*28*33;
            printf("Case %d: the next triple peak occurs in %d days.
    ", ++kase, x);
            read(p); read(e); read(i); read(d);
        }
        return 0;
    }
    

    AC代码

  • 相关阅读:
    【leetcode】字符串轮转
    【leetcode】URL化
    【leetcode】学生出勤记录 I
    【leetcode】子域名访问计数
    【leetcode】整数的各位积和之差
    【leetcode】另一个树的子树
    【leetcode】查询后的偶数和
    【leetcode】左叶子之和
    【leetcode】从根到叶的二进制数之和
    【leetcode】平方数之和
  • 原文地址:https://www.cnblogs.com/sahdsg/p/10815629.html
Copyright © 2020-2023  润新知