扩展中国剩余定理【模板】
其实扩展中国剩余定理与中国剩余定理的关系不大,后者使用类似拉格朗日插值法构造解,前者是模方程两两合并。
C++版
#include<iostream> #include<cstdio> #define LL long long //or __int128 using namespace std; const LL MAXN = 1e6 + 10; LL K, C[MAXN], M[MAXN], x, y; LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); } LL exgcd(LL a, LL b, LL &x, LL &y) { if (b == 0) {x = 1, y = 0; return a;} LL r = exgcd(b, a % b, x, y), tmp; tmp = x; x = y; y = tmp - (a / b) * y; return r; } LL inv(LL a, LL b) { LL r = exgcd(a, b, x, y); while (x < 0) x += b; return x; } int main() { while (~scanf("%lld", &K)) { for (LL i = 1; i <= K; i++) scanf("%lld%lld", &M[i], &C[i]); //x = C[i](mod M[i]) bool flag = 1; for (LL i = 2; i <= K; i++) { LL M1 = M[i - 1], M2 = M[i], C2 = C[i], C1 = C[i - 1], T = gcd(M1, M2); if ((C2 - C1) % T != 0) {flag = 0; break;} M[i] = (M1 * M2) / T; //可能爆long long C[i] = ( inv( M1 / T , M2 / T ) * (C2 - C1) / T ) % (M2 / T) * M1 + C1; C[i] = (C[i] % M[i] + M[i]) % M[i]; } printf("%lld\n", flag ? C[K] : -1); } return 0; }
个性签名:时间会解决一切