add bigint snippet

This commit is contained in:
edhlii 2025-04-11 17:38:49 +07:00
parent 1406e94956
commit aae1a8631d
2 changed files with 132 additions and 0 deletions

View File

@ -40,4 +40,136 @@ ls.add_snippets('cpp', {
'}', '}',
}, },
}), }),
s(
'bigint',
t {
'const ll BASE = 1000000000;',
'const int BASE_DIGITS = 9;',
'',
'// By edhlii and GPT :>',
'struct BigInt {',
' vector<long long> digits;',
' BigInt() {}',
' BigInt(long long v) {',
' while (v) {',
' digits.push_back(v % BASE);',
' v /= BASE;',
' }',
' }',
' BigInt(const string &s) { read(s); }',
' void read(const string &s) {',
' digits.clear();',
' int len = s.size();',
' for (int i = len; i > 0; i -= BASE_DIGITS) {',
' int start = max(0, i - BASE_DIGITS);',
' int length = i - start;',
' digits.push_back(stoll(s.substr(start, length)));',
' }',
' trim();',
' }',
' void trim() {',
' while (!digits.empty() && digits.back() == 0)',
' digits.pop_back();',
' }',
' friend ostream &operator<<(ostream &out, const BigInt &a) {',
' if (a.digits.empty()) {',
' out << 0;',
' return out;',
' }',
' out << a.digits.back();',
' for (int i = a.digits.size() - 2; i >= 0; --i)',
" out << setw(BASE_DIGITS) << setfill('0') << a.digits[i];",
' return out;',
' }',
' BigInt operator+(const BigInt &other) const {',
' BigInt res;',
' long long carry = 0;',
' size_t n = max(digits.size(), other.digits.size());',
' for (size_t i = 0; i < n || carry; ++i) {',
' long long sum = carry;',
' if (i < digits.size())',
' sum += digits[i];',
' if (i < other.digits.size())',
' sum += other.digits[i];',
' res.digits.push_back(sum % BASE);',
' carry = sum / BASE;',
' }',
' return res;',
' }',
' BigInt operator-(const BigInt &other) const {',
' BigInt res = *this;',
' long long carry = 0;',
' for (size_t i = 0; i < other.digits.size() || carry; ++i) {',
' res.digits[i] -= carry + (i < other.digits.size() ? other.digits[i] : 0);',
' carry = res.digits[i] < 0;',
' if (carry)',
' res.digits[i] += BASE;',
' }',
' res.trim();',
' return res;',
' }',
' BigInt operator*(const BigInt &other) const {',
' BigInt res;',
' res.digits.resize(digits.size() + other.digits.size());',
' for (size_t i = 0; i < digits.size(); i++) {',
' long long carry = 0;',
' for (size_t j = 0; j < other.digits.size() || carry; j++) {',
' long long cur =',
' res.digits[i + j] +',
' digits[i] * (j < other.digits.size() ? other.digits[j] : 0) + carry;',
' res.digits[i + j] = cur % BASE;',
' carry = cur / BASE;',
' }',
' }',
' res.trim();',
' return res;',
' }',
' bool operator<(const BigInt &other) const {',
' if (digits.size() != other.digits.size())',
' return digits.size() < other.digits.size();',
' for (int i = digits.size() - 1; i >= 0; i--) {',
' if (digits[i] != other.digits[i])',
' return digits[i] < other.digits[i];',
' }',
' return false;',
' }',
' bool operator<=(const BigInt &other) const { return !(other < *this); }',
' pair<BigInt, BigInt> divmod(const BigInt &other) const {',
' BigInt quotient, remainder;',
' quotient.digits.resize(digits.size());',
' for (int i = digits.size() - 1; i >= 0; i--) {',
' remainder.digits.insert(remainder.digits.begin(), digits[i]);',
' remainder.trim();',
' int low = 0, high = BASE - 1, mid, curDigit = 0;',
' while (low <= high) {',
' mid = (low + high) / 2;',
' BigInt t = other * BigInt(mid);',
' if (t <= remainder) {',
' curDigit = mid;',
' low = mid + 1;',
' } else {',
' high = mid - 1;',
' }',
' }',
' quotient.digits[i] = curDigit;',
' remainder = remainder - other * BigInt(curDigit);',
' }',
' quotient.trim();',
' remainder.trim();',
' return {quotient, remainder};',
' }',
' BigInt operator/(const BigInt &other) const { return divmod(other).first; }',
' BigInt operator%(const BigInt &other) const { return divmod(other).second; }',
' BigInt &operator+=(const BigInt &other) { return *this = *this + other; }',
' BigInt &operator-=(const BigInt &other) { return *this = *this - other; }',
' BigInt &operator*=(const BigInt &other) { return *this = *this * other; }',
' BigInt &operator/=(const BigInt &other) {',
' return *this = divmod(other).first;',
' }',
' BigInt &operator%=(const BigInt &other) {',
' return *this = divmod(other).second;',
' }',
'};',
}
),
}) })