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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| #include<algorithm> #include<bitset> #include<complex> #include<deque> #include<exception> #include<fstream> #include<functional> #include<iomanip> #include<ios> #include<iosfwd> #include<iostream> #include<istream> #include<iterator> #include<limits> #include<list> #include<locale> #include<map> #include<memory> #include<new> #include<numeric> #include<ostream> #include<queue> #include<set> #include<sstream> #include<stack> #include<stdexcept> #include<streambuf> #include<string> #include<typeinfo> #include<utility> #include<valarray> #include<vector> #include<cctype> #include<cerrno> #include<cfloat> #include<ciso646> #include<climits> #include<clocale> #include<cmath> #include<csetjmp> #include<csignal> #include<cstdarg> #include<cstddef> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #define int long long using namespace std; inline int read(){ int res=0,f=1;char ch=getchar(); while(ch<'0'ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar(); return res*f; } inline void write(int x){ if(x<0) putchar('-'),x=-x; if(x<10) putchar(x+'0'); else{ write(x/10); putchar(x%10+'0'); } } void exgcd(int a,int b,int &d,int &x,int &y){ int t=0; if(b==0) d=a,x=1,y=0; else{ exgcd(b,a%b,d,x,y); t=x;x=y;y=t-(a/b)*y; } } int n,m[100010],a[100010],M=1,t[100010],ans; int gcd(int a,int b){ return !b?a:gcd(b,a%b); } pair<int,int> solvefunction(int a,int b,int c){ int g=gcd(a,b),a1=a/g,b1=b/g,c1=c/g,x1,y1,d; exgcd(a1,b1,d,x1,y1); return make_pair(x1,y1); } namespace Function{ void solve(){ for(int i=2,x,y,d;i<=n;i++){ int C=m[i]-m[1],B=a[i],A=a[1],t=gcd(A,B); if(C%t!=0){ puts("-1"); return ; } A/=t;B/=t;C/=t; exgcd(A,B,d,x,y); x=((C*x)%B+B)%B; m[1]=m[1]+a[1]*x; a[1]=a[1]*B; } write(m[1]);putchar('\n');return ; } } signed main(){ while(cin>>n){ for(int i=1;i<=n;i++) a[i]=read(),m[i]=read(); Function::solve(); } return 0; }
|