题目链接: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都必定能被整除。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e5+; LL exgcd(LL a, LL b, LL &x, LL &y)
{
if(a== &&b==) return -;
if(b==) {x=; y=; return a;}
LL d = exgcd(b,a%b,y,x);
y -= a/b*x;
return d;
} LL china(int n, LL *a, LL *m)
{
LL M = , ret = ;
for(int i = ; i < n; i ++) M *= m[i];
for(int i = ; i < n; i ++)
{
LL w = M/m[i], x, y;
exgcd(m[i], w, x, y);
ret = (ret+y*w*a[i])%M;
}
return (ret+M)%M;
} LL p[] = {,,}, r[], d;
int main()
{
int kase = ;
while(scanf("%I64d%I64d%I64d%I64d",&r[],&r[],&r[],&d) && (~r[]||~r[]||~r[]||~d))
{
LL ans = ((china(, r, p)-d)%+)%;
printf("Case %d: the next triple peak occurs in %I64d days.\n", ++kase, ans?ans:);
}
}

POJ1006 Biorhythms —— 中国剩余定理的更多相关文章

  1. POJ1006——Biorhythms(中国剩余定理)

    Biorhythms Description人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色. ...

  2. Biorhythms(中国剩余定理)

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

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

    B - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

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

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

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

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

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

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

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

    Biorhythms 题意:读入p,e,i,d 4个整数,已知(n+d)%23=p;   (n+d)%28=e;   (n+d)%33=i ,求n .        (题在文末) 知识点:中国剩余定理 ...

  8. poj1006 中国剩余定理&&中国剩余定理解析

    poj 1006 题的思路不是很难的,可以转化数学式: 现设 num 是下一个相同日子距离开始的天数 p,e,i,d 如题中所设! 那么就可以得到三个式子:( num + d ) % 23 == p: ...

  9. 【中国剩余定理】 poj 1006

    生理周期  简单模拟 对于超出23 * 28 * 33(21252)时进行求余运算即可. #include<stdio.h> int main() { //freopen("in ...

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

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

随机推荐

  1. Arduino可穿戴教程ArduinoIDE新建编辑源文件

    Arduino可穿戴教程ArduinoIDE新建编辑源文件 Arduino IDE新建源文件 Arduino IDE启动后默认就新建了一个源文件,如图2.20所示.新建的源文件名称是以sketch_开 ...

  2. CDOJ 3 BiliBili, ACFun… And More! 模拟

    原题链接:http://acm.uestc.edu.cn/#/problem/show/3 题意: 有个人在看B站视频时有个习惯,就是每当卡住的时候,他总再次从头开始看.另外,他在看视频时会先等待T的 ...

  3. WEBLOGIC启动后,重启后控制台进入缓慢、延迟,探查WEBLOGIC

    本文说的是解决过程,可直接点击本行略过探查内容,跳到最后的解决办法!! 现象: 1.WEBLOGIC安装在 CENTOSopenSUSE 等LINUX或者UNIX机器上,无论是虚拟机或者PC或者服务器 ...

  4. win10安装 pandas

    pip install -i https://pypi.douban.com/simple pandas

  5. Android Studio调试工具总结

       前言:写代码不可避免有Bug.通常情况下除了日志最直接的调试手段就是debug.当我们的程序出现bug时,调试能够高速的找到bug. 进入调试状态.我们能够清晰的了解程序的整个运行过程,能够对内 ...

  6. 自己写的通过ADO操作mysql数据库

    #include <iostream> #include <windows.h> #include <atlstr.h> #import "c:\Prog ...

  7. java.lang.String中的trim()方法的详细说明(转)

    String.Trim()方法到底为我们做了什么,仅仅是去除字符串两端的空格吗? 一直以为Trim()方法就是把字符串两端的空格字符给删去,其实我错了,而且错的比较离谱. 首先我直接反编译String ...

  8. python(10)- 字符编码

    一 什么是编码? 基本概念很简单.首先,我们从一段信息即消息说起,消息以人类可以理解.易懂的表示存在.我打算将这种表示称为“明文”(plain text).对于说英语的人,纸张上打印的或屏幕上显示的英 ...

  9. 如何成为一个Linux内核开发者

    你想知道如何成为一个Linux内核开发者么?或者你的老板告诉你,“去为这个设备写一个Linux驱动.“这篇文档的目的,就是通过描述你需要 经历的过程和提示你如何和社区一起工作,来教给你为达到这些目的所 ...

  10. datatables参数配置详解

    //@translator codepiano //@blog codepiano //@email codepiano.li@gmail.com //尝试着翻译了一下,难免有错误的地方,欢迎发邮件告 ...