POJ 2891 Strange Way to Express Integers (解一元线性方程组)
求解一元线性同余方程组:
x=ri(mod ai) i=1,2,...,k
解一元线性同余方程组的一般步骤:
先求出前两个的解,即:
x=r1(mod a1) 1
x=r2(mod a2) 2
1式等价于x=r1+a1*m,2式等价于x=r2+a2*n
联立可得:m*a1-n*a2=r2-r1=c
若方程有解,则必须(a1,a2)|c
设d=(a1,a2),那么如果有解,即可求得
m*a1-n*a2=d的解,m=m'
则 m*a1-n*a2=c的解,m0=m'*c/d
通解m*=m0+(a2/d)*i
令s=a2/d,则可以求得m的最小解
m=(m0%s+s)%s
带入x=r1+a1*m,即可求出解x,设为x0
接下来,由
x=r1(mod a1) 1
x=r2(mod a2) 2
得出x=x0=r1(mod a1),x=x0=r2(mod a2)
由定理可得,x=x0(mod [a1,a2])
设a=lcm(a1,a2),即可将1式和2式合并成一项。
接下来的就重复上面步骤即可。
#include <iostream>
#include <stdio.h> using namespace std;
int k;
//求解方程ax+by=gcd(a,b)
long long exgcd(long long a,long long b,long long &x,long long &y){
if(b==){
x=;
y=;
return a;
}
long long g=exgcd(b,a%b,x,y);
long long tmp=x;
x=y;
y=tmp-(a/b)*y;
return g;
}
int main()
{
long long a1,a2,r1,r2,d,c;
long long x,y,x0;
long long s; //s=a2/d;
while(scanf("%d",&k)!=EOF){
bool flag=true;
scanf("%I64d%I64d",&a1,&r1);
for(int i=;i<k;i++){
scanf("%I64d%I64d",&a2,&r2);
if(!flag)
continue;
/*
解x=r1(mod a1)
x=r2(mod a2)
相当于
解不定方程:x*a1+y*a2=r2-r1
先求解方程:x*a1+y*a2=r2-r1=gcd(a1,a2)
得出解x,则方程x*a1+y*a2=r2-r1的解x0=x*(r2-r1)/gcd(a1,a2)=x*c/d
令s=a2/d,那么
x0的最小解为:x0=(x0%s+s)%s
即可得出解x=r1+x0*a1
然后将x赋值给r1,lcm(a1,a2)赋值给a1,继续求解。
*/
c=r2-r1;
d=exgcd(a1,a2,x,y);
if(c%d!=){
flag=false;
continue;
}
x0=x*c/d;
s=a2/d;
x0=(x0%s+s)%s;
r1=r1+x0*a1;
a1=a1*a2/d;
}
if(flag)
printf("%I64d\n",r1);
else
printf("-1\n");
}
return ;
}
POJ 2891 Strange Way to Express Integers (解一元线性方程组)的更多相关文章
- 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 | exGcd解同余方程组
题面就是让你解同余方程组(模数不互质) 题解: 先考虑一下两个方程 x=r1 mod(m1) x=r2 mod (m2) 去掉mod x=r1+m1y1 ......1 x=r2+m2y2 . ...
- poj 2891 Strange Way to Express Integers(中国剩余定理)
http://poj.org/problem?id=2891 题意:求解一个数x使得 x%8 = 7,x%11 = 9; 若x存在,输出最小整数解.否则输出-1: ps: 思路:这不是简单的中国剩余定 ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理MOD不互质数字方法
http://poj.org/problem?id=2891 711323 97935537 475421538 1090116118 2032082 120922929 951016541 1589 ...
- POJ 2891 Strange Way to Express Integers 中国剩余定理 数论 exgcd
http://poj.org/problem?id=2891 题意就是孙子算经里那个定理的基础描述不过换了数字和约束条件的个数…… https://blog.csdn.net/HownoneHe/ar ...
- [poj 2891] Strange Way to Express Integers 解题报告(excrt扩展中国剩余定理)
题目链接:http://poj.org/problem?id=2891 题目大意: 求解同余方程组,不保证模数互质 题解: 扩展中国剩余定理板子题 #include<algorithm> ...
随机推荐
- openshift云计算平台diy模式安装Python2.7+Flask
主要翻译了链接1)的教程,加上一些个人研究,步骤如下: 1) 在openshift.redhat.com申请账号,安装git for windows,然后安装gem install rhc,这些比较容 ...
- linq to sql (Group By/Having/Count/Sum/Min/Max/Avg操作符)
Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式: var q = from p in ...
- hdu 3074 Multiply game
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3074 Minimum Inversion Number Description Tired of pl ...
- Douglas Crockford: entityify & deentityify
大神之字符与字符实体的相互转换方法 // & to & if (!String.prototype.entityify) { String.prototype.entityify = ...
- DataReader 链接关闭解惑篇
不管是啥xxDataReader,都是继承DataReader实现的,所以是有共性的,因此标题就以DataReader为题了. 情况一:DataReader 默认链接不关闭 static void M ...
- C#.Net 图片处理大全
C# How to: Image filtering by directly manipulating Pixel ARGB values C# How to: Image filtering imp ...
- Question about pairing/bonding?
Except that on android you can bypass the pairing dialog if you know the PIN in advance through a di ...
- 微信扫码支付asp.net(C#)实现步骤
支付提交页面: [HttpPost] public ActionResult index(decimal amount) { //生成订单10位序列号,此处用时间和随机数生成,商户根据自己调整,保证唯 ...
- SQLiteAPI函数详解
使用的过程根据使用的函数大致分为如下几个过程: sqlite3_open() sqlite3_prepare() sqlite3_step() sqlite3_column() sqlite3_fin ...
- Z_blog博客尝试 http://www.uuxin.com/
原来的博客由于没有备份所有数据全部丢失,很是郁闷. 又用Z-BLOG新建了一个博客.http://www.uuxin.com