您的位置:首页 > 其它

一元线性同余方程组【数论

2015-05-19 11:35 162 查看


http://poj.org/problem?id=2891

数论P56

#include "cstdio"
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
#define ll long long
ll exgcd(ll a,ll b,ll &x, ll & y)
{
if(b==0){
x=1;y=0;return a;
}
ll d=exgcd(b,a%b,x,y);
ll t=x;x=y;y=t-a/b*y;
return d;
}
int main()
{
int k;
while(cin>>k){
k--;
bool have_ans=1;
ll a,b,d,c,x,y,b1,m1,b2,m2,m;
cin>>m1>>b1;
while(k--){
cin>>m2>>b2;
a=m1,b=m2,c=b2-b1;
d=exgcd(a,b,x,y);
if(c%d) have_ans=0;
ll s=b/d;
x=x*(c/d);
x=(x%s+s)%s;
b1=x*m1+b1;
m1=m1*m2/d;
}
if(have_ans)cout<<b1<<endl;
else cout<<-1<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数论