1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include<iostream> #include<cstdio> using namespace std; long long read() { long long x=0,f=1;char c=getchar(); while(!isdigit(c)){if(c=='-') f=-1;c=getchar();} while(isdigit(c)){x=x*10+c-'0';c=getchar();} return x*f; } long long take(long long A,long long B,long long poi) { if(B==0) return 0; long long temp=take(A,B/2,poi)*2%poi; if(B%2==1) temp=(temp+A)%poi; return temp; } long long exgcd(long long A,long long B,long long &x,long long &y) { if(B==0) { x=1,y=0; return A; } long long t_ans=exgcd(B,A%B,x,y),tx=x; x=y,y=tx-(A/B)*y; return t_ans; }
const int N=100000+100; int n; long long a[N],p[N]; int main() { n=read(); for(int i=1;i<=n;i++) p[i]=read(),a[i]=read();
long long A=a[1],P=p[1]; for(int i=2;i<=n;i++) { long long x,y,gcd=exgcd(P,p[i],x,y); long long t_P=P,t=p[i]/gcd; x=(x%t+t)%t,x=take(x,(((a[i]-A)/gcd)%t+t)%t,t); P=P*(p[i]/gcd),A=(A+take(t_P,x,P))%P; }
printf("%lld",(A%P+P)%P); return 0; }
|