templates/big-integer.cpp
compilable
$cat templates/big-integer
Big Integer
Non-negative arbitrary-precision integer with addition, subtraction, multiplication, division, and modulo.
#bigint#arbitrary-precision#big-integer#math
Log in to track progress, save custom versions, and organize into collections.Log In
$cat source_code/
cpp
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct BigInt {
static const int BASE = 1000000000;
vector<int> v;
BigInt() {}
BigInt(ll val) { *this = val; }
BigInt(const string &s) { *this = s; }
int size() const { return (int)v.size(); }
bool zero() const { return v.empty(); }
ll val() const {
ll ans = 0;
for (int i = (int)v.size() - 1; i >= 0; i--)
ans = ans * BASE + v[i];
return ans;
}
void strip() { while (!v.empty() && v.back() == 0) v.pop_back(); }
BigInt &operator=(ll val) {
v.clear();
for (; val > 0; val /= BASE) v.push_back(val % BASE);
return *this;
}
BigInt &operator=(const string &s) {
v.clear();
for (int i = (int)s.size() - 1; i >= 0; i -= 9) {
int st = max(0, i - 8);
v.push_back(stoi(s.substr(st, i - st + 1)));
}
strip();
return *this;
}
bool operator<(const BigInt &a) const {
if (size() != a.size()) return size() < a.size();
for (int i = size() - 1; i >= 0; i--)
if (v[i] != a.v[i]) return v[i] < a.v[i];
return false;
}
bool operator>(const BigInt &a) const { return a < *this; }
bool operator==(const BigInt &a) const { return v == a.v; }
bool operator<=(const BigInt &a) const { return !(a < *this); }
bool operator>=(const BigInt &a) const { return !(*this < a); }
BigInt operator+(const BigInt &a) const {
BigInt res = *this;
int carry = 0;
for (int i = 0; i < (int)a.v.size() || carry; i++) {
if (i == (int)res.v.size()) res.v.push_back(0);
ll cur = res.v[i] + carry + (i < (int)a.v.size() ? a.v[i] : 0);
res.v[i] = cur % BASE;
carry = cur / BASE;
}
return res;
}
BigInt &operator+=(const BigInt &a) { return *this = *this + a; }
BigInt operator-(const BigInt &b) const {
BigInt res = *this;
int borrow = 0;
for (int i = 0; i < (int)b.v.size() || borrow; i++) {
ll cur = res.v[i] - borrow - (i < (int)b.v.size() ? b.v[i] : 0);
if (cur < 0) { cur += BASE; borrow = 1; }
else borrow = 0;
res.v[i] = cur;
}
res.strip();
return res;
}
BigInt &operator-=(const BigInt &b) { return *this = *this - b; }
BigInt operator*(const BigInt &a) const {
if (zero() || a.zero()) return BigInt(0);
BigInt res;
res.v.resize(size() + a.size(), 0);
for (int i = 0; i < size(); i++) {
if (!v[i]) continue;
ll carry = 0;
for (int j = 0; j < (int)a.v.size() || carry; j++) {
ll cur = res.v[i + j] + carry + 1LL * v[i] * (j < (int)a.v.size() ? a.v[j] : 0);
res.v[i + j] = cur % BASE;
carry = cur / BASE;
}
}
res.strip();
return res;
}
BigInt &operator*=(const BigInt &a) { return *this = *this * a; }
BigInt &operator/=(ll a) {
ll carry = 0;
for (int i = (int)v.size() - 1; i >= 0; i--) {
ll cur = v[i] + carry * BASE;
v[i] = cur / a;
carry = cur % a;
}
strip();
return *this;
}
BigInt operator/(ll a) const { BigInt res = *this; res /= a; return res; }
BigInt operator%(ll a) const {
ll res = 0;
for (int i = (int)v.size() - 1; i >= 0; i--)
res = (res * BASE + v[i]) % a;
return BigInt(res);
}
BigInt &operator%=(ll a) { return *this = *this % a; }
friend ostream &operator<<(ostream &out, const BigInt &a) {
if (a.zero()) return out << 0;
out << a.v.back();
for (int i = (int)a.v.size() - 2; i >= 0; i--)
out << setfill('0') << setw(9) << a.v[i];
return out;
}
friend istream &operator>>(istream &in, BigInt &a) {
string s; in >> s; a = s; return in;
}
};128 linesutf-8
$cat explanation_notes.md
notes_viewer --renderedmarkdown (math enabled)
Big Integer
Arbitrary-precision non-negative integer stored in base (little-endian). Each "digit" holds up to 9 decimal digits.
Operations
long long, from decimal string, or default (zero)<, >, ==, <=, >=+, -, +=, -=. Subtraction throws if result would be negative, = using schoolbook algorithm/, %, /=, %= by long long only (not BigInt-by-BigInt)cin >> x and cout << x work directlyWhen to Use
long long, or problems that explicitly state "output can have thousands of digits"Complexity
ll —