原题
它是一道假的中国剩余定理,因为模数不互质,所以我们只能ex_gcd将方程逐一合并最后求出满足所有的答案
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 100010
typedef long long ll;
using namespace std;
ll n,m[N],r[N];
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if (b==0) return x=1,y=0,a;
ll r=exgcd(b,a%b,y,x);
y-=a/b*x;
return r;
}
ll solve()
{
ll M=m[1],R=r[1],x,y,d;
for (int i=2;i<=n;i++)
{
d=exgcd(M,m[i],x,y);
if ((R-r[i])%d!=0) return -1;
x=(R-r[i])/d*x%m[i];
R-=x*M;
M=M/d*m[i];
R%=M;
}
return (R%M+M)%M;
}
int main()
{
while (scanf("%lld",&n)!=EOF)
{
for (int i=1;i<=n;i++)
scanf("%lld%lld",&m[i],&r[i]);
printf("%lld
",solve());
}
return 0;
}