__int128 exgcd(__int128 a, __int128 b, __int128 &x, __int128 &y){ if (b == 0) { x = 1, y = 0; return a; } __int128 d = exgcd(b, a % b, x, y); __int128 t = x; x = y; y = t - a / b * y; return d; }
signedmain(){ int tmp; cin >> tmp; n = tmp; __int128 as = 1; for (int i = 1; i <= n; i++) { cin >> tmp; a[i] = tmp; cin >> tmp; b[i] = tmp; as = as * a[i]; } for (int i = 1; i <= n; i++) { __int128 mi = as / a[i], b, y; exgcd(mi, a[i], b, y); ans = (ans + ::b[i] * mi * b) % as; } cout << (int) ((ans % as + as) % as); }