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' ...
随机推荐
- linux 命令小结
chkconfig --list 查询所有服务运行情况 修改文件夹权限: 在Linux中,权限的所有者分为用户权限,组权限和其他权限,分别是用字母u, g, o 代表权限分为:读 r , 写 w , ...
- ubuntu下格式化内存当硬盘使的小实验
内存虚拟硬盘(ramdisk)是指通过软件技术,将物理内存进行分割,将一部分内存通过虚拟技术转变为硬盘以较大幅度提升计算机数据读取速度和保护硬盘. 在ubuntu下的dev下有ram相关的文件,这些文 ...
- C# type - IsPrimitive
Type t = typeof(string); if (t.IsPrimitive)//not { Console.WriteLine("string is a Primitive&quo ...
- C# 延迟初始化
一个对象的延迟初始化意味着该对象的创建将会延迟至第一次使用该对象时.(在本主题中,术语“延迟初始化”和“延迟实例化”是同义词.)延迟初始化主要用于提高性能,避免浪费计算,并减少程序内存要求. 以下是最 ...
- .NET 轻松实现HTML的绝对过滤之SafeHelper
当今网页中经常使用到网页编辑器,因为人们需要在网页中插入图片,视频,样式等html代码内容,这使得网页的信息更加丰富.随之而来的,也给程序开发者带来了不少麻烦,因为提交的html中难免会出现不安全标记 ...
- VIM配置(转载)
注: 转载于http://www.cnblogs.com/ma6174/ 花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim配置主要有以下优点: 1.按F5可以直接编译并执行C.C++.ja ...
- Unit Test Generator
- Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi
Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi This spring, Kaggle hosted two competitions w ...
- 检测php网站是否已经被攻破的方法
0x01 查看访问日志 看是否有文件上传操作(POST方法), IPREMOVED - - [01/Mar/2013:06:16:48 -0600] "POST/uploads/monthl ...
- ZendStudio中设置SVN:ignore
使用ZendStudio开发SVN中的代码时,经常容易将 .project..settings..buildpath 这类的zend的工程文件提交上去,非常麻烦,有几种方法可以去掉这个麻烦. 1.在Z ...