Biorhythms
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 139500   Accepted: 44772

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. 题解:中国剩余定理 参考
设m1,m2,m3,m4两两互素,则同余方程组
x≡a1(m1)
x≡a2(m2)
x≡a3(m3)
x≡a4(m4)
....
x≡ak(mk)
一定有解,x≡(a1*M1*M1^(-1)+a2*M2*M2^(-1)+....)
其中M=m1*m2*...*mk,Mi=M/mi,Mi^(-1)是Mi在模mi意义下的逆元。
普通的中国剩余定理要求所有mi互素,那么如果不互素呢?
我们采用两两合并的思想,假设要合并如下两个方程
x=a1+m1*x1
x=a2+m2*x2
那么得到
a1+m1x1=a2+m2x2 => m1x1+m2x2=a2-a1
再利用扩展欧几里得算法解出x1的最小正整数解,再带入
x=a1+m1x1,得到x后合并为一个方程的结果过为
y≡x(mod lcm(m1,m2))
这样一直合并下去,最终可以求得同余方程的解。


#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std; int a[],m[];
int p,e,i,d,t=; void exgcd(int a,int b,int &x,int &y){
if(b==){
x=;
y=;
return;
}
exgcd(b,a%b,x,y);
int tmp=x;
x=y;
y=tmp-(a/b)*y;
} int CRT(int a[],int m[],int n){
int M=,ans=;
for(int i=;i<=n;i++)M*=m[i];
for(int i=;i<=n;i++){
int x,y;
int Mi=M/m[i];
exgcd(Mi,m[i],x,y);
ans=(ans+Mi*x*a[i])%M;
}
if(ans<)ans+=M;
return ans;
} int main(){
while(cin>>p>>e>>i>>d){
if(p==-&&e==-&&i==-&&d==-)break;
a[]=p;a[]=e;a[]=i;
m[]=;m[]=;m[]=;
int ans=CRT(a,m,);
if(ans<=d)ans+=;
printf("Case %d: the next triple peak occurs in %d days.\n",t++,ans-d);
}
return ;
}

不互素的

 
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define N 5
#define ll long long
using namespace std;
ll n,m[N],a[N],m1,e;
ll read()
{
ll x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==)
{
x=,y=;
return a;
}
ll r=exgcd(b,a%b,x,y),tmp;
tmp=x,x=y,y=tmp-a/b*y;
return r;
}
ll crt()
{
ll a1=a[],a2,m2,d,c;m1=m[];
for(ll i=;i<=n;++i)
{
a2=a[i],m2=m[i];
c=a2-a1;ll x=,y=;
d=exgcd(m1,m2,x,y);
if(c%d) return -;
x=x*c/d;
int mod=m2/d;
x=(mod+x%mod)%mod;
a1+=m1*x;m1*=mod;
}
return a1;
}
int main()
{
// freopen("mod.in","r",stdin);
// freopen("mod.out","w",stdout);
n=;
for(int i=;i<=n;i++)
m[i]=read(),a[i]=read();
printf("%lld\n",crt());
return ;
}

												

【学习笔记-中国剩余定理】POJ1006 Biorhythms的更多相关文章

  1. 学习笔记 - 中国剩余定理&扩展中国剩余定理

    中国剩余定理&扩展中国剩余定理 NOIP考完回机房填坑 ◌ 中国剩余定理 处理一类相较扩展中国剩余定理更特殊的问题: 在这里要求 对于任意i,j(i≠j),gcd(mi,mj)=1 (就是互素 ...

  2. Hbase 学习笔记4----原理

    MapReduce 中如何处理HBase中的数据?如何读取HBase数据给Map?如何将结果存储到HBase中? Mapper类:包括一个内部类(Context)和四个方法(setup,map,cle ...

  3. 五毛的cocos2d-x学习笔记06-处理用户交互

    前几篇感觉自己在写教育文章,╮(╯▽╰)╭.今天换成开发者的口吻,毕竟我也是在边学边写博客. 处理用户交互包括:单点触摸.多点触摸.事件传递.传感器.物理按键等部分. 单点触摸: 触摸事件传递顺序 o ...

  4. Three.js学习笔记04--纹理

    1 纹理由图片组成  3D世界的纹理由图片组成. 将纹理以一定的规则映射到几何体上,一般是三角形上,那么这个几何体就有纹理皮肤了. 首先应该有一个纹理类,其次是有一个加载图片的方法,将这张图片和这个纹 ...

  5. Git学习笔记04-管理修改

    Git跟踪并管理的是修改,而非文件.新增文件,修改一行,删除一点,都算是修改. 在.git工作区新增一个文件,test.txt,输入test git ...然后git add ​ ​ add之后修改t ...

  6. Git学习笔记03-原理

    在Git中,算上远程Git仓库有四个工作区域 Git本地有三个区域(工作区域.暂存区,资源区,远程Git仓库) 工作区域:就是你本机写好的代码,你可以看到的 暂存区:你写好的代码上传后被git管理的内 ...

  7. Biorhythms(poj1006+中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 117973   Accepted: 37026 Des ...

  8. 扩展中国剩余定理(EXCRT)学习笔记

    扩展中国剩余定理(EXCRT)学习笔记 用途 求解同余方程组 \(\begin{cases}x\equiv c_{1}\left( mod\ m_{1}\right) \\ x\equiv c_{2} ...

  9. POJ1006 - Biorhythms(中国剩余定理)

    题目大意 略...有中文... 题解 就是解同余方程组 x≡(p-d)(mod 23) x≡(e-d)(mod 28) x≡(i-d)(mod 33) 最简单的中国剩余定理应用.... 代码: #in ...

随机推荐

  1. poj1061(extendgcd)

    看完题目后,题目要求: 设时间为t (x+mt)%L = (y+nt)%L ( x-y + (m-n)*t )= k*L (k是整数,可为负) 然后就是经典的 xa+yb=c 求解x,y的经典题目了. ...

  2. 【BZOJ1513】[POI2006]Tet-Tetris 3D 二维线段树

    [BZOJ1513][POI2006]Tet-Tetris 3D Description Task: Tetris 3D "Tetris" 游戏的作者决定做一个新的游戏, 一个三维 ...

  3. Android5.0以后版本把应用移动到SD或者TF卡的方法

    由于手机内存较小,才8G,用的时间一久,内部存储就满了,天天删垃圾,WIFI还老断线,终于忍无可忍了,决定把应用移动到SD卡,实践下来,只有少部分App默认支持移动到SD卡,大部分程序不支持只能装在内 ...

  4. 内存使用分析工具Valgrind简单用法

    转载自 http://www.cnblogs.com/sunyubo/archive/2010/05/05/2282170.html 暂时还未使用过,记录下,记录下,记录下 Valgrind的主要作者 ...

  5. PAT 1062. 最简分数(20)

    一个分数一般写成两个整数相除的形式:N/M,其中M不为0.最简分数是指分子和分母没有公约数的分数表示形式. 现给定两个不相等的正分数 N1/M1 和 N2/M2,要求你按从小到大的顺序列出它们之间分母 ...

  6. Computer Vision: Algorithms and ApplicationsのImage processing

    实在是太喜欢Richard Szeliski的这本书了.每一章节(after chapter3)都详述了该研究方向比較新的成果.还有很多很多的reference,假设你感兴趣.全然能够看那些參考论文 ...

  7. 【C语言】Linux C调用系统命令

    最近研究深度学习,做视频分析和检测,用到C语言,以前都是写python的,不过没关系,计算机语言都是相通的,差不多原理是一样的,只是语法不太一样. 下面介绍linux C语言种调用本地命令,访问一个地 ...

  8. [原创]java WEB学习笔记14:JSP的9 个隐含对象 及 JSP 的基本语法

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  9. nginx配置大全

    nginx配置大全

  10. 剪辑的楼天城的ACM之路

    楼天城楼教主的acm心路历程(剪辑) 利用假期空闲之时,将这几年GCJ,ACM,TopCoder 参加的一些重要比赛作个回顾.昨天是GCJ2006 的回忆,今天时间上更早一些吧,我现在还清晰记得3 年 ...