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 * ...
随机推荐
- HDU6446(树上、排列的贡献计算)
关键点在于:全排列中,任意两点u.v相邻的次数一定是(n - 1)! * 2次,即一个常数(可以由高中数学知识计算,将这两个点捏一起然后全排列然后乘二:或者用n! / C(2, n)). 这之后就好算 ...
- 回文树1960. Palindromes and Super Abilities
Bryce1010模板 http://acm.timus.ru/problem.aspx?space=1&num=1960 #include <bits/stdc++.h> usi ...
- 131 Palindrome Partitioning 分割回文串
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串.返回 s 所有可能的分割方案.例如,给出 s = "aab",返回[ ["aa"," ...
- rhel 6.5--samba
配置匿名共享: 服务端: [root@master ~]# yum install -y samba 或者 [root@master ~]# yum groupinstall -y "CIF ...
- websocket 加layim实现在线聊天系统
实现流程: 1.浏览器连接服务器时保存所有用户id以及对应的唯一session(session用户用户消息推送). 1.1:判断登录用户是否有离线消息(个人消息以及群消息),有则将离线消息进行推送给登 ...
- CF782A Andryusha and Socks
题意: Andryusha is an orderly boy and likes to keep things in their place. Today he faced a problem to ...
- canvas基础绘制-arc
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android获取本地相册图片、拍照获取图片
需求:从本地相册找图片,或通过调用系统相机拍照得到图片. 容易出错的地方: 1,当我们指定了照片的uri路径,我们就不能通过data.getData();来获取uri,而应该直接拿到uri(用全局变量 ...
- Angular和SAP C4C的事件处理队列
Angular 我们在Angular框架的代码里能看到一个名为processQueue的函数: 这个函数是通过$scope.$apply启动的: 核心代码位于一个for循环里,循环体是一个存储异步处理 ...
- 闲着蛋疼没事干,写个Mac端的Kcptun Client管理器
原理: 执行一行脚本 输入服务器地址,端口,密码等做了图形化编辑 可以控制Kcptun是否正在运行 App已上传github https://github.com/nicky2k8/KcptunCli ...