正文
增加 24 行删除 24 行
137 行未改动全部展开13813914014114214314414514614714814915015115215315415515615715815916016113 行未改动1751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132143 行未改动21821922022122222322422522622722822923023123223323423536 行未改动27227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031196 行未改动40840941041141241341441541641741841942042142242342442542642739 行未改动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 +(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 行未改动137 行未改动全部展开13813914014114214314414514614714814915015115215315415515615715815916016113 行未改动1751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132143 行未改动21821922022122222322422522622722822923023123223323423536 行未改动27227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031196 行未改动40840941041141241341441541641741841942042142242342442542642739 行未改动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 +(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 行未改动