Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 16952 | Accepted: 5669 |
Description
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<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(ll x)
{if(x<0)x=-x,putchar('-');if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=1010;
int n;
ll a[N],m[N],x,y;
ll gcd(ll a,ll b)
{return !b?a:gcd(b,a%b);}
void exgcd(ll a,ll b)
{
if(b==0){x=1,y=0;return ;}
exgcd(b,a%b);
ll t=x;
x=y;y=t-a/b*y;
}
ll inv(ll a,ll b)
{
ll d=gcd(a,b);
if(d!=1)return -1;
exgcd(a,b);
return (x%b+b)%b;
}
bool merge(ll a1,ll m1,ll a2,ll m2,ll &a3,ll &m3)
{
ll d=gcd(m1,m2),c=a2-a1;
if(c%d)return 0;
c=(c%m2+m2)%m2;
m1/=d;m2/=d;c/=d;
c*=inv(m1,m2);
c%=m2;c*=m1*d;c+=a1;
m3=m1*m2*d;
a3=(c%m3+m3)%m3;
return 1;
}
ll CRT(ll a[],ll m[],int n)
{
ll a1=a[1],m1=m[1],a2,m2,a3,m3;
for(int i=2;i<=n;++i)
{
a2=a[i];m2=m[i];
if(!merge(a1,m1,a2,m2,a3,m3))return -1;
a1=a3;m1=m3;
}
return (a1%m1+m1)%m1;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;++i)scanf("%lld%lld",&m[i],&a[i]);
printf("%lld\n",CRT(a,m,n));
}
return 0;
}
/*
2
8 7
11 9
31
*/
注释版
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(ll x)
{if(x<0)x=-x,putchar('-');if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=1010;
int n;
ll a[N],m[N],x,y;
ll gcd(ll a,ll b)
{return !b?a:gcd(b,a%b);}
void exgcd(int a,int b)
{
if(b==0){x=1,y=0;return ;}
exgcd(b,a%b);
int t=x;
x=y;y=t-a/b*y;
}
ll inv(ll a,ll b)
{
exgcd(a,b);
return (x%b+b)%b;
}
bool merge(ll a1,ll m1,ll a2,ll m2,ll &a3,ll &m3)
{
ll d=gcd(m1,m2),c=a2-a1;
if(c%d)return 0;
c=(c%m2+m2)%m2;//保证c尽可能小,运算不报long long,下面的模运算含义相同
m1/=d;m2/=d;c/=d;
c*=inv(m1,m2);//求x
c%=m2;c*=m1*d;c+=a1;
m3=m1*m2*d;
a3=(c%m3+m3)%m3;
return 1;
}
ll CRT(ll a[],ll m[],int n)
{
ll a1=a[1],m1=m[1],a2,m2,a3,m3;
for(int i=2;i<=n;++i)
{
a2=a[i];m2=m[i];
if(!merge(a1,m1,a2,m2,a3,m3))return -1;//合并方程
a1=a3;m1=m3;
}
return (a1%m1+m1)%m1;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;++i)scanf("%lld%lld",&m[i],&a[i]);
printf("%lld\n",CRT(a,m,n));
}
return 0;
}
/*
2
8 7
11 9
31
*/