poj2891 Strange Way to Express Integers poj1006 Biorhythms 同余方程组
怎样求同余方程组?如:
x \equiv a_1 \pmod {m_1} \\
x \equiv a_2 \pmod {m_2} \\
\cdots \\
x \equiv a_n \pmod {m_n}
\end{cases}\]
不保证 \(m\) 两两互素?
两两合并!
比方说
x \equiv a_1 \pmod {m_1} \\
x \equiv a_2 \pmod {m_2} \\
\end{cases}\]
就是
x = m_1x_1+a_1\\
x = m_2x_2+a_2\\
\end{cases}\]
可以变形成
\]
拿扩欧搞掉这个方程。我们肯定想让 \(x\) 最小,那就让 \(x_1\) 最小,这样就求出了 \(x\) 的特解 \(x'\)。
显然, \(x\) 的通解是 \(x=x'+[m_1,m_2] \times t ,t \in \mathbb{Z}\)。
这也就很像是
\]
我们惊喜地发现两个方程变成了一个方程。一路做下去就好了。
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
int n;
ll a, r, m, aa, rr, x, y;
bool flag;
ll exgcd(ll a, ll b, ll &x, ll &y){
if(!b){
x = 1;
y = 0;
return a;
}
ll re=exgcd(b, a%b, x, y);
ll z=x;
x = y;
y = z - a / b * y;
return re;
}
int main(){
while(scanf("%d", &n)!=EOF){
flag = true;
n--;
scanf("%lld %lld", &aa, &rr);
while(n--){
scanf("%lld %lld", &a, &r);
if(!flag) continue;
ll gcd=exgcd(aa, a, x, y);
if((r-rr)%gcd) flag = false;
else
x = (((r-rr)/gcd*x)%(a/gcd)+a/gcd)%(a/gcd);
x = rr + x * aa;
rr = x;
aa = a/gcd*aa;
}
if(flag) printf("%lld\n", x);
else printf("-1\n");
}
return 0;
}
如果保证两两互素呢?那就中国剩余定理了。记 \(m =\prod_{i=1}^n m_i\),\(M_i=m/m_i\),\(t_i\) 是 \(M_i\) 在模 \(m_i\) 意义下的乘法逆元,则一个特解是 \(\sum_{i=1}^n a_iM_it_i\)。通解是 \(\sum_{i=1}^n a_iM_it_i + mk, k \in \mathbb{Z}\)。
证明:因为当 \(i \not =j\)时,\(m_j|M_i\),则 \(a_iM_it_i \equiv 0 \pmod {m_j}\),而 \(a_jM_jt_j \equiv a_j \pmod {m_j}\),证毕。
#include <iostream>
#include <cstdio>
using namespace std;
int a[15], cnt, mul, ans, x, y, dd;
const int m[]={0, 23, 28, 33};
int exgcd(int aa, int bb, int &x, int &y){
if(!bb){
x = 1;
y = 0;
return aa;
}
int re=exgcd(bb, aa%bb, x, y);
int z=x;
x = y;
y = z - aa / bb * y;
return re;
}
int ni(int aa, int bb){
int gcd=exgcd(aa, bb, x, y);
return (x%bb+bb)%bb;
}
int main(){
while(scanf("%d %d %d %d", &a[1], &a[2], &a[3], &dd)!=EOF){
mul = 1;
if(a[1]<0) break;
ans = 0;
for(int i=1; i<=3; i++)
a[i] %= m[i], mul *= m[i];
for(int i=1; i<=3; i++)
ans += a[i] * (mul/m[i])%mul * ni(mul/m[i], m[i])%mul;
ans -= dd;
ans = (ans%mul+mul)%mul;
if(!ans) ans += mul;
printf("Case %d: the next triple peak occurs in %d days.\n", ++cnt, ans);
}
return 0;
}
poj2891 Strange Way to Express Integers poj1006 Biorhythms 同余方程组的更多相关文章
- POJ 2891 Strange Way to Express Integers | exGcd解同余方程组
题面就是让你解同余方程组(模数不互质) 题解: 先考虑一下两个方程 x=r1 mod(m1) x=r2 mod (m2) 去掉mod x=r1+m1y1 ......1 x=r2+m2y2 . ...
- 中国剩余定理+扩展中国剩余定理 讲解+例题(HDU1370 Biorhythms + POJ2891 Strange Way to Express Integers)
0.引子 每一个讲中国剩余定理的人,都会从孙子的一道例题讲起 有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二.问物几何? 1.中国剩余定理 引子里的例题实际上是求一个最小的x满足 关键是,其中 ...
- POJ2891——Strange Way to Express Integers(模线性方程组)
Strange Way to Express Integers DescriptionElina is reading a book written by Rujia Liu, which intro ...
- POJ2891 Strange Way to Express Integers
题意 Language:Default Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total S ...
- POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x, ...
- P4777 【模板】扩展中国剩余定理(EXCRT)/ poj2891 Strange Way to Express Integers
P4777 [模板]扩展中国剩余定理(EXCRT) excrt模板 我们知道,crt无法处理模数不两两互质的情况 然鹅excrt可以 设当前解到第 i 个方程 设$M=\prod_{j=1}^{i-1 ...
- POJ2891 - Strange Way to Express Integers(模线性方程组)
题目大意 求最小整数x,满足x≡a[i](mod m[i])(没有保证所有m[i]两两互质) 题解 中国剩余定理显然不行....只能用方程组两两合并的方法求出最终的解,刘汝佳黑书P230有讲~~具体证 ...
- POJ2891 Strange Way to Express Integers [中国剩余定理]
不互质情况的模板题 注意多组数据不要一发现不合法就退出 #include <iostream> #include <cstdio> #include <cstring&g ...
- POJ2891 Strange Way to Express Integers【扩展中国剩余定理】
题目大意 就是模板...没啥好说的 思路 因为模数不互质,所以直接中国剩余定理肯定是不对的 然后就考虑怎么合并两个同余方程 \(ans = a_1 + x_1 * m_1 = a_2 + x_2 * ...
随机推荐
- python之迷宫BFS
# @File: maze_queue_bfs from collections import deque maze = [ [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0 ...
- pwa-serviceWorker与页面通信postMessage
https://ppt.geekbang.org/list/gmtc2018?from=groupmessage&%3Bisappinstalled=0 http://www.sohu. ...
- poj1857 To Europe! To Europe!
思路: 一维dp. 实现: #include <cstdio> #include <iostream> using namespace std; const int INF = ...
- canvas基础绘制-倒计时(上)
效果: html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- netcdf源码在windows上的编译
作者:朱金灿 来源:http://blog.csdn.net/clever101 今天搞搞netcdf源码在windows上的编译,折腾了半天,算是搞成了,特地记录一下过程.我的目标是要生成netcd ...
- Android图片压缩上传(二)
之前有用到libjpeg,还是有一定的局限性,最近用了一个新的方式,效果还是挺不错,随着作者的版本更新,Bug也随之变少,目前项目中运用已上线. 1.之前的方式Android图片压缩,不失真,上线项目 ...
- php配置之include_path
在php.ini中配置include_path,可在引入文件时直接引入配置目录下的文件. 项目中就可以直接 引入/var/www/phpxwlib及/var/www/huicuiserver/libs ...
- MyBatis学习(三)
前言 感觉学习进度还是比较慢啊,一整天的学习效率不是很高,一会看电视,一会喝茶,对自己的要求不严格...今天就说说关联表数据的插入以及别名的使用. 正文 1.关联插入 之前,我在数据库中已经创建了一张 ...
- innerHTML的性能问题
看到一遍文章When innerHTML isn’t Fast Enough,反应了innerHTML在操作量大了以后的性能下降的问题. 并且给出了一个replaceHTML的函数 function ...
- File.Exists 文件不存在 Or FileNotFoundException
标题警告,本文仅限走投无路,最终可能的一个问题导致. 最开始出现在找不到文件,测试发现: 看起来毫无毛病 而后各种测试: 注意看,第一行跟第三行一模一样 发现[@"]这两个字符有毒,如 ...