以前的做法

#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. Silverlight数据绑定之 绑定一个int类型的属性

    还就真心不会啊! 在类FunctionPanel中作如下定义: /// <summary> /// 鼠标状态 属性 /// </summary> public Dependen ...

  2. 【Unity&C#】lambda函数

    以下两段代码等价. 未使用lambda函数的代码: private Button PB; private void Start() { PB = GameObject.Find("PButt ...

  3. 【python小练】0010

    第 0010 题:使用 Python 生成类似于下图中的字母验证码图片 思路: 1. 随机生成字符串 2. 创建画布往上头写字符串 3. 干扰画面 code: # codeing: utf-8 fro ...

  4. springboot11-security02FromDB 权限管理(用户信息和角色信息保存在数据库)

    <h4>场景</h4> <h4>代码</h4> springboot+springsecurity+mysql(jpa)实现: 1.pom依赖: < ...

  5. EF 事物Transaction简单操作

    /// <summary> /// 申请提现 /// </summary> /// <param name="userId">用户id</ ...

  6. jQuery滑动

    通过 jQuery,您可以在元素上创建滑动效果. jQuery 拥有以下滑动方法: slideDown(speed,callback):用于向下滑动元素. slideUp(speed,callback ...

  7. [C++]Linux之C编程异常[true未定义解决方案]

    C语言里面是没有bool(布尔)类型的,C++里面才有,这就是说,在C++里面使用bool类型是没有问题的.bool类型有只有两个值:true =1 .false=0. 但是,C99标准里面,又定义了 ...

  8. 树链剖分模板(洛谷P3384)

    洛谷P3384 #include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " < ...

  9. jq的load

    1.用于监听加载是否完成 当指定的元素(及子元素)已加载时,会发生 load() 事件. 该事件适用于任何带有 URL 的元素(比如图像.脚本.框架.内联框架). 根据不同的浏览器(Firefox 和 ...

  10. Django REST framework 第三章 CBV

    从介绍Django快开始,我们就一直在使用FBV的方式来撰写代码,二者本质上并没有太大的区别,然而到了REST framework,更会倾向于用CBV来写API的视图,后面会看到这个方式的强大,它允许 ...