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' ...
随机推荐
- c++ freelockquque
http://www.boost.org/doc/libs/1_56_0/doc/html/boost/lockfree/queue.html Class template queue boost:: ...
- angular入门系列教程3
主题: 本篇主要目的就是继续完善home页,增加tab导航的三个页index index1 index2 效果图: 细节: 初始化的JS就是咱们的home.js,仔细来看. angular的route ...
- 【BZOJ】【1833】【ZJOI2010】count 数字计数
数位DP Orz iwtwiioi 学习了一下用记忆化搜索来捉题的新姿势……但没学会TAT,再挖个坑(妈蛋难道对我来说数位DP就是个神坑吗……sigh) //BZOJ 1833 #include< ...
- phonegap 环境搭建
经过了一番讨论,最后还是决定用phonegap来开发产品.因为用phonegap的人力成本相比原生开发还是节省了不少,并且可以跨平台.至于软件的运行效率,在ios上还是相当流畅的,在android上就 ...
- [设计模式] 12 代理模式 proxy
在GOF的<设计模式:可复用面向对象软件的基础>一书中对代理模式是这样说的:为其他对象提供一种代理以控制对这个对象的访问.结合上面的游戏代理的例子和下面的图,我们来进行分析一下.以前你是这 ...
- spring <context:component-scan>(转)
在xml配置了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类 ...
- Unity3d 接入 移动MM支付SDK(2.3) 全攻略
原地址:http://blog.csdn.net/dingxiaowei2013/article/details/26842177 先将例程运行起来 下载例程(csdn积分不够上传不了,只能用百度网盘 ...
- String.IsNullOrEmpty()和String.IsNullOrWhiteSpace()
转自:http://hi.baidu.com/saclrpqmttbntyq/item/4592fc72c5a19e5c0d0a07eb 由于总用 String.IsNullOrEmpty( s ) ...
- gcd 最大公约数 模版!
1: #define ll long long ll gcd(ll a,ll b) { ) { return b; }else { return gcd(b % a,a); } } 2: int64 ...
- poj 3268 Silver Cow Party(最短路,正反两次,这个模版好)
题目 Dijkstra,正反两次最短路,求两次和最大的. #define _CRT_SECURE_NO_WARNINGS //这是找出最短路加最短路中最长的来回程 //也就是正反两次最短路相加找最大的 ...