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
|
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector>
#define FILE_IN(__fname) freopen(__fname, "r", stdin) #define FILE_OUT(__fname) freopen(__fname, "w", stdout) #define rap(a,s,t,i) for (int a = s; a <= t; a += i) #define basketball(a,t,s,i) for (int a = t; a > s; a -= i) #define countdown(s) while (s --> 0) #define IMPROVE_IO() std::ios::sync_with_stdio(false)
using std::cin; using std::cout; using std::endl;
typedef long long int lli;
int getint() { int x; scanf("%d", &x); return x; } lli getll() { long long int x; scanf("%lld", &x); return x; }
lli k, a[10000 + 10], m[10000 + 10];
namespace ChinaRemainderTheorem { lli exgcd(lli a, lli b, lli &x, lli &y) { if (b == 0) { x = 1; y = 0; return a; } lli g = exgcd(b, a % b, y, x); y -= a / b * x; return g; } lli CRT() { lli X = 0, M = 1; for (lli i = 1; i <= k; ++i) M *= m[i]; for (lli i = 1; i <= k; ++i) { lli ti = 0, y = 0; lli mmi = M / m[i]; exgcd(mmi, m[i], ti, y); X = ((X + a[i] * mmi * ti) % M + M) % M; } return X < 0 ? (X + M) : X; } }
signed main() { k = getll(); rap (i, 1, k, 1) { m[i] = getll(); a[i] = getll(); } printf("%lld\n", ChinaRemainderTheorem::CRT()); return 0; }
|