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 ...
随机推荐
- 【WEB基础】HTML & CSS 基础入门(7)表格
表格的基本结构 表格是网页上最常见的元素,它除了可以用来展示数据,还常常被用来排版.虽然现在提倡使用DIV+CSS完成页面布局,但表格框架简单明了,对于繁杂的数据,一个简洁的表格能让其展现的极有条理. ...
- GPP加密破解工具gpp-decrypt
GPP加密破解工具gpp-decrypt GPP是Group Policy Preferences(组策略首选项)的缩写,这是一个组策略实施工具.通过该工具,网络管理员可以实现更多的网络管理,如驱 ...
- CountDownLatch、CyclicBarrier、Samephore浅谈三大机制
CountDownLatch.CyclieBarrier与SamePhore都可用来控制线程的执行,那么他们之间有什么区别呢 CountDownLatch CountDowenlatch可以看成一个线 ...
- Maven教程:tutorialspoint-maven
来自turorialspoint的Maven教程(英文),官网:http://www.tutorialspoint.com/maven/index.htm 这个教程在国内已经被翻译成中文,官网:htt ...
- xcode创建一个工程的多个taget,便于测试和发布多个版本
背景:很多时候,我们需要在一个工程中创立多个target,也就是说我们希望同一份代码可以创建两个应用,放到模拟器或者真机上,或者是,我们平时有N多人合作开发,当测试的时候,在A这里装了一遍测A写的那块 ...
- jquery怎么找到元素下面的第一个子元素
<ul><li>11</li><li>22</li><li>33</li><li>44</li&g ...
- 设置NSZombieEnabled和MallocStackLogging
在XCode.4以上版本号中,设置NSZombieEnabled和MallocStackLogging 1.点击XCode的Product菜单.选择Edit Scheme...选项 2.选择左側的Ru ...
- drag-html
<!doctype html><html><head><meta charset="UTF-8" /><title>Ca ...
- 【Nginx】事件驱动框架和异步处理
Nginx对请求的处理是通过事件触发的,模块作为事件消费者,仅仅能被事件收集.分发器调用.这与传统的Webserver是不同的. 传统的Webserver下,一个请求由一个进程消费.请求在建立连接后将 ...
- openCV—Python(1)——初始化环境
本系列博客主要參考自--Adrian Rosebrock:<Practical Python and OpenCV: An Introductory,Example Driven Guide t ...