poj2891线性同余方程

解线性同余方程组的迭代法
x=r1(mod a1)
x=r2(mod a2)
 
x=a1*t+r1
x=a2*u+r2
a1*t+a2*u=r2-r1
解u,t
令d=(a1,a2) c=r2-r1
当仅当d|c时有解
用extgcd解得a1*t'+a2*u'=d 中的t'和u'
a1*(t'*c/d) + a2*(u'*c/d) = c
解是t=t'/d*c 和 u=u'/d*c
 
然后把t代入x=a1*t+r1
x=x(mod lcm(a1,a2))
然后再递推解第三个以及后面的方程

poj2891

/*
ID: neverchanje
PROG:
LANG: C++11
*/
#include<vector>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<set>
#include<queue>
#include<map>
#define INF 0Xfffffffff
#define st_size (1<<18)-1
#define maxn
typedef  long long ll;
using namespace std;

void extgcd(ll a,ll b,ll& d,ll& x,ll& y){
    if(!b){    d=a;x=1;y=0; }
    else{
        extgcd(b,a%b,d,y,x);
        y-=(a/b)*x;
    }
}

int n;
ll a1,a2,r1,r2;
ll c,d,x,y,t;

int main(){
//    freopen("a.txt","r",stdin);
//    freopen(".out","w",stdout);
    while(cin>>n){
        cin>>a1>>r1;
        bool flag=1;
        for(int i=1;i<n;i++)
        {
            cin>>a2>>r2;
            //对x=r1(mod a1)    x=r2(mod a2)的联立方程组
            c=r2-r1;
            extgcd(a1,a2,d,x,y);
            if(c%d)    flag=0;//无解,注意无解也不能退出循环

            //得到a1*x+a2*y=c中x的解
            t=a2/d;
            x=((x*(c/d))%t+t)%t;//注意
            r1=a1*x+r1;
            a1=(a1*a2)/d;
        }

        printf("%lld\n",flag?r1:-1);
    }
    return 0;
}

/*
DESCRIPTION:

*/

 

 

转载于:https://www.cnblogs.com/neverchanje/p/3755211.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值