题目链接

题意:给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)的更多相关文章

  1. POJ.2891.Strange Way to Express Integers(扩展CRT)

    题目链接 扩展中国剩余定理:1(直观的).2(详细证明). [Upd:]https://www.luogu.org/problemnew/solution/P4774 #include <cst ...

  2. POJ - 2891 Strange Way to Express Integers (扩展中国剩余定理)

    题目链接 扩展CRT模板题,原理及证明见传送门(引用) #include<cstdio> #include<algorithm> using namespace std; ty ...

  3. poj 2891 Strange Way to Express Integers (非互质的中国剩余定理)

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 9472   ...

  4. poj——2891 Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839 ...

  5. [POJ 2891] Strange Way to Express Integers

    Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10907 ...

  6. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

  7. [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)

    题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...

  8. poj 2891 Strange Way to Express Integers【扩展中国剩余定理】

    扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...

  9. 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' ...

随机推荐

  1. IIS7.5 HTTP 错误 500.19 - Internal Server Error 问题的解决方案

    昨天在 windows 7 下用 IIS 7.5 运行一个以前用 .NET Framework 3.5 写的项目,发现总是出现 500.19 错误,如下: 百度了好久,没找到解决问题确切的答案,我也知 ...

  2. Careercup - Facebook面试题 - 23594662

    2014-05-02 03:19 题目链接 原题: Given a sequence of numbers A() ..A(n), find the continuous subsequenceA(i ...

  3. android 开发解密时出现pad block corrupted 错误

    情景:在虚拟机上运行正常的,但是到我的真机上就解密失败,出现pad block corrupted  ,据说是版本原因:我机器是小米3 最新版的android  4.2 出现问题的代码: privat ...

  4. mac mysql安装

    一.安装 1.下载软件包直接安装即可: http://rj.baidu.com/soft/detail/25675.html?ald 安装完成后root默认密码为空: 二.修改密码 直接修改密码会提示 ...

  5. SQL SERVER时间函数

    本篇文章还是学习<程序员的SQL金典>内容的记录,此次将讲解的是SQL SERVER的时间函数. 本文只讲SQL SERVER支持的时间函数(其它数据库这里就不罗列了,想看更多的可以关注& ...

  6. jquery(1.3.2)<--json-->spring(3.0)

    发现spring 3已经对ajax支持的很好了,前端可以只使用html+jquery,后端 只使用spring再加上一种orm,两者之间用json交换数据就可以了,现在是放弃 jsp,struts这些 ...

  7. c++ 虚继承

    虚继承(个人感觉用到的地方不多,项目中没有用到这个的) 最典型的例子就是iostream的继承方式 class istream : virtual public ios{...};//此处就是虚继承, ...

  8. Eclipse改变外观,护眼模式

    1.Eclipse改变背景颜色 Windows menu --> Preference General -> Editors -> Text Editors(click),  在右下 ...

  9. Unity3D脚本中文系列教程(十三)

    http://dong2008hong.blog.163.com/blog/static/469688272014032334486/ Unity3D脚本中文系列教程(十二) ◆ function G ...

  10. Indent Guides VS 插件 对齐线