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
|
using namespace std; typedef long long LL; const int maxn = 15; LL n, r[maxn], m[maxn]; LL (LL a, LL b, LL &x, LL &y) { if(b == 0LL) {x = 1LL; y = 0LL; return a;} LL res = ext_gcd(b, a % b, x, y); LL tmp = x; x = y, y = tmp - a / b * y; return res; } LL quick_mul(LL a, LL b, LL mod){ LL res = 0LL; while(b) { if(b & 1) res =(res + a) % mod; a = (a + a) % mod; b >>= 1; } return res; }
LL crt(LL *r, LL *m, LL n) { LL lcm = 1LL, res = 0LL, M_i, x, y; for(LL i = 0; i < n; ++i) lcm *= m[i]; for(LL i = 0; i < n; ++i) { M_i = lcm / m[i]; ext_gcd(M_i, m[i], x, y); x = (x % m[i] + m[i]) % m[i]; res = (res + quick_mul(quick_mul(r[i], M_i, lcm), x, lcm)) % lcm; } return res; }
|
近期评论