中国剩余定理 (POJ 1006)
http://poj.org/problem?id=1006
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 125972 | Accepted: 39820 |
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.
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm> using namespace std;
typedef long long ll;
const int maxn = ; int mod; int exgcd(int a, int b, int &x, int &y)
{
if(b==)
{
x = ;
y = ;
return a;
}
int ans = exgcd(b, a%b, x, y);
int t = x;
x = y;
y = t - a/b*y;
return ans;
} int china_remainder(int chu[], int yu[], int n)
{
int i, m, x, y, ans=; mod = ;
for(i=; i<n; i++)
mod *= chu[i]; for(i=; i<n; i++)
{
m = mod/chu[i];
exgcd(m, chu[i], x, y);
ans = (ans + x*yu[i]*m)%mod;
} return ans;
} int main()
{
int iCase=, chu[]={, , }, yu[], d; while(scanf("%d%d%d%d", &yu[], &yu[], &yu[], &d)!=EOF)
{
int ans;
if(yu[]==- && yu[]==- && yu[]==- && d==-) break; ans = china_remainder(chu, yu, ); ans = (ans-d+mod)%mod; if(ans==) ans = mod; printf("Case %d: the next triple peak occurs in %d days.\n", iCase++, ans);
} return ;
}
中国剩余定理 (POJ 1006)的更多相关文章
- 中国剩余定理 POJ 1006 Biorhythms
题目传送门 题意:POJ有中文题面 分析:其实就是求一次同余方程组:(n+d)=p(%23), (n+d)=e(%28), (n+d)=i(%33),套用中国剩余定理模板 代码: /********* ...
- POJ 1006 - Biorhythms (中国剩余定理)
B - Biorhythms Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Subm ...
- POJ 1006 Biorhythms(中国剩余定理)
题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...
- 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms
题目链接: http://poj.org/problem?id=1006 http://acm.hdu.edu.cn/showproblem.php?pid=1370 题目大意: (X+d)%23=a ...
- POJ 1006 生理周期(中国剩余定理)
POJ 1006 生理周期 分析:中国剩余定理(注意结果要大于d即可) 代码: #include<iostream> #include<cstdio> using namesp ...
- poj 1006 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)
POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
- poj 1006中国剩余定理模板
中国剩余定理(CRT)的表述如下 设正整数两两互素,则同余方程组 有整数解.并且在模下的解是唯一的,解为 其中,而为模的逆元. 模板: int crt(int a[],int m[],int n) { ...
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110991 Accepted: 34541 Des ...
随机推荐
- python 验证码 和进度条
import random def make_code(n): res='' for i in range(n): s1=chr(random.randint(65,90)) s2=str(rando ...
- bowtie:短序列比对的新工具
bowtie:短序列比对的新工具(转) (2014-11-17 22:15:24) 转载▼ 标签: 转载 原文地址:bowtie:短序列比对的新工具(转)作者:玉琪星兆 Bowtie是一个超级快速 ...
- Mongodb数据导出工具mongoexport和导入工具mongoimport介绍(转)
原文地址:http://chenzhou123520.iteye.com/blog/1641319 一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个colle ...
- Luogu 2279 [HNOI2003]消防局的设立 - 贪心
Description 给定一棵树形图, 建若干个消防站, 消防站能够覆盖到距离不超过2的点, 求最少需要建几个消防站才能覆盖所有点 Solution 从深度最深的点开始, 在它的爷爷节点上建, 每建 ...
- 使用VisualStudio开发php的图文设置方法
早先在asp横行的年代,php和asp一样,大都都是html中夹杂代码,说实话,这时候IDE的确用处不是很大,倒是类似于dw之类的设计器甚为上手. 现在,三层.mvc之类的思想遍地开花,使得代码和ht ...
- tiny cc 编译器,tinycc,变种
去掉了 -run 参数 下载代码和编译好的程序
- Python的程序入口 __name__属性
python中每个模块都有一个 '__name__' 属性,当其值为 '__main__' 时,表名该模块自身在运行,否则是被引入的. 当一个模块被当做一个整体调用的时候,模块名.__name__ 的 ...
- CSS-背景-渐变-文本格式化
1.背景 1.背景色 属性:background-color 取值:合法的颜色值 注意:背景颜色和背景图片默认都从边框位置处开始填充 2.背景图片 属性:background-image 取值:url ...
- python ui学习过程,使用pyqt5实现
首先安装pyqt5的包,然后打开notebook就可以编写了.当然这样编写,也可以用designer进行. 它是pyqt5-tools的一个exe软件,\Anaconda3\Lib\site-pack ...
- kbmmw 中JSON 中使用SQL 查询
前面讲到了kbmmw 的JSON 对象操作,如何快速的查找JSON 中的值? 一种办法就是通过遍历的方法,其实在kbmmw 还有一种灵活的查询方式, 就是通过SQL 方式查询JSON 中的值.也就是说 ...