poj 2891 Strange Way to Express Integers (扩展gcd)
题意:给k对数,每对ai, ri。求一个最小的m值,令m%ai = ri;
分析:由于ai并不是两两互质的, 所以不能用中国剩余定理。
只能两个两个的求。
a1*x+r1=m=a2*y+r2
联立得:a1*x-a2*y=r2-r1;
设r=r2-r2;
互质的模线性方程组m=r[i](mod a[i])。两个方程可以合并为一个,新的a1为lcm(a1,a2),
新的r为关于当前两个方程的解m,然后再和下一个方程合并……。(r2-r1)不能被gcd(a1,a2)整除时无解。 怎么推出的看了好多博客也没有介绍。
下面求最小的解的时候有一个定理,上一篇博客青蛙 也用到了: 对方程 ax ≡ b (mod) n
d = gcd(a,n) 若 d | b 则 有 d 个解 ,最小的解为x0 = (x*(b/d) mod n + n )mod(n/d)
则所有满足方程 x = x0 (mod) (n/d) 的 x 都是原方程的解。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define LL long long
using namespace std; 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); }
}
int main()
{
int flag;
LL k, a1, a2, r1, r2;
LL c, d, x, y, q;
while(~scanf("%lld", &k))
{
flag = ;
scanf("%lld%lld", &a1, &r1);
k--;
while(k--)
{
scanf("%lld%lld", &a2, &r2);
if(flag)
continue;
c = r2-r1;
exgcd(a1, a2, d, x, y);
if(c%d!=)
flag = ;
q = a2/d;
x = (x*(c/d)%q + q)%q; //求最小的解
r1 = x*a1 + r1; //令r1为所求值,x;
a1 = a1*a2/d; //令a1为a1a2最小公倍数
}
if(flag)
printf("-1\n");
else
printf("%lld\n", r1);
}
return ;
}
poj 2891 Strange Way to Express Integers (扩展gcd)的更多相关文章
- POJ.2891.Strange Way to Express Integers(扩展CRT)
题目链接 扩展中国剩余定理:1(直观的).2(详细证明). [Upd:]https://www.luogu.org/problemnew/solution/P4774 #include <cst ...
- POJ - 2891 Strange Way to Express Integers (扩展中国剩余定理)
题目链接 扩展CRT模板题,原理及证明见传送门(引用) #include<cstdio> #include<algorithm> using namespace std; ty ...
- poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9472 ...
- poj——2891 Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 16839 ...
- [POJ 2891] Strange Way to Express Integers
Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 10907 ...
- POJ 2891 Strange Way to Express Integers(拓展欧几里得)
Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
- poj 2891 Strange Way to Express Integers【扩展中国剩余定理】
扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...
- POJ 2891 Strange Way to Express Integers【扩展欧几里德】【模线性方程组】
求解方程组 X%m1=r1 X%m2=r2 .... X%mn=rn 首先看下两个式子的情况 X%m1=r1 X%m2=r2 联立可得 m1*x+m2*y=r2-r1 用ex_gcd求得一个特解x' ...
随机推荐
- SharePoint 101 Code Samples are now available
The Microsoft Office Developer Center has created 101 code samples for SharePoint 2010. These sample ...
- JPA学习---第三节:搭建JPA开发环境和全局事务介绍
一.创建 Java 项目 1.导入所需的 jar 包: 2.创建 persistence.xml 文件, 代码如下: <?xml version="1.0" encoding ...
- 如何自定义Liferay 7 portal的Log in登录界面
前提: 1. Liferay portal 7 2. Liferay IDE 3.0.1 Liferay现有的工具中提供了很多修改portal的模板,以满足开发者的各种自定义需求. 修改的原理是利用M ...
- MyEclipse 2013官网下载地址以及破解方法
刚刚发布了MyEclipse 2013,我现在用的还是6.5的版本,6.5的版本是我觉得最好用的一个版本. 我装上了,还没感受到有哪些好用,就是感觉体积庞大,和IBM 的WID一样,是个多面手,啥事都 ...
- 一些牛逼的统计SQL
SQL 1.查询连续2天,每天发帖大于等于2次的用户 SELECT USER_ID FROM ( SELECT USER_ID, DATEDIFF(CREATE_TIME, '1971-01-01') ...
- Educational Codeforces Round 11 C. Hard Process 前缀和+二分
题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...
- InterlliJ调试:Method breakpoints may dramatically slow down debugging
问题:Method breakpoints may dramatically slow down debugging 因为此问题久久不能调试.问题发现后原来如此... 原因:设置了方法断点!--什么是 ...
- 【DP/二分】BZOJ 1863:[Zjoi2006]trouble 皇帝的烦恼
863: [Zjoi2006]trouble 皇帝的烦恼 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 465 Solved: 240[Submit][ ...
- 【POJ】【1741】/【BZOJ】【1468】Tree
点分治 怎么又一道叫Tree的题目……真是醉了. 本题为漆子超论文<分治算法在树的路径问题中的应用>例一 题解 : http://blog.csdn.net/sdj222555/artic ...
- uva 11181
直接枚举计算就行: #include<cstdio> #include<cstring> #include<algorithm> #define maxn 22 u ...