POJ 1006 同余方程组
以前的做法
#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 同余方程组的更多相关文章
- POJ 1006 Biorhythms (中国剩余定理)
在POJ上有译文(原文右上角),选择语言:简体中文 求解同余方程组:x=ai(mod mi) i=1~r, m1,m2,...,mr互质利用中国剩余定理令M=m1*m2*...*mr,Mi=M/mi因 ...
- 【poj2891-Strange Way to Express Integers】拓展欧几里得-同余方程组
http://poj.org/problem?id=2891 题意:与中国剩余定理不同,p%ai=bi,此处的ai(i=1 2 3 ……)是不一定互质的,所以要用到的是同余方程组,在网上看到有人称为拓 ...
- POJ 1006 Biorhythms(中国剩余定理)
题目地址:POJ 1006 学习了下中国剩余定理.參考的该博客.博客戳这里. 中国剩余定理的求解方法: 假如说x%c1=m1,x%c2=m2,x%c3=m3.那么能够设三个数R1,R2,R3.R1为c ...
- poj 1006 中国剩余定理解同余方程
其实画个图就明白了, 该问题就是求同余方程组的解: n+d≡p (mod 23) n+d≡e (mod 28) n+d≡i (mod 33) #include "iostream" ...
- POJ 1006 - Biorhythms (中国剩余定理)
B - Biorhythms Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Subm ...
- 【poj2891】同余方程组
同余方程组 例题1:pku2891Strange Way to Express Integers 中国剩余定理求的同余方程组mod 的数是两两互素的.然而本题(一般情况,也包括两两互素的情况,所以中国 ...
- [POJ 1006]生理周期
Description 人生来就有三个生理周期,分别为体力.感情和智力周期,它们的周期长度为23天.28天和33天.每一个周期中有一天是高峰.在高峰这天,人会在相应的方面表现出色.例如,智力周期的高峰 ...
- POJ 1006 生理周期(中国剩余定理)
POJ 1006 生理周期 分析:中国剩余定理(注意结果要大于d即可) 代码: #include<iostream> #include<cstdio> using namesp ...
- POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)
POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理) 题意分析 不妨设日期为x,根据题意可以列出日期上的方程: 化简可得: 根据中国剩余定理求解即可. 代码总览 #include & ...
随机推荐
- ubuntu主题收集
ubuntu主题收集 一些cmd常用命令: 任务栏底部,进入Dash并打开终端,命令最后一个是参数可选 ( Left | Bottom ) gsettings set com.canonical.Un ...
- chart.js angular组件封装(ng6)、实战配置、插件编写
前言 项目需要使用chart.js插件,由于项目是使用angular开发,那么我第一步就是先把chart.js改造成angular组件来使用. 本项目代码都可以在github上下载:项目git地址 a ...
- NFine框架全选checkBox列错位
在jqgrid.css里找到 .ui-jqgrid .cbox{margin-left: -1px;position: initial;vertical-align: text-bottom;}.ui ...
- python3.x与2.x区别
1.性能 Py3.0运行 pystone benchmark的速度比Py2.5慢30%.Guido认为Py3.0有极大的优化空间,在字符串和整形操作上可 以取得很好的优化结果. Py3.1性能比Py2 ...
- luogu 2827 蚯蚓 单调队列/优先队列
易知可利用优先队列选取最大值: 但是通过分析可知,先取出的蚯蚓分开后仍然要比后分的长,所以可直接利用单调队列找队头即可,分三个单调队列,分别找未切割,切割,切割2三种情况 #include<bi ...
- python 读 json 文件
一个汽车图标的训练集:train.json [{"items": [{"label_id": "0028", "bbox" ...
- pyqt5-控件是否可用
setEnabled(bool) 设置控件是否可用 True 可用 isEnabled() 获取控件是否可用 s=button.isEnabled()
- Hbase思维导图之逻辑结构
- GDI+学习---2.GDI+编程模式及组成类
在使用GDI+的时候,您不必像在GDI中那样关心设备场景句柄,只需简单地创建一个Graphics对象,然后以您熟悉的面向对象的方式(如myGraphicsObject.DrawLine(paramet ...
- linux 基础笔记本
ubuntu 端口使用查看 查看已经连接的服务端口(ESTABLISHED) netstat -a 查看所有的服务端口(LISTEN,ESTABLISHED) netstat -ap 查看指定端口,可 ...