-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigInt.cpp
More file actions
321 lines (298 loc) · 8.81 KB
/
BigInt.cpp
File metadata and controls
321 lines (298 loc) · 8.81 KB
1
2
3
4
5
6
7
8
9
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <vector>
#include <limits>
#include <string>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <iostream>
#include "BigInt.hpp"
using namespace std;
typedef int i31;
typedef unsigned int i32;
typedef long long i63;
typedef unsigned long long i64;
constexpr i31 M31 = numeric_limits<i31>::max();
constexpr i32 M32 = numeric_limits<i32>::max();
constexpr i63 M63 = numeric_limits<i63>::max();
constexpr i64 M64 = numeric_limits<i64>::max();
constexpr i63 SIZE = 1e9;
constexpr unsigned char guess[33] = {
0, 0, 0, 0, 1, 1, 1, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 5, 5, 5,
6, 6, 6, 6, 7, 7, 7, 8, 8, 8,
9, 9, 9
};
constexpr unsigned int tenToThe[] = {
1, 10, 100, 1000, 10000, 100000,
1000000, 10000000, 100000000, 1000000000,
};
static string xZeros[] = { // constexpr doesnt work with strings?
"", "0", "00", "000", "0000", "00000", "000000",
"0000000", "00000000", "00000000", "000000000"
};
i32 baseTwoDigits(i32 x) {
return x ? 32 - __builtin_clz(x) : 0;
}
static i32 baseTenDigits(i32 x) {
if (!x) return 1; // there is 1 digit in '0'
i32 digits = guess[baseTwoDigits(x)];
return digits + (x >= tenToThe[digits]);
}
i64 compare(vector<i63>& a, vector<i63>& b) {
//(a > b) == 1, (a < b) == 0, (a == b) == 2
if (a.size() != b.size()) return a.size() > b.size();
for (i63 i = 0; i < a.size(); ++i) {
if (a[i] != b[i]) return a[i] > b[i];
}
return 2; // they are equal
}
vector<i63> flatten(vector<i63> &chunks) {
// ensures that max(chunks) < SIZE
chunks.push_back(0);
for (i63 i = 0; i < chunks.size(); ++i) {
chunks[i + 1] += chunks[i] / SIZE;
chunks[i] %= SIZE;
}
while (chunks.size() > 1 && chunks.back() == 0) chunks.pop_back();
return chunks;
}
bool isZero(vector<i63>& a) {
return a.size() == 1 && !a[0];
}
BigInt::BigInt() {
chunks = { 0 };
isPos = true;
}
BigInt::BigInt(i31 val) {
isPos = (val >= 0);
if (!isPos) val = -val;
chunks = { val };
}
BigInt::BigInt(i32 val) {
isPos = (val >= 0);
if (!isPos) val = -val;
chunks = { val };
}
BigInt::BigInt(i63 val) {
isPos = (val >= 0);
if (!isPos) val = -val;
chunks = { val % SIZE };
if (val >= SIZE) chunks.push_back(val / SIZE);
}
BigInt::BigInt(i64 val) {
isPos = (val >= 0);
if (!isPos) val = -val;
chunks = { (i63)val % SIZE };
if (val >= SIZE) chunks.push_back(val / SIZE);
}
BigInt::BigInt(vector<i63> _chuncks, bool _isPos) {
isPos = _isPos;
chunks = _chuncks;
}
BigInt::BigInt(vector<bool> val, bool _isPos) {
*this = 0;
BigInt curr = 1;
for (bool bit : val) {
if (bit) *this += curr;
curr *= 2;
}
isPos = _isPos;
}
BigInt::BigInt(string val) {
i63 s = 0;
isPos = true;
if (val[0] == '-') isPos = false, ++s;
while (val[s] == '0') ++s;
if (s == val.size()) {
isPos = true;
chunks = { 0 };
return;
}
for (i63 i = val.size() - 1; i >= s;) {
i63 chunk = 0;
for (i63 rep = 0; rep < 9 && i >= s; ++rep, --i) chunk += tenToThe[rep] * (val[i] - '0');
chunks.push_back(chunk);
}
}
BigInt::BigInt(const BigInt& cp) : chunks(cp.chunks), isPos(cp.isPos) {}; // thanks Max!
bool BigInt::operator==(BigInt o) {
// O(logn b=SIZE)
if (!toBool() || !o.toBool()) return toBool() == o.toBool(); // shortcut?
return isPos == o.isPos && chunks.size() == o.chunks.size() && chunks == o.chunks;
}
bool BigInt::operator!=(BigInt o) {
// O(logn b=SIZE)
return !(this->operator==(o));
}
bool BigInt::operator>(BigInt o) {
// O(logn b=SIZE)
if (isPos != o.isPos) return (isPos > o.isPos);
// bigbrain move, equivalent to
/*if (isPos) return compare(chunks, o.chunks) == 1;
if (!isPos) return compare(chunks, o.chunks) == 0;*/
return isPos == compare(chunks, o.chunks);
}
bool BigInt::operator>=(BigInt o) {
// O(logn b=SIZE)
return !this->operator<(o);
}
bool BigInt::operator<(BigInt o) {
// O(logn b=SIZE)
// bigbrain move, equivalent to
/*if (isPos) return compare(chunks, o.chunks) == 0;
if (!isPos) return compare(chunks, o.chunks) == 1;*/
return !isPos == compare(chunks, o.chunks);
}
bool BigInt::operator<=(BigInt o) {
// O(logn b=SIZE)
return !this->operator>(o);
}
BigInt BigInt::operator+(BigInt o) {
// O(logn b=SIZE)
if (isPos && !o.isPos) return this->operator-(-o); // (*this) - (-o);
if (!isPos && o.isPos) return o.operator-(this->operator-()); // o - -(*this);
vector<i63> output;
i63 carry = 0;
for (i63 i = 0; i < max(chunks.size(), o.chunks.size()); ++i) {
i63 chunk = carry + (i < chunks.size() ? chunks[i] : 0) + (i < o.chunks.size() ? o.chunks[i] : 0);
output.push_back(chunk % SIZE);
carry = chunk / SIZE;
}
if (carry) output.push_back(carry);
return { output, isPos };
}
BigInt BigInt::operator-() {
// negate, O(1)
return { chunks, !isPos || isZero(chunks) };
}
BigInt BigInt::operator-(BigInt o) {
// O(2logn b=SIZE)
if (isPos && !o.isPos) return *this + (-o);
if (!isPos && o.isPos) return o + -(*this);
if (isPos && *this < o) return -(o - *this);
if (!isPos) return -o - -(*this);
// assume both positive, this > 0
if (!isPos || !o.isPos || *this < o) throw 20;
vector<i63> output;
bool carry = false;
for (i63 i = 0; i < max(chunks.size(), o.chunks.size()); ++i) {
i63 chunk = (i < chunks.size() ? chunks[i] : 0) - (i < o.chunks.size() ? o.chunks[i] : 0) - carry;
carry = (chunk < 0);
output.push_back(chunk + carry * SIZE);
}
return { output, true };
}
BigInt BigInt::operator*(BigInt o) {
// O(log^2 n b=SIZE)
vector<i63> output(chunks.size() + o.chunks.size(), 0);
for (i63 i = 0; i < chunks.size(); ++i) {
for (i63 j = 0; j < o.chunks.size(); ++j) {
output[i + j] += chunks[i] * o.chunks[j];
}
}
return { flatten(output), !(isPos ^ o.isPos) || isZero(output) };
}
bool BigInt::isEven() {
return chunks[0] % 2 == 0;
}
BigInt BigInt::pow(i64 o) {
// O(logo b=2)
if (o < 0) throw 20;
BigInt curr = *this, total = 1;
for (i64 p = 1; p <= o; p *= 2) {
if (p & o) total *= curr;
curr *= curr;
}
total.isPos = isPos || (o % 2) == 0;
return total;
}
BigInt BigInt::pow(BigInt o) {
// O(logo b=2)
if (o < 0) throw 20;
if (o <= SIZE * SIZE) return pow(o.toLL());
BigInt curr = *this, total = 1;
bool totalIsPos = isPos || o.isEven();
for (; o != 0; o = o >> 1) {
if (!o.isEven()) total *= curr;
curr *= curr;
}
total.isPos = totalIsPos;
return total;
}
BigInt BigInt::operator<<(BigInt o) {
// O(logo b=2)
return *this * BigInt(2).pow(o);
}
BigInt BigInt::operator>>(BigInt o) {
// O(o logn b=SIZE) whoops
if (o <= 0) {
if (o == 0) return *this;
else throw 20;
}
vector<i63> output = chunks; // hopefully makes a copy
for (; o != 0; o--) {
for (i63 i = output.size() - 1; i > 0; --i) {
if (output[i] % 2) output[i - 1] += SIZE / 2;
output[i] /= 2;
}
output[0] /= 2;
if (output.size() > 1 && output.back() == 0) output.pop_back();
}
return { output, isPos };
}
BigInt BigInt::abs() {
return { chunks, true };
}
i63 BigInt::toLL() {
// if (chunks.size() >= 2 || *this >= SIZE * SIZE) cout << "WARNING: narrowing conversion of 'BigInt' to 'i63'";
i63 total = chunks[0] + (chunks.size() > 1 ? SIZE * chunks[1] : 0);
return (isPos ? total : -total);
}
bool BigInt::toBool() {
return !isZero(chunks);
}
vector<bool> BigInt::toBin() {
// O(logn b=2) vector<bool> instead of bitset for dynamic size
// returns bin(abs(this))
if (!this->toBool()) return { 0 };
vector<bool> output;
for (BigInt cp = this->abs(); cp != 0; cp = cp >> 1) output.push_back(!cp.isEven());
return output;
}
BigInt BigInt::operator|(BigInt o) {
// O(logn b=2)
vector<bool> currBin = toBin(), oBin = o.toBin();
currBin.resize(max(currBin.size(), oBin.size()));
bool isZero = true;
for (i64 i = 0; i < oBin.size(); ++i) isZero &= (currBin[i] = currBin[i] | oBin[i]) == 0;
return { currBin, !(isPos ^ o.isPos) || isZero };
}
BigInt BigInt::operator^(BigInt o) {
// O(logn b=2)
vector<bool> currBin = toBin(), oBin = o.toBin();
currBin.resize(max(currBin.size(), oBin.size()));
bool isZero = true;
for (i64 i = 0; i < oBin.size(); ++i) isZero &= (currBin[i] = currBin[i] ^ oBin[i]) == 0;
return { currBin, !(isPos ^ o.isPos) || isZero };
}
BigInt BigInt::operator&(BigInt o) {
// O(logn b=2)
vector<bool> currBin = toBin(), oBin = o.toBin();
currBin.resize(max(currBin.size(), oBin.size()));
bool isZero = true;
for (i64 i = 0; i < oBin.size(); ++i) isZero &= (currBin[i] = currBin[i] & oBin[i]) == 0;
return { currBin, !(isPos ^ o.isPos) || isZero };
}
// I/O
ostream& operator<<(ostream& out, BigInt obj) {
if (!obj.isPos) out << '-';
out << obj.chunks.back();
for (i63 i = obj.chunks.size() - 2; i >= 0; --i) out << xZeros[9 - baseTenDigits(obj.chunks[i])] << obj.chunks[i];
return out;
}
istream& operator>>(istream& in, BigInt &obj) {
string val;
in >> val;
obj = BigInt(val);
return in;
}