zoukankan      html  css  js  c++  java
  • Biorhythms 1006 PKU

    Description:

    http://acm.pku.edu.cn/JudgeOnline/problem?id=1006

     人有三个周期:physical、emotional、intellectual其周期分别为23天、28天、33天,当天数为其对应的倍数时,说明其到达了巅峰,例如第46天,physical到达了巅峰,而其他两个没有

    现给出4个数p、e、i、d,前三个分别为第几天时physical、emotional、intellectual到达巅峰,给出一个天数第d天,请问最少再过几天三个能够到达巅峰

    Solution:

    假设再过ans天,则其满足

    ans+d  ≡ p (mod 23)

    ans + d ≡ e (mod 28)

    ans + d ≡ i (mod 33)

    中国剩余定理--孙子定理
    此定理的一般形式是设m1 ,… ,mk 为两两互素的正整数(必须两两互素,否则后面会出错),m=m1*…*mk ,m=miMi,i=1,2,… ,k 。

    同余式组x≡b1(modm1),…,x≡bk(mod mk)的解为x≡c1M1b1+…+ckMkbk (mod m)式中ciMi≡1 (mod mi),i=1,2,…,k 。

    当m1 ,… ,mk 为两两互素的正整数时,如何求解,这时可能误解,例如

    x % 6 = 3

    x % 8 = 4

    1 #include <stdio.h>
    2  #define MAXSIZE 4
    3
    4  /* find ans s.t. (ans * y) % x = r*/
    5  int remaind(int x, int y, int r)
    6 {
    7 int i=0,temp=y%x;
    8 for (i=x-1; i; i--)
    9 if ( (i*temp)%x == r)
    10 break;
    11
    12 return i;
    13 }
    14
    15  int main ()
    16 {
    17 int cases=1,ans=0,d=0,totalProduct,i=0;
    18 const int len=3;
    19 int in[MAXSIZE];
    20 int m[MAXSIZE];
    21 int circle[MAXSIZE]={23,28,33};
    22
    23 totalProduct = 1;
    24 for (i=0; i<len; i++)
    25 totalProduct *= circle[i];
    26 for (i=0; i<len; i++)
    27 m[i] = totalProduct / circle[i];
    28
    29 while (scanf ("%d %d %d %d",&in[0],&in[1],&in[2],&d))
    30 {
    31 if (in[0]==-1)
    32 break;
    33
    34 for (i=0; i<len; i++)
    35 in[i] %= circle[i];
    36
    37 ans = 0;
    38 for (i=0; i<len; i++)
    39 ans += remaind(circle[i],m[i],in[i])*m[i];
    40
    41 ans = ans + totalProduct - d % totalProduct;
    42 ans %= totalProduct;
    43
    44 if (ans == 0)
    45 ans = totalProduct;
    46
    47 printf ("Case %d: the next triple peak occurs in %d days.\n",
    48 cases++,ans);
    49
    50 }
    51 }
  • 相关阅读:
    Spring发展历程总结
    杂说
    说说Java生态圈的那些事儿
    你知道什么是Grunt么?
    jquery常见知识点 总结
    优化JavaScripe 提升首页加载速度的几种方案解析
    final static 深度解析
    JS的预编译和执行顺序 详析(及全局与局部变量)
    swipe.js 2.0 轻量级框架实现mobile web 左右滑动
    JS中跨域和沙箱的解析
  • 原文地址:https://www.cnblogs.com/eavn/p/1752840.html
Copyright © 2011-2022 走看看