B - Biorhythms

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

Description

人生来就有三个生理周期,分别为体力、感情和智力周期,它们的周期长度为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,智力周期的高峰,人会思维敏捷,精力容易高度集中。因为三个周期的周长不同,所以通常三个周期的高峰不会落在同一天。对于每个人,我们想知道何时三个高峰落在同一天。对于每个周期,我们会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。你的任务是给定一个从当年第一天开始数的天数,输出从给定时间开始(不包括给定时间)下一次三个高峰落在同一天的时间(距给定时间的天数)。例如:给定时间为10,下次出现三个高峰同天的时间是12,则输出2(注意这里不是3)。

Input

输入四个整数:p, e, i和d。 p, e, i分别表示体力、情感和智力高峰出现的时间(时间从当年的第一天开始计算)。d 是给定的时间,可能小于p, e, 或 i。 所有给定时间是非负的并且小于365, 所求的时间小于21252。

当p = e = i = d = -1时,输入数据结束。

Output

从给定时间起,下一次三个高峰同天的时间(距离给定时间的天数)。

采用以下格式:  
Case 1: the next triple peak occurs in 1234 days.

注意:即使结果是1天,也使用复数形式“days”。

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.

Hint

Translator

北京大学程序设计实习2007, Xie Di
题意如题。
题解:现设 num 是下一个相同日子距离开始的天数 p,e,i,d 如题中所设!那么就可以得到三个式子:
   ( num + d ) % 23 == p;
   ( num + d ) % 28 == e;
   ( num + d ) % 33 == i;
   这是典型的中国剩余定理问题:传送门
以上是互质的中国剩余定理,比较好理解,还有非互质的中国剩余定理,存在无解情况:传送门
给出两种代码,第一种包含了运算过程,适用于其他数据,第二种是该题的成品。
代码一:
#include <iostream>
#include <cstdio>
using namespace std;
#define mod 21252
int exgcd(int a,int b,int &x,int &y) //计算扩展欧几里得
{
if(b==) {x=;y=;return a;}
int d=exgcd(b,a%b,y,x); y-=a/b*x;
return d;
}
int inv(int a,int n) //计算逆元
{
int d,x,y;
d=exgcd(a,n,x,y);
if(d==) return (x%n+n)%n;
else return -;
}
int datain[]={,,};
int dataout[];
void cal() //计算中国剩余定理
{
int ans=;
for(int i=;i<;i++)
ans*=datain[i];
for(int i=;i<;i++)
{
ans/=datain[i];
dataout[i]=inv(ans,datain[i])*ans; //记得结果算出来
ans*=datain[i]; //记得乘回来
}
}
int main()
{
cal();
//for(int i=0;i<3;i++)
// cout<<dataout[i]<<endl;
int p,e,i,d,cas=,ans;
while(cin>>p>>e>>i>>d)
{
if(p==-&&e==-&&i==-&&d==-)
break;
ans=(dataout[]*p+dataout[]*e+dataout[]*i-d+mod)%mod;
if(!ans) ans=mod;
printf("Case %d: the next triple peak occurs in %d days.\n",cas++,ans);
}
return ;
}

代码二:

#include <iostream>
#include <cstdio>
using namespace std;
#define mod 21252
int main()
{
int p,e,i,d,cas=,ans;
while(cin>>p>>e>>i>>d)
{
if(p==-&&e==-&&i==-&&d==-)
break;
ans=(*p+*e+*i-d+mod)%mod;
if(!ans) ans=mod;
printf("Case %d: the next triple peak occurs in %d days.\n",cas++,ans);
}
return ;
}

POJ 1006 - Biorhythms (中国剩余定理)的更多相关文章

  1. POJ 1006 Biorhythms(中国剩余定理)

    题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...

  2. POJ 1006 Biorhythms --中国剩余定理(互质的)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 103539   Accepted: 32012 Des ...

  3. PKU POJ 1006 Biorhythms (中国剩余定理)

    中国剩余定理 x = ai (mod mi)  ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk     其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st   M ...

  4. POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)

    POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...

  5. Biorhythms(中国剩余定理)

    http://shuxueshi.jie.blog.163.com/blog/static/13611628820104179856631/ 这篇博客写的很棒! #include<stdio.h ...

  6. poj 1006 Biorhythms (中国剩余定理模板)

    http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...

  7. poj 1006:Biorhythms(水题,经典题,中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Des ...

  8. POJ 1006 Biorhythms (中国剩余定理)

    在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...

  9. 中国剩余定理 POJ 1006 Biorhythms

    题目传送门 题意:POJ有中文题面 分析:其实就是求一次同余方程组:(n+d)=p(%23), (n+d)=e(%28), (n+d)=i(%33),套用中国剩余定理模板 代码: /********* ...

随机推荐

  1. BZOJ-1880 Elaxia的路线 SPFA+枚举

    1880: [Sdoi2009]Elaxia的路线 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 921 Solved: 354 [Submit][Sta ...

  2. Bzoj 1336&1337 Alien最小圆覆盖

    1336: [Balkan2002]Alien最小圆覆盖 Time Limit: 1 Sec  Memory Limit: 162 MBSec  Special Judge Submit: 1473  ...

  3. LFI、RFI、PHP封装协议安全问题学习

    本文希望分享一些本地文件包含.远程文件包含.PHP的封装协议(伪协议)中可能包含的漏洞 相关学习资料 http://www.ibm.com/developerworks/cn/java/j-lo-lo ...

  4. C#通过编程方式实现Ping

    代码是照着书敲的,贴出来方便平时参考 using System; using System.Collections.Generic; using System.Linq; using System.T ...

  5. P、NP、NPC、NP-Hard问题

    转自:http://www.matrix67.com/blog/archives/105 总结 P:能用多项式时间求解的问题NP:能用多项式时间验证的问题(但不知道能不能用多项式时间求解).存在不属于 ...

  6. php多态设计

    原文:http://www.cnblogs.com/tecs27/archive/2012/03/13/2394028.html 多态性是指相同的操作或函数.过程可作用于多种类型的对象上并获得不同的结 ...

  7. Mvc3提交表格验证(转载)

    Model层:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System ...

  8. 字符串模拟赛T3

    只看我的做法就够了 #include<iostream> #include<cstdio> #include<string> #include<cstring ...

  9. Robot Motion(imitate)

    Robot Motion Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11065   Accepted: 5378 Des ...

  10. 还是畅通工程(MST)

    还是畅通工程 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...