// Returns in r the solution of x == r[k] (mod m[k]), k = 0, ..., n-1 void crt (rawtype *r, rawtype *m, size_t n) { size_t t, k; rawtype *mm = new rawtype[n], *x = new rawtype[n], *s = new rawtype[n]; modint y; rawtype o; o = modint::modulus; mm[0] = m[0]; for (t = 1; t < n; t++) mm[t] = bigmul (mm, mm, m[t], t); for (t = 0; t < n; t++) s[t] = 0; for (k = 0; k < n; k++) { setmodulus (m[k]); y = 1; for (t = 0; t < n; t++) if (t != k) y *= m[t]; y = r[k] / y; bigdiv (x, mm, m[k], n); x[n - 1] = bigmul (x, x, y, n - 1); if (bigadd (s, x, n) || bigcmp (s, mm, n) > 0) bigsub (s, mm, n); } moveraw (r, s, n); setmodulus (o); delete[] mm; delete[] x; delete[] s; }
// Adds n words from s to d, returns overflow carry bit rawtype baseadd (rawtype *dest, rawtype *src1, rawtype *src2, size_t len, rawtype carry) { size_t t; for (t = len; t--;) { dest[t] = src1[t]; bigadd (dest + t, &carry, 1); if ((src2 && bigadd (dest + t, src2 + t, 1) != 0) || bigcmp (dest + t, &Base, 1) >= 0) { bigsub (dest + t, &Base, 1); carry = 1; } else { carry = 0; } } return carry; }
bool operator ==(const Bigint& num1, const Bigint& num2) { if(bigcmp(num1, num2) ==0) return true; return false; }