POJ 1006 Biorhythms(中国剩余定理)
题目地址:POJ 1006
学习了下中国剩余定理。參考的该博客。博客戳这里。
中国剩余定理的求解方法:
假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c2,c3的公倍数且余c1为1,同理。R2,R3也是如此。然后设z=R1*m1+R2*m2+R3*m3,那么z就是当中一个解。并且每隔(c1,c2,c3)的最小公倍数就是一个解。想要最小解的话,仅仅需对最小公倍数取余即可了。
以下的代码未删改。比赛的时候为了避免超时,R1,R2,R3的求解过程全然没有必要放在程序里,自己算出来直接用上即可。
代码例如以下:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
#define LL __int64
int main()
{
LL a, b, c, d, R1, R2, R3, i, j, k, R, num=0;
R1=28*33;
R2=23*33;
R3=23*28;
for(i=1;; i++)
{
if(R1*i%23==1)
{
R1*=i;
break;
}
}
for(i=1;; i++)
{
if(R2*i%28==1)
{
R2*=i;
break;
}
}
for(i=1;; i++)
{
if(R3*i%33==1)
{
R3*=i;
break;
}
}
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&c,&d)!=EOF)
{
if(a<0&&b<0&&c<0&&d<0)
break;
num++;
R=R1*a+R2*b+R3*c;
LL z, ans;
z=R%21252;//21252为a,b,c的最小公倍数
if(z<=d)
{
z+=21252;
}
ans=z-d;
printf("Case %I64d: the next triple peak occurs in %I64d days.\n",num,ans);
}
return 0;
}
POJ 1006 Biorhythms(中国剩余定理)的更多相关文章
- 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 ...
- PKU POJ 1006 Biorhythms (中国剩余定理)
中国剩余定理 x = ai (mod mi) ai和mi是一组数,mi两两互质,求x 令Mi = m1*m2*~mk 其中,mi不包含在内. 因为mi两两互质,所以存在x和y, st M ...
- POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)
POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...
- Biorhythms(中国剩余定理)
http://shuxueshi.jie.blog.163.com/blog/static/13611628820104179856631/ 这篇博客写的很棒! #include<stdio.h ...
- poj 1006 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- poj 1006:Biorhythms(水题,经典题,中国剩余定理)
Biorhythms Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 110991 Accepted: 34541 Des ...
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
- 中国剩余定理 POJ 1006 Biorhythms
题目传送门 题意:POJ有中文题面 分析:其实就是求一次同余方程组:(n+d)=p(%23), (n+d)=e(%28), (n+d)=i(%33),套用中国剩余定理模板 代码: /********* ...
随机推荐
- Controllers, Actions 和 Action Results
Controllers, Actions 和 Action Results 原文:Controllers, Actions, and Action Results作者:Steve Smith翻译:姚阿 ...
- 一些Swift编程语言的相关资料
苹果官方Swift文档<The Swift Programming Language> 苹果开发者Swift文档及介绍 中文版Apple官方Swift教程(Github协作翻译中) Git ...
- 什么是xsi:type ???
http://www.w3.org/2001/XMLSchema-instance http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.ecli ...
- BZOJ 1629: [Usaco2007 Demo]Cow Acrobats
Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...
- 如何有效申请TI的免费样片
转自如何有效申请TI的免费样片 TI公司愿意为支持中国大学的师生们的教学.实验.创新实践.竞赛和科研项目,提供有限数量的免费样片.首先需要指出的是:所有的样片申请应该是诚实正当的,所有不恰当的申 ...
- Android 使用XmlPullParser解析xml
这里我们假设要解析的xml文件名为:test.xml,我们将其放在assets路径中. xml文件内容为: <?xml version='1.0' encoding='utf-8' standa ...
- 【网络流24题】 No.5 圆桌问题 (多重匹配)
[题意] 假设有来自 n 个不同单位的代表参加一次国 际会议.每个单位的代表数分别为r i ni , = 1,2, .会议餐厅共有 m 张餐桌,每张餐桌可容纳 ci (i = 1,2, , m) 个 ...
- 读取spring配置文件的方法(spring读取资源文件)
1.spring配置文件 <bean id="configproperties" class="org.springframework.beans.factory. ...
- spring初始化
* Created by litao on 15/12/29. */@Component("initTagDataProcessor")public class InitTagDa ...
- 解决qt5窗口不刷新(测试窗口类型,测试窗口属性)
QApplication::notify #if QT_VERSION >= 0x050000 if (QEvent::Show == pEvent->type()) ...