版本比较
DS的高精板子
正文
增加 24 行删除 24 行
137 行未改动全部展开13813914014114214214314414514514614714814915015015115215315415515615715715815916016113 行未改动1751761771781791791801811821821831841851861861871881891901911911921931941951961961971981992002012022032032042052052062072082092102102112122132143 行未改动21821922022122222222322422522622722722822923023123123223323423536 行未改动27227327427527627627727827928028128228228328428528628728828929029029129229329429529629729729829930030130130230330430530630730730830931031196 行未改动40840941041141241241341441541541641741841942042142242342342442542642739 行未改动137 行未改动全部展开
// 使用NTT进行乘法 BigInt mul_ntt(const BigInt& other) const { if (digits.empty() || other.digits.empty()) return BigInt(0);
vector<ll> a(digits.begin(), digits.end()); vector<ll> b(other.digits.begin(), other.digits.end());
int n = 1; while (n < a.size() + b.size()) n <<= 1; a.resize(n); b.resize(n);
ntt(a, false); ntt(b, false); for (int i = 0; i < n; i++) { a[i] = a[i] * b[i] % NTT_MOD; } ntt(a, true);
BigInt res; res.digits.resize(n); ll carry = 0; for (int i = 0; i < n; i++) {13 行未改动 static pair<BigInt, BigInt> divide_mod(const BigInt& a, const BigInt& b) { if (b.is_zero()) { throw runtime_error("Division by zero"); }
BigInt abs_a = a.abs(); BigInt abs_b = b.abs();
if (abs_a < abs_b) { return make_pair(BigInt(0), a); }
BigInt quotient, remainder; int n = abs_a.digits.size(), m = abs_b.digits.size(); int len = n - m + 1; quotient.digits.resize(len);
// 规范化除数 int d = BASE / (abs_b.digits.back() + 1); BigInt norm_a = abs_a * d; BigInt norm_b = abs_b * d;
remainder = norm_a; for (int i = len - 1; i >= 0; i--) { // 估计商的值 long long prefix = remainder.get_digit_range(max(i + m - n, 0), n - i); int q = prefix / norm_b.digits.back(); int r = prefix % norm_b.digits.back();
// 调整商的值 while (r < BASE && (q == BASE || (long long)q * norm_b.digits[m-2] > (long long)BASE * r + while (r < BASE && (q == BASE || (long long)q * norm_b.digits[m-2] > (long long)BASE * r + (i + m - 2 < remainder.digits.size() ? remainder.digits[i + m - 2] : 0))) { q--; r += norm_b.digits.back(); }
// 减法 BigInt temp = norm_b * q; temp.shift_left(i); if (remainder < temp) {3 行未改动 } remainder -= temp; quotient.digits[i] = q; }
quotient.trim(); remainder.trim(); // 计算余数 remainder = remainder / d;
// 设置符号 quotient.sign = a.sign != b.sign; remainder.sign = a.sign;
return make_pair(quotient, remainder); }
// 获取数字范围 [start, end) 的值36 行未改动 // 从字符串解析 void from_string(const string& s) { digits.clear(); sign = false;
int start = 0; if (s[0] == '-') { sign = true; start = 1; }
for (int i = s.size() - 1; i >= start; i -= BASE_DIGITS) { int digit = 0; for (int j = max(start, i - BASE_DIGITS + 1); j <= i; j++) { digit = digit * 10 + (s[j] - '0'); } digits.push_back(digit); }
trim(); }
// 转换为字符串 string to_string() const { if (digits.empty()) return "0";
string res; if (sign) res += "-"; res += std::to_string(digits.back());
for (int i = digits.size() - 2; i >= 0; i--) { string digit_str = std::to_string(digits[i]); // 前导零填充 res += string(BASE_DIGITS - digit_str.size(), '0') + digit_str; }
return res; }
// 判断是否为零96 行未改动 // 快速幂 BigInt pow(int exponent) const { if (exponent == 0) return BigInt(1); if (exponent == 1) return *this;
BigInt base = *this; BigInt result(1);
while (exponent > 0) { if (exponent & 1) { result = result * base; } base = base * base; exponent >>= 1; }
return result; }
// 输出运算符39 行未改动