POJ1006 Biorhythms —— 中国剩余定理
题目链接:https://vjudge.net/problem/POJ-1006
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 141576 | Accepted: 45491 |
Description
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
Output
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 —— 中国剩余定理的更多相关文章
- POJ1006——Biorhythms(中国剩余定理)
Biorhythms Description人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色. ...
- Biorhythms(中国剩余定理)
http://shuxueshi.jie.blog.163.com/blog/static/13611628820104179856631/ 这篇博客写的很棒! #include<stdio.h ...
- POJ 1006 - Biorhythms (中国剩余定理)
B - Biorhythms Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Subm ...
- POJ 1006 Biorhythms --中国剩余定理(互质的)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 103539 Accepted: 32012 Des ...
- POJ 1006 Biorhythms(中国剩余定理)
题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...
- PKU POJ 1006 Biorhythms (中国剩余定理)
中国剩余定理 x = ai (mod mi) ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk 其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st M ...
- poj1006 / hdu1370 Biorhythms (中国剩余定理)
Biorhythms 题意:读入p,e,i,d 4个整数,已知(n+d)%23=p; (n+d)%28=e; (n+d)%33=i ,求n . (题在文末) 知识点:中国剩余定理 ...
- poj1006 中国剩余定理&&中国剩余定理解析
poj 1006 题的思路不是很难的,可以转化数学式: 现设 num 是下一个相同日子距离开始的天数 p,e,i,d 如题中所设! 那么就可以得到三个式子:( num + d ) % 23 == p: ...
- 【中国剩余定理】 poj 1006
生理周期 简单模拟 对于超出23 * 28 * 33(21252)时进行求余运算即可. #include<stdio.h> int main() { //freopen("in ...
- Biorhythms(poj1006+中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 117973 Accepted: 37026 Des ...
随机推荐
- linux tomcat shutdown.sh 有时不能结束进程,使用如下指令进度重启
ps -ef | grep tomcat | grep -v grep | cut -c 9-15 | xargs kill -9 & ./startup.sh
- vue常用指命
1.v-text:用于更新标签包含的文本,作用和{{}}的效果一样. 2.v-html:绑定一些包含html代码的数据在视图上. 3.v-show:用来控制元素的display属性,和显示隐藏有关.v ...
- 在Dev GridControl中添加颜色可变的ProgressBar z
在使用DevExpress,GridControl自带的ProgressBarControl的时候 由于无法通过BackColor/ForeColor来改变进度条的颜色所以很多特效是实现不了的.如下面 ...
- 转:如何mac下使用wireshark
Mac OS Mountain Lion默认是没有安装X11的,而wireshark运行需要x11,因此如果直接安装wireshark而没有安装x11,wireshark不会正常运行. 去苹果主页下载 ...
- 别样JAVA学习(五)继承上(1.1)Object类toString()
接下来说完equals以后,我们学习接下来的toString(), Java又觉得全部对象不光具有比較性, 还能使对象变成字符串被打印. 出现 曾经前面显示的是数组.如今显示的是这个对象所属的类. 紧 ...
- iOS学习笔记12-网络(一)NSURLConnection
一.网络请求 在网络开发中.须要了解一些经常使用的请求方法: GET请求:get是获取数据的意思,数据以明文在URL中传递,受限于URL长度,所以数据传输量比較小. POST请求:post是向serv ...
- background-attachment
CreateTime--2017年9月28日10:58:58 Author:Marydon background-attachment 1.定义 定义背景图片随滚动轴的移动方式(设置背景图像是否固 ...
- mysql复制表命令
http://hi.baidu.com/dwspider/item/908bf5e1746275bd2e140b03 上面命令是实现复制表的一种方法,缺陷就是索引等表信息不会复制过去,只是复制 ...
- 8款精美的HTML5图片动画分享
From:http://geek.csdn.net/news/detail/196250 HTML5结合jQuery可以让网页图片变得更加绚丽多彩,比如实现一些图片3D切换.CSS3动画绘制以及各种图 ...
- iframe截取站点的部分内容
<div style="width:630px;height:350px;overflow:hidden;border:0px"> & ...