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
| #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std;
typedef long long ll; const int N=12; int n,m[N],b[N];
void exGCD(ll a,ll b,ll &x,ll &y) { if (!b) { x=1;y=0; return; } exGCD(b,a%b,y,x); y-=a/b*x; return; }
int main() { cin>>n; for (int i=1;i<=n;++i) scanf("%d %d",&m[i],&b[i]); ll M=1,res=0; for (int i=1;i<=n;++i) M*=m[i]; for (int i=1;i<=n;++i) { ll Mi=M/m[i],ti,x; exGCD(Mi,m[i],ti,x); res+=b[i]*Mi*ti; } printf("%lld\n",(res%M+M)%M); return 0; }
|