-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigint_nat.hpp
259 lines (236 loc) · 7.66 KB
/
bigint_nat.hpp
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
#pragma once
#include <stdint.h>
#include <stdexcept>
#include <string>
class BigInt {
size_t len;
int8_t const *arr;
size_t *ctr;
int8_t get(size_t i) const {
return arr[i < len ? i : len];
}
BigInt(size_t rlen, int8_t const *rarr)
: len(rlen)
, arr(rarr)
, ctr(new size_t(1)) {
while (len && arr[len - 1] == arr[len]) {
len--;
}
}
public:
static BigInt from_string(std::string_view sv) {
auto itr = sv.rbegin(), end = sv.rend();
if (sv.front() == '+' || sv.front() == '-') {
end--;
}
size_t len = end - itr;
int8_t *arr = new int8_t[len + 1];
if (sv.front() == '-') {
int8_t d = 10;
for (size_t j = 0; itr != end; itr++) {
if (*itr < '0' || *itr > '9') {
delete[] arr;
throw std::invalid_argument("invalid argument");
} else {
d = '9' - *itr + (d == 10);
arr[j++] = d == 10 ? 0 : d;
}
}
arr[len] = d == 10 ? 0 : 9;
} else {
for (size_t j = 0; itr != end; itr++) {
if (*itr < '0' || *itr > '9') {
delete[] arr;
throw std::invalid_argument("invalid argument");
} else {
arr[j++] = *itr - '0';
}
}
arr[len] = 0;
}
return BigInt(len, arr);
}
std::string to_string() const {
char str[len + 2]; // GCC and Clang variable length array extension
char *itr = &str[len + 2], *end = &str[len + 2];
if (arr[len]) {
bool flag = true;
for (size_t i = 0; i < len; i++) {
int8_t d = '9' - arr[i] + flag;
*--itr = (flag = d > '9') ? '0' : d;
}
if (flag) {
*--itr = '1';
*--itr = '-';
} else {
*--itr = '-';
}
} else if (len) {
for (size_t i = 0; i < len; i++) {
*--itr = '0' + arr[i];
}
} else {
*--itr = '0';
}
return std::string(itr, end);
}
BigInt(BigInt const &rval)
: len(rval.len)
, arr(rval.arr)
, ctr(rval.ctr) {
++*ctr;
}
BigInt &operator=(BigInt const &rval) {
++*rval.ctr;
if (--*ctr == 0) {
delete[] arr;
delete ctr;
}
len = rval.len;
arr = rval.arr;
ctr = rval.ctr;
return *this;
}
~BigInt() {
if (--*ctr == 0) {
delete[] arr;
delete ctr;
}
}
operator bool() const {
return len || arr[len];
}
friend BigInt operator+(BigInt const &lbi, BigInt const &rbi) {
size_t len = (lbi.len > rbi.len ? lbi.len : rbi.len) + 1;
int8_t *arr = new int8_t[len + 1];
int8_t s = 0;
for (size_t i = 0; i <= len; i++) {
s = lbi.get(i) + rbi.get(i) + (s >= 10);
arr[i] = s >= 10 ? s - 10 : s;
}
return BigInt(len, arr);
}
friend BigInt operator-(BigInt const &lbi, BigInt const &rbi) {
size_t len = (lbi.len > rbi.len ? lbi.len : rbi.len) + 1;
int8_t *arr = new int8_t[len + 1];
int8_t d = 0;
for (size_t i = 0; i <= len; i++) {
d = lbi.get(i) - rbi.get(i) - (d < 0);
arr[i] = d < 0 ? d + 10 : d;
}
return BigInt(len, arr);
}
friend BigInt operator*(BigInt const &lbi, BigInt const &rbi) {
size_t len = lbi.len + rbi.len + 1;
int8_t *arr = new int8_t[len + 1]();
for (size_t i = 0; i <= len; i++) {
int8_t p = 0, s = 0;
for (size_t j = 0; i + j <= len; j++) {
p = lbi.get(j) * rbi.get(i) + p / 10;
s = p % 10 + arr[i + j] + (s >= 10);
arr[i + j] = s >= 10 ? s - 10 : s;
}
}
return BigInt(len, arr);
}
template<bool select>
friend BigInt divmod(BigInt const &lbi, BigInt const &rbi) {
size_t len = lbi.len + rbi.len;
int8_t *parr = new int8_t[len + 1], *narr = new int8_t[len + 1];
int8_t *qarr = new int8_t[lbi.len + 1];
int8_t *rarr = new int8_t[rbi.len + 1];
for (size_t i = 0; i <= len; i++) {
parr[i] = lbi.get(i);
}
if (lbi.arr[lbi.len] == rbi.arr[rbi.len]) {
for (size_t i = lbi.len; i <= lbi.len; i--) {
for (qarr[i] = 0;; qarr[i]++) {
int8_t d = 0;
for (size_t j = 0; i + j <= len; j++) {
d = parr[i + j] - rbi.get(j) - (d < 0);
narr[i + j] = d < 0 ? d + 10 : d;
}
if (narr[len] != parr[len]) {
break;
}
for (size_t j = 0; i + j <= len; j++) {
parr[i + j] = narr[i + j];
}
}
}
for (size_t i = 0; i <= rbi.len; i++) {
rarr[i] = parr[i];
}
} else {
for (size_t i = lbi.len; i <= lbi.len; i--) {
for (qarr[i] = 9;; qarr[i]--) {
int8_t s = 0;
for (size_t j = 0; i + j <= len; j++) {
s = parr[i + j] + rbi.get(j) + (s >= 10);
narr[i + j] = s >= 10 ? s - 10 : s;
}
if (narr[len] != parr[len]) {
break;
}
for (size_t j = 0; i + j <= len; j++) {
parr[i + j] = narr[i + j];
}
}
}
for (size_t i = 0; i <= rbi.len; i++) {
rarr[i] = narr[i];
}
}
delete[] parr;
delete[] narr;
if constexpr (select) {
delete[] qarr;
return BigInt(rbi.len, rarr);
} else {
delete[] rarr;
return BigInt(lbi.len, qarr);
}
}
friend BigInt operator/(BigInt const &lbi, BigInt const &rbi) {
return divmod<0>(lbi, rbi);
}
friend BigInt operator%(BigInt const &lbi, BigInt const &rbi) {
return divmod<1>(lbi, rbi);
}
template<auto gt, auto eq, auto lt>
friend auto compare(BigInt const &lbi, BigInt const &rbi) {
if (lbi.arr[lbi.len] < rbi.arr[rbi.len]) {
return gt;
}
if (lbi.arr[lbi.len] > rbi.arr[rbi.len]) {
return lt;
}
for (size_t m = lbi.len > rbi.len ? lbi.len : rbi.len, i = m - 1; i < m; i--) {
if (lbi.get(i) > rbi.get(i)) {
return gt;
}
if (lbi.get(i) < rbi.get(i)) {
return lt;
}
}
return eq;
}
friend bool operator>(BigInt const &lbi, BigInt const &rbi) {
return compare<true, false, false>(lbi, rbi);
}
friend bool operator<(BigInt const &lbi, BigInt const &rbi) {
return compare<false, false, true>(lbi, rbi);
}
friend bool operator>=(BigInt const &lbi, BigInt const &rbi) {
return compare<true, true, false>(lbi, rbi);
}
friend bool operator<=(BigInt const &lbi, BigInt const &rbi) {
return compare<false, true, true>(lbi, rbi);
}
friend bool operator==(BigInt const &lbi, BigInt const &rbi) {
return compare<false, true, false>(lbi, rbi);
}
friend bool operator!=(BigInt const &lbi, BigInt const &rbi) {
return compare<true, false, true>(lbi, rbi);
}
};