POJ 1006 Biorhythms 解题报告

Biorhythms

Time Limit: 1000MS
Memory Limit: 10000K

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.

想说的话

大家看到这种英文题面是不是就崩溃了啊
推荐一下windows10 Edge Translator软件
马上自动翻译成下面的状态
这里写图片描述

题解

在讲题解之前
我想先分享一下一道题
古代大将韩信点兵的故事
传说西汉大将韩信,由于比较年轻,开始他的部下对他不很佩服。有一次阅兵时,韩信要求士兵分三路纵队,结果末尾多2人,改成五路纵队,结果末尾多3人,再改成七路纵队,结果又余下2人,后来下级军官向他报告共有士兵2395人,韩信立即笑笑说不对(因2395除以3余数是1,不是2),由于已经知道士兵总人数在2300~2400之间,所以韩信根据23,128,233,——,每相邻两数的间隔是105(3、5、7的最小公倍数),便立即说出实际人数应是2333人(因2333=128+20χ105+105,它除以3余2,除以5余3,除以7余2)。这样使下级军官十分敬佩,这就是韩信点兵的故事。

韩信点兵问题简化:已知 n%3=2, n%5=3, n%7=2, 求n。

韩信用的就是“中国剩余定理”,《孙子算经》中早有计算方法,大家可以查阅相关资料。
“韩信点兵”问题计算如下:
因为n%3=2, n%5=3, n%7=2 且 3,5,7互质 (互质可以直接得到这三个数的最小公倍数)
令x= n%3=2 , y= n%5=3 ,z= n%7=2
使5×7×a被3除余1,有35×2=70,即a=2;
使3×7×b被5除余1,用21×1=21,即b=1;
使3×5×c被7除余1,用15×1=15,即c=1。
那么n =(70×x+21×y+15×z)%lcm(3,5,7) = 23 这是n的最小解
而韩信已知士兵人数在2300~2400之间,所以只需要n+i×lcm(3,5,7)就得到了2333,此时i=22
以上说的就是我们的中国剩余定理
中国剩余定理是一个非常玄妙的东西
先给你们看一下百度百科2333
换句话说我们把这种东西转化成同余方程组
然后通过解方程组解出来
但是一般别人的代码都比较长
我用的不是常规思路解方程组
我们这里有两种情况
同余方程所有的 mod互质的时候是比较简单的
已知(n+d)%23=p; (n+d)%28=e; (n+d)%33=i
使33×28×a被23除余1,用33×28×8=5544;
使23×33×b被28除余1,用23×33×19=14421;
使23×28×c被33除余1,用23×28×2=1288。
因此有(5544×p+14421×e+1288×i)% lcm(23,28,33) =n+d
又23、28、33互质,即lcm(23,28,33)= 21252;
所以有n=(5544×p+14421×e+1288×i-d)%21252
本题所求的是最小整数解,避免n为负,因此最后结果为n= [n+21252]% 21252
那么最终求解n的表达式就是:
n=(5544*p+14421*e+1288*i-d+21252)%21252;
当然了我旁边的春犇用的就是中规中矩的解法 一会会附上代码 大家自己看就好
第二种情况就是不互质 这个时候我们就需要用到exgcd 比较麻烦
这个时候就不能用中国剩余定理!!!
这个时候就不能用中国剩余定理!!!
这个时候就不能用中国剩余定理!!!
说了三遍 !!!

我的代码

#include<cstdio>
int main()
{
  int a,b,c,d,cnt=0;
  while( scanf("%d%d%d%d",&a,&b,&c,&d) )
  {
    if(a==-1&&b==-1&&c==-1&&d==-1) return 0;
    int n=(a*5544+b*14421+c*1288-d+21252)%21252;
    if(n==0) n=21252;
    cnt++;
    printf("Case %d: the next triple peak occurs in %d days.\n",cnt,n);
  }
}

中规中矩的春犇的代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
const ll m[3]={23,28,33};
ll r[3];
ll M=23*28*33;
inline void read(ll &x)
{
  ll s=0,w=1;
  char c=getchar();
  while(c<'0'||c>'9')
  {
    if(c=='-')w=-1;
    c=getchar();
  }
  while(c>='0'&&c<='9')
  {
    s=s*10+c-'0';
    c=getchar();
  }
  x=s*w;
}
ll p,e,i,d;
ll quick_pow(ll a,ll b,ll mod)
{
  if(b==0)return 1;
  ll temp=quick_pow(a,b>>1,mod);
  temp=(temp*temp)%mod;
  if(b&1)temp=(temp*a)%mod;
  return temp;
}
inline ll CRT(ll a,ll b,ll c,ll d)
{
  ll ans=0;
  r[0]=a%m[0];
  r[1]=b%m[1];
  r[2]=c%m[2];
  for(int i=0;i<3;i++)
  {
    ll temp=M/m[i],j;
    for(j=1;j<m[i];j++)if((temp*j)%m[i]==1)break;
    ans+=r[i]*temp*j;
    ans%=M;
  }
  ans=((ans-d)%M+M)%M;
  return ans?ans:ans+M;
}
ll cnt;
int main()
{
  while(scanf("%lld%lld%lld%lld",&p,&e,&i,&d)&&p!=-1&&e!=-1&&i!=-1&&d!=-1)
  {
    cnt++;
    printf("Case %lld: the next triple peak occurs in %lld days.\n",cnt,CRT(p,e,i,d));
  }
}

写在最后

中国剩余定理是个好东西
如果能找到的话
有一道题叫做曹冲养猪
那题用暴力就能打 但是中国剩余定理更有用
大家可以试试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值