zoukankan      html  css  js  c++  java
  • POJ1006 Biorhythms —— 中国剩余定理

    题目链接:https://vjudge.net/problem/POJ-1006

    Biorhythms
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 141576   Accepted: 45491

    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.

    Source

    题解:

    中国剩余定理的模板题。

    中国剩余定理:

    假设有:

      s ≡ a1  (mod%m1)

      s ≡ a2  (mod%m2)

      ……

      s ≡ an (mod%mn)

    且m1、m2……mn两两互斥,求最小的s。

    解法:

    1.设M是m1、m2……mn的最小公倍数,由于m之间两两互斥,所以:M = ∏mi 。

    2. 设 wi = M/mi,则可知 gcd(mi, wi) = 1,因此必定有:x*mi + y*wi = gcd(mi, wi) ,即:x*mi + y*wi = 1 。

    3. 对 x*mi + y*wi = 1 两边模mi, 则有:(y*wi)%mi = 1,两边乘以ai,则:(ai*y*wi)%mi = ai (前提是ai<mi)。

    4. s = ∑ ai*y*wi ,最小的s:s = ( ∑ ai*y*wi)%M 。对于ai*y*wi,它模mi的时候等于ai,而模mj(i!=j)时等于0,因为wi是所有mj的倍数。对于每个ai*y*wi也如此,因此 ∑ ai*y*wi。那为什么最小的s为什么是%M?因为M是所有mi的倍数,每个M都必定能被整除。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <vector>
     6 #include <cmath>
     7 #include <queue>
     8 #include <stack>
     9 #include <map>
    10 #include <string>
    11 #include <set>
    12 using namespace std;
    13 typedef long long LL;
    14 const int INF = 2e9;
    15 const LL LNF = 9e18;
    16 const int MOD = 1e9+7;
    17 const int MAXN = 1e5+100;
    18 
    19 LL exgcd(LL a, LL b, LL &x, LL &y)
    20 {
    21     if(a==0 &&b==0) return -1;
    22     if(b==0) {x=1; y=0; return a;}
    23     LL d = exgcd(b,a%b,y,x);
    24     y -= a/b*x;
    25     return d;
    26 }
    27 
    28 LL china(int n, LL *a, LL *m)
    29 {
    30     LL M = 1, ret = 0;
    31     for(int i = 0; i < n; i ++) M *= m[i];
    32     for(int i = 0; i < n; i ++)
    33     {
    34         LL w = M/m[i], x, y;
    35         exgcd(m[i], w, x, y);
    36         ret = (ret+y*w*a[i])%M;
    37     }
    38     return (ret+M)%M;
    39 }
    40 
    41 LL p[3] = {23,28,33}, r[3], d;
    42 int main()
    43 {
    44     int kase = 0;
    45     while(scanf("%I64d%I64d%I64d%I64d",&r[0],&r[1],&r[2],&d) && (~r[0]||~r[1]||~r[2]||~d))
    46     {
    47         LL ans = ((china(3, r, p)-d)%21252+21252)%21252;
    48         printf("Case %d: the next triple peak occurs in %I64d days.
    ", ++kase, ans?ans:21252);
    49     }
    50 }
    View Code
  • 相关阅读:
    java设计模式简介
    java设计模式--单例模式
    判断整形回文数
    常用正则表达式 捕获组(分组)
    [转]十分钟搞定Vue搭建
    ActiveX界面已显示,调用方法报undefined的处理办法
    [转]纯js导出json到excel(支持chrome)
    webapi 开启gzip压缩
    webapi下载文件
    iis添加共享目录为虚拟目录
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/8392390.html
Copyright © 2011-2022 走看看