zoukankan      html  css  js  c++  java
  • poj1006 Biorhythms(CRT)

    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.

    分析:
    CRT
    题目大意:
    人自出生起就有体力,情感和智力三个生理周期,分别为23,28和33天。
    一个周期内有一天为峰值,在这一天,人在对应的方面(体力,情感或智力)表现最好。
    通常这三个周期的峰值不会是同一天。
    现在给出三个日期,分别对应于体力,情感,智力出现峰值的日期。
    然后再给出一个起始日期,要求从这一天开始,算出最少再过多少天后三个峰值同时出现。

    p,e,i分别表示三个周期上一次到达峰值的日期
    我也不写同余方程了,直观的:
    设x是三个峰值重合的日期
    x-k1*28=p
    x-k2*23=e
    x-k3*33=i
    28,23,33互质,那么我就可以用CRT
    计算在% 23*33*28意义下的最小x

    具体的流程我们再来说一遍
    M=28*23*33
    Mi=M/mi
    求解:Mi*ki≡1 (mod mi)
    即:Mi*ki+mi*y=1 (mod mi)
    exgcd(Mi,mi);
    ans=ΣMi*ki*ai (mod M)

    实际上因为数字很固定
    我们可以直接写出来ki
    28*33*a%23==1 a=6
    23*33*b%28==1 b=19
    23*28*c%33==1 c=2

    tip

    M!=21252
    在计算答案的时候,时刻%21252

    注意输出格式

    不知道为什么,一直WA
    和AC代码对拍
    这里写图片描述
    lajipoj

    这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    
    using namespace std;
    
    const int M=23*28*33;
    const int mod=21252;
    int x,y,p,q,e,d,ans;
    
    void exgcd(int a,int b)
    {
        if (b==0)
        {
            x=1;y=0;
            return;
        }
        else
        {
            exgcd(b,a%b);
            int t=y;
            y=x-(a/b)*y;
            x=t;
        }
    }
    
    int main()
    {
        int T=1;
        scanf("%d%d%d%d",&p,&e,&q,&d);
        while (p!=-1&&q!=-1&&e!=-1&&d!=-1)
        {
            ans=0;
            int Mi,tt;
            Mi=M/23; exgcd(Mi,23); ans=(ans%mod+(Mi%mod*x%mod*p%mod+mod)%mod)%mod;
            Mi=M/28; exgcd(Mi,28); ans=(ans%mod+(Mi%mod*x%mod*e%mod+mod)%mod)%mod;
            Mi=M/33; exgcd(Mi,33); ans=(ans%mod+(Mi%mod*x%mod*q%mod+mod)%mod)%mod;
            while (ans-d<=0) ans+=mod;
            ans-=d; ans%=mod;
            printf("Case %d: the next triple peak occurs in %d days.
    ",T,ans);
            scanf("%d%d%d%d",&p,&e,&q,&d);
            T++;
        }
        return 0;
    }

    附网上的AC代码

    #include <cstdio>
    int main()
    {
        int p,e,i,n,sum,t;
        t=0;
        while(scanf("%d%d%d%d",&p,&e,&i,&n),p+e+i+n!=-4)
        {
            sum=(5544*p+14421*e+1288*i)%21252;
            if(sum-n<=0)
            sum+=21252;
            printf("Case %d: the next triple peak occurs in %d days.
    ",++t,sum-n);
    
         }return 0;
    }
  • 相关阅读:
    C语言中const关键字的用法
    LDO和DC-DC的概念,区别及优缺点
    Ubuntu下几个命令行方式使用的图片浏览工具
    I2C和I2S的区别和使用方法
    scikit-image 图像处理库介绍
    USB协议介绍
    Ubuntu 16.04 python和OpenCV安装
    一种基于python的人脸识别开源系统
    numpy 介绍
    python enumerate用法总结
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673222.html
Copyright © 2011-2022 走看看