代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
void fast_io();
const int MAXN = 1e5+5;
int n;
LL a[MAXN],b[MAXN];
LL mod(LL a, LL b);
LL lcm(LL a, LL b);
void exgcd(LL a, LL b, LL &x, LL &y);
LL excrt() {
LL d, p1, p2, lambda1, lambda2;
b[0] = mod(b[0], a[0]);
for (int i = 0; i < n - 1; i++)
{
d = __gcd(a[i], a[i + 1]);
p1 = a[i] / d;
p2 = a[i + 1] / d;
exgcd(p1, p2, lambda1, lambda2);
b[i + 1] = b[i] + (b[i + 1] - b[i]) / d * lambda1 * a[i];
a[i + 1] = lcm(a[i], a[i + 1]);
b[i + 1] = mod(b[i + 1], a[i + 1]);
}
return b[n - 1];
}
int main() {
fast_io();
cin>>n;
for (int i = 0; i < n; i++)
cin>>a[i]>>b[i];
cout<<excrt();
return 0;
}
void fast_io() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
LL mod(LL a, LL b) {
return (a%b+b)%b;
}
LL lcm(LL a, LL b) {
return a/__gcd(a, b)*b;
}
void exgcd(LL a, LL b, LL &x, LL &y) { //求解 ax+by=gcd(a,b)
if (b == 0) {
x = 1;
y = 0;
return;
}
exgcd(b, a % b, y, x);
y -= a / b * x;
}
记录:记录