求a1x1+r1=y...anxn+rn=y,crt合并

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#include<bits/stdc++.h>
#include<cstdio>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 998244353
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=400000+10,maxn=400000+10,inf=0x3f3f3f3f; ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b){x=1,y=0;return a;}
ll ans=exgcd(b,a%b,x,y);
ll t=x;x=y;y=t-a/b*y;
return ans;
}
int main()
{
ll a1,a2,r1,r2,x,y;int n;
while(~scanf("%d",&n))
{
scanf("%lld%lld",&a1,&r1);
bool f=1;
for(int i=2;i<=n;i++)
{
// printf("%lld\n%lld\n",a1,r1);
scanf("%lld%lld",&a2,&r2);
ll r=gcd(a1,-a2);
if((r2-r1)%r!=0){f=0;continue;}
exgcd(a1,-a2,x,y);
ll p=a2/gcd(a1,a2);
x*=(r2-r1)/r;x=(x%p+p)%p;
r1=a1*x+r1;
a1=a1/gcd(a1,a2)*a2;
r1%=a1;
}
if(!f)puts("-1");
else printf("%lld\n",r1);
}
return 0;
}
/********************
7
11323 9793
5537 4754
21538 10901
16118 203
2082 1209
22929 9510
16541 15898
********************/
05-11 19:31