以前的做法

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
LL ai[],ri[],M;
void Exgcd(LL a,LL b,LL& d,LL& x,LL& y)
{
if(b == ) { d = a, x = , y = ;}
else {
Exgcd(b,a%b,d,y,x); y -= x*(a / b);
}
}
LL gcd(LL a,LL b)
{
return b == ? a : gcd(b,a%b);
}
LL solve(int n)
{
LL a1,r1;
LL a, b, c;
LL x, y, d;
a1 = ai[], r1 = ri[];
for(int i = ; i <= n; ++i)
{
a = a1, b = ai[i], c = ri[i] - r1;
Exgcd(a,b,d,x,y);
if(c % d) return -;
LL t = b / d;
x = (x * (c / d)% t + t) % t;
r1 = a1 * x + r1;
a1 = a1 *(ai[i] / d);
}
return r1;
}
int main()
{
LL p,e,k,d;
int Kase = ;
while(cin >> p >> e >>k >>d)
{
ai[] = , ai[] = , ai[] = ;
ri[] = p , ri[] = e, ri[] = k;
if(p == - && e == - && k == - && d == -) break;
LL ans = solve();
M = ;
for(int i = ; i <= ; ++i) M = M * ai[i] / gcd(M,ai[i]);
if(ans >= M) ans %= M; // 求最小的值.
while(ans <= d) ans += M;
printf("Case %d: the next triple peak occurs in %lld days.\n",Kase++,ans-d);
}
}

中国剩余定理撸一发

不过中国剩余定理 ai[i] 需要互质

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long LL;
LL ai[],ri[],M;
void Exgcd(LL a,LL b,LL& d,LL& x,LL& y)
{
if(b == ) { d = a, x = , y = ;}
else {
Exgcd(b,a%b,d,y,x); y -= x*(a / b);
}
}
LL gcd(LL a,LL b)
{
return b == ? a : gcd(b,a%b);
}
LL China(int n)
{
M = ;
LL Mi,x,y,d,ans = ;
for(int i = ; i <= n; ++i) M *= ai[i];
for(int i = ; i <= n; ++i)
{
Mi = M / ai[i];
Exgcd(Mi,ai[i],d,x,y);
ans = (ans + Mi * x * ri[i]) % M;
}
if(ans < ) ans += M;
return ans ;
}
int main()
{
LL p,e,k,d;
int Kase = ;
while(cin >> p >> e >>k >>d)
{
ai[] = , ai[] = , ai[] = ;
ri[] = p , ri[] = e, ri[] = k;
if(p == - && e == - && k == - && d == -) break;
LL ans = China();
/*M = 1;
for(int i = 1; i <= 3; ++i) M = M * ai[i] / gcd(M,ai[i]);*/
//if(ans >= M) ans %= M; // 求最小的值.
while(ans <= d) ans += M;
printf("Case %d: the next triple peak occurs in %lld days.\n",Kase++,ans-d);
}
}

POJ 1006 同余方程组的更多相关文章

  1. POJ 1006 Biorhythms (中国剩余定理)

    在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...

  2. 【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组

    http://poj.org/problem?id=2891 题意:与中国剩余定理不同,p%ai=bi,此处的ai(i=1 2 3 ……)是不一定互质的,所以要用到的是同余方程组,在网上看到有人称为拓 ...

  3. POJ 1006 Biorhythms(中国剩余定理)

    题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...

  4. poj 1006 中国剩余定理解同余方程

    其实画个图就明白了, 该问题就是求同余方程组的解: n+d≡p (mod 23) n+d≡e (mod 28) n+d≡i (mod 33) #include "iostream" ...

  5. POJ 1006 - Biorhythms (中国剩余定理)

    B - Biorhythms Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Subm ...

  6. 【poj2891】同余方程组

    同余方程组 例题1:pku2891Strange Way to Express Integers 中国剩余定理求的同余方程组mod 的数是两两互素的.然而本题(一般情况,也包括两两互素的情况,所以中国 ...

  7. [POJ 1006]生理周期

    Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰 ...

  8. POJ 1006 生理周期(中国剩余定理)

    POJ 1006 生理周期 分析:中国剩余定理(注意结果要大于d即可) 代码: #include<iostream> #include<cstdio> using namesp ...

  9. POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)

    POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...

随机推荐

  1. 让Windows Server 2008r2 IIS7.5 ASP.NET 支持10万并发请求

    由于之前使用的是默认配置,服务器最多只能处理5000个同时请求,今天下午由于某种情况造成同时请求超过5000,从而出现了上面的错误. 为了避免这样的错误,我们根据相关文档调整了设置,让服务器从设置上支 ...

  2. es6模块化导入导出

    模块化指的就是将一个大程序拆分成若干个互相依赖的小文件,然后在用简单的方法拼装起来. 在 ES6 之前,JS没有模块化系统,社区制定了一些模块加载方案 最主要的有 CommonJS(Asynchron ...

  3. Native APP ,Web APP,Hybrid APP三者对比

    Native APP Native APP 指的是原生程序(Android.iOS.WP),一般依托于操作系统,有很强的交互,可拓展性强,需要用户下载安装使用,是一个完整的App. 原生应用程序是某一 ...

  4. springboot(十八):CORS方式实现跨域

    资料 https://www.cnblogs.com/toutou/p/9843588.html

  5. Extjs.net Button点击下载jpg图片

    <ext:Button ID=" AutoPostBack="false"> <DirectEvents> <Click OnEvent=& ...

  6. sql server中的merge

    http://www.cnblogs.com/CareySon/archive/2012/03/07/2383690.html 简介 Merge关键字是一个神奇的DML关键字.它在SQL Server ...

  7. 【bzoj 2588】Spoj 10628. Count on a tree

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

  8. Andrew NG 机器学习编程作业2 Octave

    问题描述:用逻辑回归根据学生的考试成绩来判断该学生是否可以入学 这里的训练数据(training instance)是学生的两次考试成绩,以及TA是否能够入学的决定(y=0表示成绩不合格,不予录取:y ...

  9. PHP7语法知识(二):流程控制语句、函数、字符串、数组

    流程控制: 一.条件控制语句 1.if条件控制语句: 2.switch分支语句: 二.循环控制语句 1.while循环: 2.do while循环: 3.for循环: 4.foreach循环: 三.跳 ...

  10. ES6走一波 变量结构赋值

    Destructuring  变量的解构赋值 是一种模式匹配 ES6我关注点之一是用途  能否举些好例子是检验学习到位的方法之一 交换变量值 函数返回多个值 函数入参为对象.数组,内部使用更简洁 意义 ...