strange way to express integers
#include<iostream>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
typedef long long ll;
void gcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(!b)
{
d=a;
x=1;
y=0;
}
else
{
gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
ll china(ll n,ll a[],ll b[])
{
ll m1=a[0];
ll r1=b[0];
ll flag=0;
ll d;
for(ll i=1;i<n;i++)
{
ll m2=a[i];
ll r2=b[i];//余数
if(flag)
continue;
ll x,y;
gcd(m1,m2,d,x,y);
ll c=r2-r1;
if(c%d)
{
flag=1;
continue;
}
ll t=m2/d;
x=(c/d*x%t+t)%t;
r1=m1*x+r1;
m1=m1*m2/d;
}
if(flag)
return -1;//无解输出-1
if(n==1&&r1==0)
return m1;
return r1;
}
ll a[55000],b[55000];
int main()
{
ll k,i;
while(~scanf("%lld",&k))
{
for(i=0;i<k;i++)
scanf("%lld%lld",&a[i],&b[i]);
cout<<china(k,a,b)<<endl;
}
}
Hello KiKi也是不互质的中国剩余定理