现在方程是x=b(mod c)
pi.first = c, pi.second = b;
代码按照第一篇题解思路模拟而来/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define int long long
using namespace std;
inline int read(){
register int x = 0, f = 1; register char ch = 0;
while(!isdigit(ch)){
if(ch == '-') f = -1;
ch = getchar();
}
while(isdigit(ch)){
x = (x << 1) + (x << 3) + (ch ^ '0');
ch = getchar();
}
return x * f;
}
int n;
vector<pair<int, int> > vec;
int gcd(int a, int b){
return !b ? a : gcd(b, a % b);
}
void exgcd(int a, int b, int &x, int &y){
if(!b) {
x = 1; y = 0; return;
}
exgcd(b, a % b, y, x); y -= (a / b) * x;
}
signed main(){
n = read();
for(int i = 1; i <= n; ++ i){
pair<int,int> pi;
pi.first = read(); pi.second = read();
vec.push_back(pi);
}
while(vec.size() != 1){
pair<int, int>pi1, pi2;
pi1 = vec.back(); vec.pop_back();
pi2 = vec.back(); vec.pop_back();
int h = gcd(pi1.first, pi2.first);
// cout << pi1.first << ' ' << pi1.second << endl << pi2.first << ' ' << pi2.second << endl;
int p1 = pi1.first / h, p2 = pi2.first / h;
int r = (pi2.second - pi1.second) / h;
int lcm = p1 * p2 * h;
int k1, k2, x;
exgcd(p1, p2, k1, k2);
x = ((((k1 % p2 + p2) % p2 * r % lcm) % lcm * pi1.first % lcm + pi1.second) % lcm + lcm) % lcm;
// cout << x << endl;
pi1.first = lcm;
pi1.second = x;
vec.push_back(pi1);
}
pair<int ,int> pi = vec.back(); int x, y;
exgcd(1, pi.first, x, y);
cout << pi.second;
}