Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:
Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.
“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”
Since Elina is new to programming, this problem is too difficult for her. Can you help her?
Input
The input contains multiple test cases. Each test cases consists of some lines.
- Line 1: Contains the integer k.
- Lines 2 ~ k + 1: Each contains a pair of integers ai, ri (1 ≤ i ≤ k).
Output
Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.
Sample Input
2
8 7
11 9
Sample Output
31
Hint
All integers in the input and the output are non-negative and can be represented by 64-bit integral types.
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<b;i++)
const int maxn=1e5+5;
ll m[maxn],r[maxn];
void exgcd(ll a,ll b,ll&d,ll&x,ll&y){
if(!b){d=a;x=1,y=0;}
else{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
//d是负数也没有关系的,最后都会转化成正数
ll cal(ll a,ll b,ll c,ll&d){
ll x,y;
exgcd(a,b,d,x,y);
if(c%d)return -1;
x*=c/d;
b/=d;
if(b<0)b=-b;
x=x%b;
if(x<0)x+=b;
return x;
}
/*
考虑
X = r1 % m1
X = r2 % m2
易得
X - m1*Y = r1
X - m2*Y = r2
则
m1*Y+r1=m2*Y+r2
m1*Y = r2-r1 + m2*Y
两边同时mod m2
m1*Y =r2-r1 (%m2)
可以解出 Y 的最小正值,然后回带
X = m1*(Y+n*(m2/d))+r1
= m1*Y+r1+ n*(m1*m2/d)
注:d=gcd(m1,m2)
所以可以看出 原先的两个方程
又可以化为
X = m1*Y+r1 (mod(m1*m2/d))
*/
int main(){
int n;
while(scanf("%d",&n)==1){
rep(i,0,n)scanf("%lld %lld",&m[i],&r[i]);
ll d,x,y,ans;
int wrong=0;
ll M=m[0],R=r[0],a,b,c;
rep(i,1,n){
a=M,b=m[i];
x=cal(a,b,r[i]-R,d);
if(x==-1){
wrong=1;break;
}
R=(M*x+R);
M=M*m[i]/d;
}
ans=wrong?-1:R;
printf("%lld\n",ans);
}
return 0;
}
Hello Kiki(hdu 3579)
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<b;i++)
const int maxn=110;
ll m[maxn],r[maxn];
void exgcd(ll a,ll b,ll&d,ll&x,ll&y){
if(!b){d=a;x=1,y=0;}
else{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
//d是负数也没有关系的,最后都会转化成正数
ll cal(ll a,ll b,ll c,ll&d){
ll x,y;
exgcd(a,b,d,x,y);
if(c%d)return -1;
x*=c/d;
b/=d;
if(b<0)b=-b;
x=x%b;
if(x<0)x+=b;
return x;
}
/*
考虑
X = r1 % m1
X = r2 % m2
易得
X - m1*Y = r1
X - m2*Y = r2
则
m1*Y+r1=m2*Y+r2
m1*Y = r2-r1 + m2*Y
两边同时mod m2
m1*Y =r2-r1 (%m2)
可以解出 Y 的最小正值,然后回带
X = m1*(Y+n*(m2/d))+r1
= m1*Y+r1+ n*(m1*m2/d)
注:d=gcd(m1,m2)
所以可以看出 原先的两个方程
又可以化为
X = m1*Y+r1 (mod(m1*m2/d))
*/
int main(){
int T;
scanf("%d",&T);
rep(kase,0,T){
int n;
scanf("%d",&n);
rep(i,0,n)scanf("%lld",&m[i]);
rep(i,0,n)scanf("%lld",&r[i]);
ll d,x,y,ans;
int wrong=0;
ll M=m[0],R=r[0],a,b,c;
rep(i,1,n){
a=M,b=m[i];
x=cal(a,b,r[i]-R,d);
if(x==-1){
wrong=1;break;
}
R=(M*x+R);
M=M*m[i]/d;
}
if(R==0){
R=1;
rep(i,0,n){
R=R*m[i]/__gcd(R,m[i]);
}
}
ans=wrong?-1:R;
printf("Case %d: %lld\n",kase+1,ans);
}
return 0;
}