poj 1006 中国剩余定理解同余方程
其实画个图就明白了,
该问题就是求同余方程组的解:
n+d≡p (mod 23)
n+d≡e (mod 28)
n+d≡i (mod 33)
#include "iostream"
using namespace std;
int a[],m[];
int p,e,i,d,ans; int extend_gcd(int a,int b,int &x,int &y){
if (b==){
x=;y=;
return a;
}
else{
int r=extend_gcd(b,a%b,y,x);
y=y-x*(a/b);
return r;
}
} int CRT(int a[],int m[],int n)
{
int M=;
for (int i=;i<=n;i++) M*=m[i];
int ret=;
for (int i=;i<=n;i++)
{
int x,y;
int tm=M/m[i];
extend_gcd(tm,m[i],x,y);
ret=(ret+tm*x*a[i])%M;
}
return (ret+M)%M;
} int main()
{
int T=;
while (cin>>p>>e>>i>>d)
{
T++;
if ((p==-)&&(e==-)&&(i==-)&&(d==-))
break;
a[]=p; a[]=e; a[]=i;
m[]=; m[]=; m[]=;
ans=CRT(a,m,);
ans=ans-d;
if (ans<) ans+=;
ans=ans%;
if (ans==) ans=;
//Case 1: the next triple peak occurs in 1234 days.
cout<<"Case "<<T<<": the next triple peak occurs in "<<ans<<" days."<<endl;
}
return ;
}
poj 1006 中国剩余定理解同余方程的更多相关文章
- poj 1006中国剩余定理模板
中国剩余定理(CRT)的表述如下 设正整数两两互素,则同余方程组 有整数解.并且在模下的解是唯一的,解为 其中,而为模的逆元. 模板: int crt(int a[],int m[],int n) { ...
- 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 生理周期(中国剩余定理)
POJ 1006 生理周期 分析:中国剩余定理(注意结果要大于d即可) 代码: #include<iostream> #include<cstdio> using namesp ...
- POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)
POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...
- poj 1696 叉积理解
Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3967 Accepted: 2489 Descrip ...
- 【中国剩余定理】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 Biorhythms (中国剩余定理模板)
http://poj.org/problem?id=1006 题目大意: 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这 ...
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
随机推荐
- java 21 - 13 IO流之序列化和反序列化
序列化流:把对象按照流一样的方式存入文本文件或者在网络中传输.对象 -- 流数据(ObjectOutputStream) 构造方法:ObjectInputStream(InputStream in) ...
- AndroidStudio权威教程 AS添加第三方库的6种方式(Jar module so等)
点击项目设置按钮 依次选择 App > Dependencies 1. 直接搜索法 依次选择 + > Library dependency 这里的搜索一定要是全名的,不然搜不到哦 下图所表 ...
- this关键字的使用
一,表示类中属性 1,没有使用this的情况 class Person{ // 定义Person类 private String name ; // 姓名 private int age ; // 年 ...
- Yii提供的Htmler助手checkboxList可自定义Checkbox输出格式
foreach($catetags as $cate){ echo Html::checkboxList('category_id','',$cate,['item'=>'customCheck ...
- 解决ios下的微信打开的页面背景音乐无法自动播放
后面的项目发现,还有两个坑,需要注意下: ·本文的解决方案的核心是利用了 微信/易信 在ready的时候会有个 WeixinJSBridgeReady/YixinJSBridgeReady事件,通过监 ...
- WPF SDK研究 之 AppModel
Jianqiang's Mobile Dev Blog iOS.Android.WP CnBlogs Home New Post Contact Admin Rss Posts - 528 Artic ...
- 微软职位内部推荐-Software Development Engineering II
微软近期Open的职位: Job Title: Software Development Engineering II Work Location: Suzhou, China Enterprise ...
- 对RESTful Web API的理解与设计思路
距离上一篇关于Web API的文章(如何实现RESTful Web API的身份验证)有好些时间了,在那篇文章中提到的方法是非常简单而有效的,我在实际的项目中就这么用了,代码经过一段时间的磨合,已经很 ...
- Hello,cnblog‘s world!
纠结了许久,终于选在这个地方安家.之所以选在这里,是因为这里是个很干净的社区. 之前一直在其他博文网站里发文章,可是越到后来,发现页面广告越来越多.真正对自己有价值的内容越来越少,虽然已经使用过几年了 ...
- LeetCode 笔记25 Candy (艰难的调试)
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...