-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbig_integer.cpp
More file actions
684 lines (614 loc) · 17.4 KB
/
big_integer.cpp
File metadata and controls
684 lines (614 loc) · 17.4 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
#include "big_integer.h"
#include <bits/stdc++.h>
typedef uint_fast32_t usi;
typedef int_fast64_t ll;
static const uint_fast32_t base = (uint_fast32_t) (1 << 31) - 1;
static const int_fast64_t actualBase = (int_fast64_t) base + 1;
static const int basepow = 31;
static inline bool zerocheck(big_integer &a) {
if (a.number.size() == 0) a.number.push_back(0);
if (a.number.size() == 1 && a.number[0] == 0) {
return true;
}
return a.sign;
}
static inline void afterMultSignValidation(big_integer &th, bool xsign) {
if (th.sign == xsign) {
th.sign = true;
} else th.sign = false;
th.sign = zerocheck(th);
}
static inline void extracode(big_integer &a) {
for (size_t i = 0; i < a.number.size(); i++) {
a.number[i] = (~(a.number[i]) & base);
}
a.sign = true;
a += 1;
}
static inline void normalcode(big_integer &a) {
a -= 1;
a.sign = false;
for (size_t i = 0; i < a.number.size(); i++) {
a.number[i] = (~(a.number[i]) & base);
}
}
static void check_zerodiv(big_integer const &a) {
if (a == 0) throw std::string("Devising by zero\n");
}
static void check_zerodiv(int_fast32_t a) {
if (a == 0) throw std::string("Devising by zero\n");
}
static inline void check_negative_shf(int const a) {
if (a < 0) throw std::string("Negative shigting\n");
}
static big_integer &abstractLogicOperation(big_integer &a, big_integer b,
uint_fast32_t (*logicFunc)(uint_fast32_t, uint_fast32_t),
bool (*check)(bool, bool)) {
bool asign = a.sign;
bool bsign = b.sign;
if (!asign) {
extracode(a);
}
if (!b.sign) {
extracode(b);
}
for (size_t i = 0; i < a.number.size(); i++) {
a.number[i] = logicFunc(a.number[i], (i < b.number.size() ? b.number[i] : 0));
}
if (check(!asign, !bsign)) {
normalcode(a);
}
return a;
}
static bool cmpPosSigns(big_integer const &a, big_integer const &b) { //if a == b return false; if a > b return true;
if (a.number.size() > b.number.size()) {
return true;
} else if (a.number.size() < b.number.size()) {
return false;
} else {
for (int i = (int) a.number.size() - 1; i >= 0; i--) {
if (a.number[i] > b.number[i]) {
return true;
}
if (a.number[i] < b.number[i]) {
return false;
}
}
}
return false;
}
big_integer::big_integer() {
number.clear();
number.push_back(0);
sign = true;
}
big_integer::big_integer(big_integer const &other) {
number = other.number;
sign = other.sign;
}
big_integer::big_integer(int_fast64_t a) {
number.clear();
sign = (a >= 0);
if (a == 0) {
number.push_back(0);
}
while (a != 0) {
number.push_back((usi) (std::abs((a % actualBase))));
a /= actualBase;
}
}
big_integer::big_integer(std::string const &str) {
std::string s = str;
number.clear();
size_t it = 0;
if (str[0] == '-') {
it++;
}
sign = true;
for (size_t i = it; i < s.length(); i++) {
*this *= 10;
*this += (s[i] - '0');
}
if (s[0] == '-') {
*this = -*this;
}
}
big_integer::~big_integer() {
number.clear();
}
big_integer &big_integer::operator=(big_integer const &other) {
this->number.resize(other.number.size());
//memcpy(&this->number[0], &other.number[0], other.number.size() * sizeof(usi));
//number = other.number;
for (size_t i = 0; i < other.number.size(); i++) {
this->number[i] = other.number[i];
}
sign = other.sign;
return *this;
}
big_integer &big_integer::operator+=(big_integer rhs) {
if (sign == rhs.sign) {
usi carry = 0;
ll cur = 0;
for (size_t i = 0; i < std::max(number.size(), rhs.number.size()) || carry; ++i) {
if (i == number.size())
number.push_back(0);
cur = (ll) number[i] + (ll) carry + (ll) (i < rhs.number.size() ? rhs.number[i] : 0);
number[i] = (usi) (cur % actualBase);
carry = (usi) (cur / actualBase);
}
} else {
bool saveSign = sign;
sign = true;
rhs.sign = true;
bool newSign;
if (*this > rhs) {
newSign = saveSign;
} else {
newSign = !saveSign;
}
*this -= rhs;
sign = newSign;
}
sign = zerocheck(*this);
return *this;
}
big_integer &big_integer::operator-=(big_integer const &rhs) {
big_integer a = *this;
a.sign = true;
big_integer b = rhs;
b.sign = true;
if (this->sign == rhs.sign) {
if (a < b) {
std::swap(a, b);
}
ll carry = 0;
ll cur = 0;
for (size_t i = 0; i < b.number.size() || carry; ++i) {
cur = (ll) a.number[i];
cur -= carry + (ll) (i < b.number.size() ? b.number[i] : 0);
carry = (cur < 0);
if (carry) cur += actualBase;
a.number[i] = (usi) cur;
}
while (a.number.size() > 1 && a.number.back() == 0)
a.number.pop_back();
if (cmpPosSigns(*this, rhs)) {
sign = this->sign;
} else {
sign = !this->sign;
}
} else {
a += b;
}
this->number = a.number;
sign = zerocheck(*this);
return *this;
}
//static usi _ans[(1 << 64)];
static big_integer _ans;
static big_integer mult(big_integer const &b, uint_fast32_t x) {
ll carry = 0;
big_integer a = b;
a.number.push_back(0);
for (size_t i = 0; i < a.number.size() || carry; i++) {
ll cur = carry + (ll) a.number[i] * x;
a.number[i] = (usi) (cur % actualBase);
carry = usi(cur / actualBase);
}
while (a.number.size() > 1 && a.number.back() == 0)
a.number.pop_back();
return a;
}
big_integer &big_integer::operator*=(big_integer const &rhs) {
if (rhs.number.size() == 1) {
number = mult(*this, rhs.number[0]).number;
afterMultSignValidation(*this, rhs.sign);
return *this;
}
//_ans.number.resize(number.size() + rhs.number.size());
if (number.size() + rhs.number.size() > _ans.number.size()) _ans.number.resize(number.size() + rhs.number.size());
memset(&_ans.number[0], 0, _ans.number.size() * sizeof(usi));
for (size_t i = 0; i < number.size(); i++)
for (size_t j = 0, carry = 0; j < rhs.number.size() || carry; j++) {
ll cur = (ll) _ans.number[i + j] + (ll) number[i] * (ll) (j < rhs.number.size() ? rhs.number[j] : 0) +
carry;
_ans.number[i + j] = usi((cur % actualBase));
carry = usi((cur / actualBase));
}
// number.resize(_ans.number.size());
// memcpy(&number[0], &_ans.number[0], sizeof(usi) *_ans.number.size());
number = _ans.number;
while (number.size() > 1 && number.back() == 0)
number.pop_back();
afterMultSignValidation(*this, rhs.sign);
return *this;
}
//returns:
//div = true : th / rhs
//div = false : rh % rhs
static big_integer &divWithModBig(big_integer &th, big_integer const &rhs, bool div) {
if (rhs.number.size() == 1 && (rhs.number[0] <= usi(std::numeric_limits<int_fast32_t>::max()))) {
bool saveRhsSign = true;
if (div) {
th /= rhs.number[0];
saveRhsSign = rhs.sign;
}
else th %= rhs.number[0];
afterMultSignValidation(th, saveRhsSign);
return th;
}
std::vector<usi> ans;
ans.resize(th.number.size() - rhs.number.size() + 1);
big_integer curValue(th);
curValue.sign = true;
big_integer now(0);
big_integer divider(rhs);
divider.sign = true;
for (size_t i = 0; i < ans.size(); i++) {
ll l = 0, r = base + 1;
while (r - l > 1) {
ll m = (l + r) / 2;
now = mult(divider, usi(m));
if (ans.size() - i - 1 > 0) {
std::reverse(now.number.begin(), now.number.end());
for (size_t j = 0; j < ans.size() - i - 1; j++) {
now.number.push_back(0);
}
std::reverse(now.number.begin(), now.number.end());
}
if (now <= curValue) {
l = m;
} else {
r = m;
}
}
now = l * divider;
if (now.number.size() < curValue.number.size()) {
std::reverse(now.number.begin(), now.number.end());
while (now.number.size() < curValue.number.size()) {
now.number.push_back(0);
}
std::reverse(now.number.begin(), now.number.end());
}
if (now > curValue) {
now.number.erase(now.number.begin());
}
curValue -= now;
ans[i] = usi(l);
}
bool saveSign;
if (div) {
std::reverse(ans.begin(), ans.end());
th.number = ans;
saveSign = rhs.sign;
} else {
th.number = curValue.number;
saveSign = true;
}
while (th.number.size() > 1 && th.number.back() == 0) {
th.number.pop_back();
}
afterMultSignValidation(th, saveSign);
return th;
}
big_integer &big_integer::operator/=(big_integer const &rhs) {
try {
check_zerodiv(rhs);
return divWithModBig(*this, rhs, true);
} catch (std::string e) {
throw e;
}
}
big_integer &big_integer::operator%=(big_integer const &rhs) {
// *this -= rhs * (*this / rhs);
// return *this;
try {
check_zerodiv(rhs);
return divWithModBig(*this, rhs, false);
} catch (std::string e) {
throw e;
}
}
big_integer &big_integer::operator&=(big_integer const &rhs) {
return abstractLogicOperation(*this, big_integer(rhs), [](usi a, usi b) -> usi { return a & b; },
[](bool x, bool y) -> bool { return x & y; });
}
big_integer &big_integer::operator|=(big_integer const &rhs) {
return abstractLogicOperation(*this, big_integer(rhs), [](usi a, usi b) -> usi { return a | b; },
[](bool x, bool y) -> bool { return x | y; });
}
big_integer &big_integer::operator^=(big_integer const &rhs) {
return abstractLogicOperation(*this, big_integer(rhs), [](usi a, usi b) -> usi { return a ^ b; },
[](bool x, bool y) -> bool { return x | y; });
}
big_integer &big_integer::operator<<=(int rhs) {
try {
check_negative_shf(rhs);
std::reverse(this->number.begin(), this->number.end());
for (int i = 0; i < rhs / basepow; i++) {
this->number.push_back(0);
}
std::reverse(this->number.begin(), this->number.end());
for (int i = 0; i < rhs % basepow; i++) {
*this *= 2;
}
return *this;
} catch (std::string e) {
throw e;
}
}
big_integer &big_integer::operator>>=(int rhs) {
try {
check_negative_shf(rhs);
std::reverse(this->number.begin(), this->number.end());
bool thissigne = sign;
for (int i = 0; i < rhs / basepow; i++) {
this->number.pop_back();
}
std::reverse(this->number.begin(), this->number.end());
for (int i = 0; i < rhs % basepow; i++) {
*this /= 2;
}
if (!thissigne) {
*this -= 1;
}
return *this;
} catch (std::string e) {
throw e;
}
}
big_integer big_integer::operator+() const {
big_integer a = *this;
a.sign = true;
return a;
}
big_integer big_integer::operator-() const {
big_integer a = *this;
a.sign = true;
if (a != 0) {
a.sign = !sign;
}
return a;
}
big_integer big_integer::operator~() const {
return (-*this) - 1;
}
big_integer &big_integer::operator++() {
return *this += 1;
}
big_integer big_integer::operator++(int a) {
return *this += 1;
}
big_integer &big_integer::operator--() {
return *this -= 1;
}
big_integer big_integer::operator--(int) {
return *this -= 1;
}
big_integer operator+(big_integer a, big_integer const &b) {
return a += b;
}
big_integer operator-(big_integer a, big_integer const &b) {
return a -= b;
}
big_integer operator*(big_integer a, big_integer const &b) {
return a *= b;
}
big_integer operator*(big_integer a, int_fast32_t const x) {
return a *= x;
}
big_integer operator/(big_integer a, big_integer const &b) {
try {
return a /= b;
} catch (std::string e) {
throw e;
}
}
big_integer operator/(big_integer a, int_fast32_t const x) {
try {
return a /= x;
} catch (std::string e) {
throw e;
}
}
big_integer operator%(big_integer a, big_integer const &b) {
try {
a %= b;
return a;
} catch (std::string e) {
throw e;
}
}
big_integer operator%(big_integer a, int_fast32_t const x) {
try {
a %= x;
return a;
} catch (std::string e) {
throw e;
}
}
big_integer operator&(big_integer a, big_integer const &b) {
return a &= b;
}
big_integer operator|(big_integer a, big_integer const &b) {
return a |= b;
}
big_integer operator^(big_integer a, big_integer const &b) {
return a ^= b;
}
big_integer operator<<(big_integer a, int b) {
try {
return a <<= b;
} catch (std::string e) {
throw e;
}
}
big_integer operator>>(big_integer a, int b) {
try {
return a >>= b;
} catch (std::string e) {
throw e;
}
}
bool operator==(big_integer const &a, big_integer const &b) {
if (a.sign == b.sign) {
if (a.number.size() == b.number.size()) {
for (int i = 0; i < (int) a.number.size(); i++) {
if (a.number[i] != b.number[i]) return false;
}
return true;
} else return false;
} else return false;
}
bool operator!=(big_integer const &a, big_integer const &b) {
return !(a == b);
}
bool operator<(big_integer const &a, big_integer const &b) {
return !(a >= b);
}
bool operator>(big_integer const &a, big_integer const &b) {
if (a.sign == b.sign) {
if (!a.sign) {
big_integer pb = b;
pb.sign = true;
big_integer pa = a;
pa.sign = true;
return cmpPosSigns(pb, pa);
} else {
return cmpPosSigns(a, b);
}
} else {
return a.sign;
}
}
bool operator<=(big_integer const &a, big_integer const &b) {
return !(a > b);
}
bool operator>=(big_integer const &a, big_integer const &b) {
return (a > b) || (a == b);
}
std::string to_string(big_integer const &a) {
std::string s;
if (!a.sign) {
s += '-';
}
big_integer b = a;
b.sign = true;
big_integer cur = 0;
std::vector<usi> ans;
if (b == 0) ans.push_back(0);
while (b != 0) {
cur = b % 10;
ans.push_back(cur.number[0]);
b /= 10;
}
std::reverse(ans.begin(), ans.end());
for (size_t i = 0; i < ans.size(); i++) {
s += std::to_string(ans[i]);
}
return s;
}
//returns:
//div = true: *this / x
//div = false: *this % x
static inline big_integer &divWithMod(big_integer &th, int_fast32_t const x, bool div) {
usi carry = 0;
int_fast64_t xx = (int_fast64_t) x;
uint_fast32_t y = (usi) std::abs(xx);
if (th == 0) {
if (div) return th;
else {
th = 0;
return th;
}
}
bool xsign = (x >= 0);
for (int i = (int) th.number.size() - 1; i >= 0; --i) {
ll cur = (ll) th.number[i] + (ll) carry * actualBase;
th.number[i] = usi(cur / y);
carry = usi(cur % y);
}
if (div) {
while (th.number.size() > 1 && th.number.back() == 0)
th.number.pop_back();
} else {
bool saveSign = th.sign;
th = carry;
th.sign = saveSign;
xsign = true;
}
afterMultSignValidation(th, xsign);
return th;
}
big_integer &big_integer::operator/=(int_fast32_t const x) {
try {
check_zerodiv(x);
return divWithMod(*this, x, true);
} catch (std::string e) {
throw e;
}
}
big_integer &big_integer::operator%=(int_fast32_t const x) {
try {
check_zerodiv(x);
return divWithMod(*this, x, false);
} catch (std::string e) {
throw e;
}
}
big_integer &big_integer::operator*=(int_fast32_t const x) {
if (*this == 0 || x == 0) {
*this = 0;
return *this;
}
bool xsign = (x >= 0);
this->number = mult(*this, usi(std::abs(x))).number;
afterMultSignValidation(*this, xsign);
return *this;
}
big_integer &big_integer::operator=(int_fast64_t other) {
big_integer a(other);
*this = a;
return *this;
}
std::ostream &operator<<(std::ostream &s, big_integer const &a) {
return s << to_string(a);
}
void operator>>(std::istream &s, big_integer &a) {
std::string str;
s >> str;
a = big_integer(str);
}
//int main() {
// big_integer p;
// big_integer q;
// std::cout << clock() / 1000.0 << std::endl;
// freopen("tests.in", "r", stdin);
// std::cin >> p;
// std::cin >> q;
//
// const int N = 1000;
// for (int i = 0; i < N; i++) {
// p *= q;
// }
// for (int i = 0; i < N; i++) {
// p += p;
// }
// for (int i = 0; i < N; i++) {
// p /= 2;
// }
// for (int i = 0; i < N; i++) {
// p /= q;
// }
//
//// try {
//// p <<= (-1);
//// } catch (std::string e) {
//// std::cout << e;
//// }
//
// std::cout << p << std::endl;
// std::cout << clock() / 1000.0 << std::endl;
// return 0;
//}