-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.h
1782 lines (1441 loc) · 76.7 KB
/
matrix.h
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
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <vector>
#include <string>
#include <vector>
#include <cstdlib>
#include <fstream>
#include<cmath>
namespace TYPES::BIGINTEGER {
//--------------------------------------------------------------------------------------------------------------------//
/*
████████╗██╗░░██╗███████╗ ██████╗░███████╗░██████╗░██╗███╗░░██╗███╗░░██╗██╗███╗░░██╗░██████╗░
╚══██╔══╝██║░░██║██╔════╝ ██╔══██╗██╔════╝██╔════╝░██║████╗░██║████╗░██║██║████╗░██║██╔════╝░
░░░██║░░░███████║█████╗░░ ██████╦╝█████╗░░██║░░██╗░██║██╔██╗██║██╔██╗██║██║██╔██╗██║██║░░██╗░
░░░██║░░░██╔══██║██╔══╝░░ ██╔══██╗██╔══╝░░██║░░╚██╗██║██║╚████║██║╚████║██║██║╚████║██║░░╚██╗
░░░██║░░░██║░░██║███████╗ ██████╦╝███████╗╚██████╔╝██║██║░╚███║██║░╚███║██║██║░╚███║╚██████╔╝
░░░╚═╝░░░╚═╝░░╚═╝╚══════╝ ╚═════╝░╚══════╝░╚═════╝░╚═╝╚═╝░░╚══╝╚═╝░░╚══╝╚═╝╚═╝░░╚══╝░╚═════╝░
░█████╗░███████╗ ██╗░░██╗███████╗██╗░░░░░██╗░░░░░
██╔══██╗██╔════╝ ██║░░██║██╔════╝██║░░░░░██║░░░░░
██║░░██║█████╗░░ ███████║█████╗░░██║░░░░░██║░░░░░
██║░░██║██╔══╝░░ ██╔══██║██╔══╝░░██║░░░░░██║░░░░░
╚█████╔╝██║░░░░░ ██║░░██║███████╗███████╗███████╗
*/
//--------------------------------------------------------------------------------------------------------------------//
using std::string;
using std::vector;
//из записок Грустного кота (BIGINTEGER, edition of Sad_cat)
//--------------------------------------------------------------------------------------------------------------------//
/*
████████╗██╗░░██╗███████╗ ██████╗░██╗░██████╗░░██████╗░███████╗░██████╗████████╗ ██╗███╗░░██╗████████╗
╚══██╔══╝██║░░██║██╔════╝ ██╔══██╗██║██╔════╝░██╔════╝░██╔════╝██╔════╝╚══██╔══╝ ██║████╗░██║╚══██╔══╝
░░░██║░░░███████║█████╗░░ ██████╦╝██║██║░░██╗░██║░░██╗░█████╗░░╚█████╗░░░░██║░░░ ██║██╔██╗██║░░░██║░░░
░░░██║░░░██╔══██║██╔══╝░░ ██╔══██╗██║██║░░╚██╗██║░░╚██╗██╔══╝░░░╚═══██╗░░░██║░░░ ██║██║╚████║░░░██║░░░
░░░██║░░░██║░░██║███████╗ ██████╦╝██║╚██████╔╝╚██████╔╝███████╗██████╔╝░░░██║░░░ ██║██║░╚███║░░░██║░░░
░░░╚═╝░░░╚═╝░░╚═╝╚══════╝ ╚═════╝░╚═╝░╚═════╝░░╚═════╝░╚══════╝╚═════╝░░░░╚═╝░░░ ╚═╝╚═╝░░╚══╝░░░╚═╝░░░
██╗███╗░░██╗ ████████╗██╗░░██╗███████╗ ░██╗░░░░░░░██╗██╗██╗░░░░░██████╗░
██║████╗░██║ ╚══██╔══╝██║░░██║██╔════╝ ░██║░░██╗░░██║██║██║░░░░░██╔══██╗
██║██╔██╗██║ ░░░██║░░░███████║█████╗░░ ░╚██╗████╗██╔╝██║██║░░░░░██║░░██║
██║██║╚████║ ░░░██║░░░██╔══██║██╔══╝░░ ░░████╔═████║░██║██║░░░░░██║░░██║
██║██║░╚███║ ░░░██║░░░██║░░██║███████╗ ░░╚██╔╝░╚██╔╝░██║███████╗██████╔╝
╚═╝╚═╝░░╚══╝ ░░░╚═╝░░░╚═╝░░╚═╝╚══════╝ ░░░╚═╝░░░╚═╝░░╚═╝╚══════╝╚═════╝░
░██╗░░░░░░░██╗███████╗░██████╗████████╗
░██║░░██╗░░██║██╔════╝██╔════╝╚══██╔══╝
░╚██╗████╗██╔╝█████╗░░╚█████╗░░░░██║░░░
░░████╔═████║░██╔══╝░░░╚═══██╗░░░██║░░░
░░╚██╔╝░╚██╔╝░███████╗██████╔╝░░░██║░░░
░░░╚═╝░░░╚═╝░░╚══════╝╚═════╝░░░░╚═╝░░░
*/
//--------------------------------------------------------------------------------------------------------------------//
class BigInteger {
public:
BigInteger() {
isPositive = true;
number = {0};
};
BigInteger(int newValue) : isPositive(true) {
CheckSignAndABS(newValue);
StitchingNumber(newValue);
}
BigInteger(const string &newValue) : isPositive(true) {
CheckSign(newValue);
LocalPars(newValue);
Coup(newValue);
}
BigInteger(const BigInteger &x) {
isPositive = x.isPositive;
number = x.number;
}
BigInteger &operator=(const BigInteger &other) {
BigInteger copy(other);
std::swap(number, copy.number);
std::swap(isPositive, copy.isPositive);
return *this;
}
~BigInteger() {
number.clear();
}
explicit operator bool() const {
return !(number.size() == 1 && number[0] == 0);
}
string toString() const {
string s;
CheckSign(s);
StitchingString(s);
return s;
}
bool operator!=(const BigInteger &other) const {
return Comparator(*this, other) != 1;
}
bool operator<(const BigInteger &other) const {
return Comparator(*this, other) == 3;
}
bool operator>(const BigInteger &other) const {
return Comparator(*this, other) == 2;
}
bool operator>=(const BigInteger &other) const {
int res = Comparator(*this, other);
return res == 2 || res == 1;
}
bool operator<=(const BigInteger &other) const {
int res = Comparator(*this, other);
return res == 3 || res == 1;
}
bool operator==(const BigInteger &other) const {
return Comparator(*this, other) == 1;
}
static BigInteger abs(const BigInteger &x) {
BigInteger copy = BigInteger(x);
copy.isPositive = true;
return copy;
}
BigInteger operator-() {
BigInteger copy = BigInteger(*this);
copy.isPositive = !isPositive;
copy.isPositive = number.size() == 1 && number[0] == 0;
return copy;
}
BigInteger &operator--(int) {
*this -= 1;
return *this;
}
BigInteger &operator--() {
*this -= 1;
return *this;
}
BigInteger &operator++(int) {
*this += 1;
return *this;
}
BigInteger &operator++() {
*this += 1;
return *this;
}
friend BigInteger operator%(const BigInteger &first, const BigInteger &second) {
BigInteger resultRemains(first);
resultRemains %= second;
return resultRemains;
}
friend BigInteger operator/(const BigInteger &first, const BigInteger &second) {
BigInteger resultDivision(first);
resultDivision /= second;
return resultDivision;
}
friend BigInteger operator*(const BigInteger &first, const BigInteger &second) {
BigInteger resultComposition(first);
resultComposition *= second;
return resultComposition;
}
friend BigInteger operator+(const BigInteger &first, const BigInteger &second) {
BigInteger summa(first);
summa += second;
return summa;
}
friend BigInteger operator-(const BigInteger &first, const BigInteger &second) {
BigInteger difference(first);
difference -= second;
return difference;
}
BigInteger &operator%=(const BigInteger &other) {
*this -= ((*this / other) * other);
*this = DeleteZeros(*this);
return *this;
}
BigInteger &operator/=(const BigInteger &other) {
bool sign = this->isPositive;
*this = DivideSec(*this, other);
isPositive = (!((!sign || !other.isPositive) && (sign || other.isPositive)));
if ((number.size() == 1 && number[0] == 0))
isPositive = true;
return *this;
}
BigInteger &operator*=(const BigInteger &other) {
BigInteger resultComposition;
for (size_t i = 0; i < other.number.size(); i++) {
resultComposition += Multiply(*this, other.number[i], i);
}
resultComposition.isPositive = !((isPositive && !other.isPositive) ||
(!isPositive && other.isPositive));
if ((resultComposition.number.size() == 1 && resultComposition.number[0] == 0))
resultComposition.isPositive = true;
*this = resultComposition;
return *this;
}
BigInteger &operator-=(const BigInteger &other) {
bool sign = isPositive;
if ((isPositive && other.isPositive) || (!isPositive && !other.isPositive)) {
BigInteger copy1 = abs(*this), copy2 = abs(other);
*this = Difference(*this, other);
isPositive = copy1 > copy2 ? sign : !sign;
if ((number.size() == 1 && number[0] == 0))
isPositive = true;
} else {
*this = AddUp(*this, other);
isPositive = sign;
}
*this = DeleteZeros(*this);
return *this;
}
BigInteger &operator+=(const BigInteger &other) {
bool sign = this->isPositive;
if (!(this->isPositive && other.isPositive) &&
!(!this->isPositive && !other.isPositive)) {
BigInteger copy1 = abs(*this), copy2 = abs(other);
*this = Difference(*this, other);
this->isPositive = copy1 > copy2 ? sign : other.isPositive;
if ((number.size() == 1 && number[0] == 0))
isPositive = true;
} else {
*this = AddUp(*this, other);
this->isPositive = sign;
}
*this = DeleteZeros(*this);
return *this;
}
private:
vector<int> number;
bool isPositive;
void CheckSign(string &s) const {
if (!(isPositive))
s += '-';
if ((number.size() == 1) && number[0] == 0)
s = "";
}
void StitchingString(string &s) const {
for (size_t j = 0; j < number.size(); j++) {
char a = number[number.size() - j - 1] + '0';
s += a;
}
}
static BigInteger &CutZeros(BigInteger &other) {
long long i = other.number.size() - 1;
while (other.number[i] == 0) {
if (i == 0 && other.number[i] == 0) {
other.isPositive = true;
return other;
}
other.number.pop_back();
i--;
}
return other;
}
void CheckSignAndABS(int &newValue) {
newValue < 0 ? this->isPositive = false : this->isPositive = true;
newValue = std::abs(newValue);
}
void StitchingNumber(int &newValue) {
if (newValue == 0)
number.push_back(0);
while (newValue != 0) {
number.push_back(newValue % 10), newValue /= 10;
}
}
void CheckSign(const string &newValue) {
newValue[0] == '-' ?
isPositive = false : isPositive = true;
}
void LocalPars(const string &newValue) {
size_t i = 0;
if (!this->isPositive)
i++;
for (; i < newValue.size(); i++)
number.push_back(newValue[i] - '0');
}
void Coup([[maybe_unused]] const string &newValue) {
for (size_t i = 0; i < number.size() / 2; i++)
std::swap(number[i], number[number.size() - i - 1]);
}
static void Coup(BigInteger &newValue) {
for (size_t i = 0; i < newValue.number.size() / 2; i++)
std::swap(newValue.number[i], newValue.number[newValue.number.size() - i - 1]);
}
static int Comparator(const BigInteger &first, const BigInteger &second) {
size_t n = first.number.size();
size_t m = second.number.size();
if (first.isPositive && second.isPositive && n == m) {
return Compare(first, second, 1);
}
if (!(first.isPositive) && !(second.isPositive) && n == m) {
return Compare(first, second, -1);
}
return CompareNotEqualSignOrOther(first, second);
}
static int CompareNotEqualSignOrOther(const BigInteger &first, const BigInteger &second) {
if (!first.isPositive && second.isPositive) {
return 3;
}
if (first.isPositive && !second.isPositive) {
return 2;
}
return CompareNotEqualSizeOrOtherPlus(first, second);
}
static int
CompareNotEqualSizeOrOtherPlus(const BigInteger &first, const BigInteger &second) {
if (first.isPositive && second.isPositive &&
first.number.size() < second.number.size()) {
return 3;
}
if (first.isPositive && second.isPositive &&
first.number.size() > second.number.size()) {
return 2;
}
return CompareNotEqualSizeOrOtherMinus(first, second);
}
static int
CompareNotEqualSizeOrOtherMinus(const BigInteger &first, const BigInteger &second) {
if (!(first.isPositive) && !(second.isPositive) &&
first.number.size() > second.number.size()) {
return 2;
}
if (!(first.isPositive) && !(second.isPositive) &&
first.number.size() < second.number.size()) {
return 3;
}
return -1;
}
static int Compare(const BigInteger &first, const BigInteger &second, int sign) {
size_t n = first.number.size();
size_t m = second.number.size();
for (size_t i = 0; i < m; i++) {
if (first.number[n - i - 1] * sign > second.number[n - i - 1] * sign) {
return 2;
} else if (first.number[n - i - 1] * sign < second.number[n - i - 1] * sign) {
return 3;
}
}
return 1; // equal
}
static BigInteger DivideSec(const BigInteger &first, const BigInteger &second) {
string s;
BigInteger row_result = s;
if (abs(first) < abs(second)) {
row_result = 0;
} else {
BigInteger rest, current_dividend;
int last = first.number.size(), digit;
while (last > 0) {
digit = 0;
last--;
current_dividend = Multiply(rest, 10, 0) + first.number[last];
while (current_dividend >= abs(second)) {
current_dividend -= abs(second);
if (current_dividend >= 0) {
digit++;
rest = current_dividend;
}
}
if (current_dividend >= 0) {
rest = current_dividend;
}
row_result.number.push_back(digit);
}
}
Coup(row_result);
row_result.isPositive = true;
row_result = DeleteZeros(row_result);
return row_result;
}
static BigInteger Multiply(BigInteger &other, int digit, size_t order) {
int rest = 0;
size_t size = other.number.size();
string s;
BigInteger result = s;
for (size_t i = 0; i < order; i++) {
result.number.push_back(0);
}
for (size_t i = 0; i < size; i++) {
if (other.number[i] * digit + rest <= 9) {
result.number.push_back(other.number[i] * digit + rest);
rest = 0;
} else {
result.number.push_back((other.number[i] * digit + rest) % 10);
rest = (other.number[i] * digit + rest) / 10;
}
}
if (rest != 0) {
result.number.push_back(rest);
}
result = DeleteZeros(result);
result.isPositive = true;
return result;
}
BigInteger &Difference(BigInteger &first, const BigInteger &second) {
string s;
BigInteger firstCopy = s;
firstCopy = abs(abs(first) > abs(second) ? first : second);
BigInteger secondCopy;
secondCopy.MakeLonger(abs(first), abs(second));
size_t n = secondCopy.number.size();
first = s;
int prev_rest = 0;
for (size_t i = 0; i < n; i++) {
if (firstCopy.number[i] - prev_rest >= secondCopy.number[i]) {
first.number.push_back(firstCopy.number[i] - prev_rest - secondCopy.number[i]);
prev_rest = 0;
} else {
first.number.push_back(
10 - prev_rest + firstCopy.number[i] - secondCopy.number[i]);
prev_rest = 1;
}
}
first = DeleteZeros(first);
first.isPositive = true;
return first;
}
BigInteger MakeLonger(BigInteger first, BigInteger second) {
string s;
*this = s;
size_t n = first.number.size();
size_t m = second.number.size();
if (n < m) {
this->isPositive = first.isPositive;
for (size_t j = 0; j < n; j++) {
this->number.push_back(first.number[j]);
}
for (size_t i = 0; i < m - n; i++) {
this->number.push_back(0);
}
} else if (m < n) {
this->isPositive = second.isPositive;
for (size_t j = 0; j < m; j++) {
this->number.push_back(second.number[j]);
}
for (size_t i = 0; i < n - m; i++) {
this->number.push_back(0);
}
} else {
if (first > second) {
*this = second;
return second;
} else {
*this = first;
return first;
}
}
return *this;
}
static BigInteger &DeleteZeros(BigInteger &other) {
return other.number.empty() ? other : CutZeros(other);
}
static BigInteger &AddUp(BigInteger &first, const BigInteger &second) {
BigInteger firstCopy;
firstCopy = abs(abs(first) <= abs(second) ? second : first);
BigInteger secondCopy;
secondCopy.MakeLonger(abs(first), abs(second));
size_t n = secondCopy.number.size();
string s = "";
first = s;
int rest = 0;
for (size_t i = 0; i < n; i++) {
int sum = firstCopy.number[i] + secondCopy.number[i] + rest;
rest = sum / 10;
first.number.push_back(sum % 10);
}
if (rest != 0) {
first.number.push_back(rest);
}
first = DeleteZeros(first);
first.isPositive = true;
return first;
}
};
BigInteger operator ""_bi(const char *str, size_t) {
string number = string(str);
return BigInteger(number);
}
//ты кот, который смог
//--------------------------------------------------------------------------------------------------------------------//
/*
██╗████████╗░██████╗███████╗██╗░░░░░███████╗ ██╗░██████╗ ███╗░░██╗░█████╗░████████╗ ░█████╗░
██║╚══██╔══╝██╔════╝██╔════╝██║░░░░░██╔════╝ ██║██╔════╝ ████╗░██║██╔══██╗╚══██╔══╝ ██╔══██╗
██║░░░██║░░░╚█████╗░█████╗░░██║░░░░░█████╗░░ ██║╚█████╗░ ██╔██╗██║██║░░██║░░░██║░░░ ███████║
██║░░░██║░░░░╚═══██╗██╔══╝░░██║░░░░░██╔══╝░░ ██║░╚═══██╗ ██║╚████║██║░░██║░░░██║░░░ ██╔══██║
██║░░░██║░░░██████╔╝███████╗███████╗██║░░░░░ ██║██████╔╝ ██║░╚███║╚█████╔╝░░░██║░░░ ██║░░██║
╚═╝░░░╚═╝░░░╚═════╝░╚══════╝╚══════╝╚═╝░░░░░ ╚═╝╚═════╝░ ╚═╝░░╚══╝░╚════╝░░░░╚═╝░░░ ╚═╝░░╚═╝
██████╗░░█████╗░████████╗██╗░█████╗░███╗░░██╗░█████╗░██╗░░░░░ ░██╗░░░░░░░██╗░█████╗░░██████╗████████╗███████╗
██╔══██╗██╔══██╗╚══██╔══╝██║██╔══██╗████╗░██║██╔══██╗██║░░░░░ ░██║░░██╗░░██║██╔══██╗██╔════╝╚══██╔══╝██╔════╝
██████╔╝███████║░░░██║░░░██║██║░░██║██╔██╗██║███████║██║░░░░░ ░╚██╗████╗██╔╝███████║╚█████╗░░░░██║░░░█████╗░░
██╔══██╗██╔══██║░░░██║░░░██║██║░░██║██║╚████║██╔══██║██║░░░░░ ░░████╔═████║░██╔══██║░╚═══██╗░░░██║░░░██╔══╝░░
██║░░██║██║░░██║░░░██║░░░██║╚█████╔╝██║░╚███║██║░░██║███████╗ ░░╚██╔╝░╚██╔╝░██║░░██║██████╔╝░░░██║░░░███████╗
╚═╝░░╚═╝╚═╝░░╚═╝░░░╚═╝░░░╚═╝░╚════╝░╚═╝░░╚══╝╚═╝░░╚═╝╚══════╝ ░░░╚═╝░░░╚═╝░░╚═╝░░╚═╝╚═════╝░░░░╚═╝░░░╚══════╝
░█████╗░███████╗ ████████╗██╗███╗░░░███╗███████╗ ██╗███╗░░██╗ ███╗░░░███╗██╗░░░██╗
██╔══██╗██╔════╝ ╚══██╔══╝██║████╗░████║██╔════╝ ██║████╗░██║ ████╗░████║╚██╗░██╔╝
██║░░██║█████╗░░ ░░░██║░░░██║██╔████╔██║█████╗░░ ██║██╔██╗██║ ██╔████╔██║░╚████╔╝░
██║░░██║██╔══╝░░ ░░░██║░░░██║██║╚██╔╝██║██╔══╝░░ ██║██║╚████║ ██║╚██╔╝██║░░╚██╔╝░░
╚█████╔╝██║░░░░░ ░░░██║░░░██║██║░╚═╝░██║███████╗ ██║██║░╚███║ ██║░╚═╝░██║░░░██║░░░
░╚════╝░╚═╝░░░░░ ░░░╚═╝░░░╚═╝╚═╝░░░░░╚═╝╚══════╝ ╚═╝╚═╝░░╚══╝ ╚═╝░░░░░╚═╝░░░╚═╝░░░
██╗░░░░░██╗███████╗███████╗
██║░░░░░██║██╔════╝██╔════╝
██║░░░░░██║█████╗░░█████╗░░
██║░░░░░██║██╔══╝░░██╔══╝░░
███████╗██║██║░░░░░███████╗
╚══════╝╚═╝╚═╝░░░░░╚══════╝
*/
//--------------------------------------------------------------------------------------------------------------------//
class Rational {
public:
Rational() : numerator(0), denominator(1), isPositive(true) {};
Rational(BigInteger numberUp, BigInteger numberDown = 1) : numerator(numberUp),
denominator(numberDown),
isPositive(true) {
CheckSignAndCorrect(numerator, denominator);
};
Rational(const int numberUp, int numberDown = 1) : numerator(numberUp),
denominator(numberDown),
isPositive(true) {
CheckSignAndCorrect(numerator, denominator);
};
friend Rational operator+(const Rational &first, const Rational &second);
friend Rational operator*(const Rational &first, const Rational &second);
friend Rational operator-(const Rational &first, const Rational &second);
friend Rational operator/(const Rational &first, const Rational &second);
friend Rational operator%(const Rational &first, const Rational &second);
Rational &operator+=(const Rational &other) {
if (isPositive == other.isPositive)
numerator = numerator * other.denominator + denominator * other.numerator;
else
numerator = numerator * other.denominator - denominator * other.numerator;
denominator = other.denominator * denominator;
CheckSignAndCorrect(*this);
return *this;
};
friend std::istream &operator>>(std::istream &in, Rational &value) {
string s;
in >> s;
string first = "";
for (size_t i; i < s.size(); i++) {
first += s[i];
if (s.at(i) == '/')break;
}
string second = s.substr(first.size() + 1, (s.size() - first.size() - 1));
value = Rational(BigInteger(first), BigInteger(second));
return in;
}
friend std::ostream &operator<<(std::ostream &out, const Rational &value) {
string str = value.toString();
out << str;
return out;
}
Rational &operator-=(const Rational &other) {
if (isPositive != other.isPositive)
numerator = numerator * other.denominator + denominator * other.numerator;
else
numerator = numerator * other.denominator - denominator * other.numerator;
denominator = other.denominator * denominator;
CheckSignAndCorrect(*this);
return *this;
};
Rational &operator*=(const Rational &other) {
numerator *= other.numerator;
denominator *= other.denominator;
isPositive = other.isPositive ? isPositive : !isPositive;
CheckSignAndCorrect(*this);
return *this;
};
Rational &operator/=(const Rational &other) {
BigInteger numeratorOther = other.numerator;
numerator *= other.denominator;
denominator *= numeratorOther;
isPositive = other.isPositive ? isPositive : !isPositive;
CheckSignAndCorrect(*this);
return *this;
};
const Rational operator-() const {
Rational copy = Rational(*this);
copy.isPositive = !copy.isPositive;
copy.MakeShort();
return copy;
};
Rational &operator%=(const Rational &other) {
BigInteger numeratorOther = other.numerator;
*this -= ((*this / other) * other);
isPositive = other.isPositive ? isPositive : !isPositive;
CheckSignAndCorrect(*this);
return *this;
};
bool operator==(const Rational &other) const {
if (this->isPositive == other.isPositive) {
if (this->numerator == other.numerator && this->denominator == other.denominator) {
return true;
} else {
return false;
}
} else {
return false;
}
};
bool operator!=(const Rational &other) const {
return !(*this == other);
};
bool operator<(const Rational &other) const {
if (this->isPositive == other.isPositive) {
return this->numerator * other.denominator < this->denominator * other.numerator;
} else {
return !this->isPositive;
}
};
bool operator<=(const Rational &other) const {
if (this->isPositive == other.isPositive) {
return this->numerator * other.denominator <= this->denominator * other.numerator;
} else {
return !this->isPositive;
}
};
bool operator>(const Rational &other) const {
return !(*this <= other);
};
bool operator>=(const Rational &other) const {
return !(*this < other);
};
//--------------------------------------------------------------------------------------------------------------------//
/*
███████╗██████╗░██████╗░░█████╗░██████╗░ ░█████╗░██╗░░░░░░█████╗░░██████╗███████╗
██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗ ██╔══██╗██║░░░░░██╔══██╗██╔════╝██╔════╝
█████╗░░██████╔╝██████╔╝██║░░██║██████╔╝ ██║░░╚═╝██║░░░░░██║░░██║╚█████╗░█████╗░░
██╔══╝░░██╔══██╗██╔══██╗██║░░██║██╔══██╗ ██║░░██╗██║░░░░░██║░░██║░╚═══██╗██╔══╝░░
███████╗██║░░██║██║░░██║╚█████╔╝██║░░██║ ╚█████╔╝███████╗╚█████╔╝██████╔╝███████╗
╚══════╝╚═╝░░╚═╝╚═╝░░╚═╝░╚════╝░╚═╝░░╚═╝ ░╚════╝░╚══════╝░╚════╝░╚═════╝░╚══════╝
*/
//--------------------------------------------------------------------------------------------------------------------//
const string toString() const {
string result = "", numeratorString = this->numerator.toString(), denominatorString = this->denominator.toString();
if (!this->isPositive)result += "-";
result += numeratorString;
if (denominator != 1 && numerator != 0)
result += '/' + denominatorString;
return result;
};
const string asDecimal(size_t precision = 0) const {
string ans;
BigInteger numeratorCopy = numerator, denominatorCopy = denominator;
if (precision > 0) {
if (numeratorCopy / denominatorCopy == 0) {
BigInteger n = numeratorCopy;
for (size_t i = 0; i < precision; ++i) {
n *= 10;
}
BigInteger result = n / denominatorCopy;
string after = result.toString();
if (after.size() < precision) {
after = string(precision - after.size(), '0') + after;
}
ans = "0." + after;
if (!isPositive && numeratorCopy != 0 && result != 0)ans = '-' + ans;
} else {
if (numeratorCopy % denominatorCopy != 0) {
BigInteger n = numeratorCopy;
for (size_t i = 0; i < precision; ++i) {
n *= 10;
}
BigInteger result = n / denominatorCopy;
ans = result.toString();
ans = ans.substr(0, ans.size() - precision) + '.' +
ans.substr(ans.size() - precision, precision);
if (!isPositive && numeratorCopy != 0)ans = '-' + ans;
} else {
numeratorCopy /= denominatorCopy;
string afterStr;
if (afterStr.size() < precision) {
afterStr = string(precision - afterStr.size(), '0') + afterStr;
}
ans = numeratorCopy.toString() + '.' + afterStr;
if (!isPositive && numeratorCopy != 0)ans = '-' + ans;
}
}
} else {
numeratorCopy /= denominatorCopy;
ans = numeratorCopy.toString();
if (!isPositive && numeratorCopy != 0)ans = '-' + ans;
}
return ans;
}
//--------------------------------------------------------------------------------------------------------------------//
/*
███████╗██████╗░██████╗░░█████╗░██████╗░ ░█████╗░██╗░░░░░░█████╗░░██████╗███████╗
██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗ ██╔══██╗██║░░░░░██╔══██╗██╔════╝██╔════╝
█████╗░░██████╔╝██████╔╝██║░░██║██████╔╝ ██║░░╚═╝██║░░░░░██║░░██║╚█████╗░█████╗░░
██╔══╝░░██╔══██╗██╔══██╗██║░░██║██╔══██╗ ██║░░██╗██║░░░░░██║░░██║░╚═══██╗██╔══╝░░
███████╗██║░░██║██║░░██║╚█████╔╝██║░░██║ ╚█████╔╝███████╗╚█████╔╝██████╔╝███████╗
╚══════╝╚═╝░░╚═╝╚═╝░░╚═╝░╚════╝░╚═╝░░╚═╝ ░╚════╝░╚══════╝░╚════╝░╚═════╝░╚══════╝
*/
//--------------------------------------------------------------------------------------------------------------------//
bool &CheckSignAndCorrect(BigInteger &numberUp, BigInteger &numberDown) {
if (!isPositive) {
numberUp *= -1;
isPositive = 1;
}
numberUp * numberDown < 0 ? isPositive = false : isPositive = true;
if (numberUp < 0) numberUp *= -1;
if (numberDown < 0) numberDown *= -1;
MakeShort();
return isPositive;
}
bool &CheckSignAndCorrect(Rational ©) {
return CheckSignAndCorrect(copy.numerator, copy.denominator);
}
static Rational abs(const Rational &x) {
Rational copy = Rational(x.numerator, x.denominator);
copy.isPositive = true;
return copy;
}
void MakeShort() {
if (this->numerator == 0)this->denominator = 1;
BigInteger x = GCD(this->numerator, this->denominator);
if (x != 0) {
this->numerator /= x;
this->denominator /= x;
}
}
BigInteger GCD(BigInteger a, BigInteger b) {
while (b > 0 && a > 0) {
if (a > b) a %= b;
else b %= a;
}
return a + b;
}
explicit operator double() {
double result = atof((*this).asDecimal(17).c_str());
return result;
}
private:
BigInteger numerator;//числитель
BigInteger denominator;//знаменатель
bool isPositive;//знак
};
Rational operator+(const Rational &first, const Rational &second) {
Rational summa(first);
summa += second;
summa.MakeShort();
return summa;
}
Rational operator*(const Rational &first, const Rational &second) {
Rational resultComposition(first);
resultComposition *= second;
resultComposition.MakeShort();
return resultComposition;
}
Rational operator-(const Rational &first, const Rational &second) {
Rational difference(first);
difference -= second;
difference.MakeShort();
return difference;
}
Rational operator/(const Rational &first, const Rational &second) {
Rational resultDivision(first);
resultDivision /= second;
resultDivision.MakeShort();
return resultDivision;
}
Rational operator%(const Rational &first, const Rational &second) {
Rational resultDivision(first);
resultDivision %= second;
resultDivision.MakeShort();
return resultDivision;
}
//--------------------------------------------------------------------------------------------------------------------//
/*
███████╗██╗░░░██╗███████╗██████╗░██╗░░░██╗████████╗██╗░░██╗██╗███╗░░██╗░██████╗░ ██╗░██████╗
██╔════╝██║░░░██║██╔════╝██╔══██╗╚██╗░██╔╝╚══██╔══╝██║░░██║██║████╗░██║██╔════╝░ ██║██╔════╝
█████╗░░╚██╗░██╔╝█████╗░░██████╔╝░╚████╔╝░░░░██║░░░███████║██║██╔██╗██║██║░░██╗░ ██║╚█████╗░
██╔══╝░░░╚████╔╝░██╔══╝░░██╔══██╗░░╚██╔╝░░░░░██║░░░██╔══██║██║██║╚████║██║░░╚██╗ ██║░╚═══██╗
███████╗░░╚██╔╝░░███████╗██║░░██║░░░██║░░░░░░██║░░░██║░░██║██║██║░╚███║╚██████╔╝ ██║██████╔╝
╚══════╝░░░╚═╝░░░╚══════╝╚═╝░░╚═╝░░░╚═╝░░░░░░╚═╝░░░╚═╝░░╚═╝╚═╝╚═╝░░╚══╝░╚═════╝░ ╚═╝╚═════╝░
██╗███╗░░██╗ ██╗░░░██╗░█████╗░██╗███╗░░██╗ ██╗███╗░░██╗ ████████╗██╗░░██╗██╗░██████╗
██║████╗░██║ ██║░░░██║██╔══██╗██║████╗░██║ ██║████╗░██║ ╚══██╔══╝██║░░██║██║██╔════╝
██║██╔██╗██║ ╚██╗░██╔╝███████║██║██╔██╗██║ ██║██╔██╗██║ ░░░██║░░░███████║██║╚█████╗░
██║██║╚████║ ░╚████╔╝░██╔══██║██║██║╚████║ ██║██║╚████║ ░░░██║░░░██╔══██║██║░╚═══██╗
██║██║░╚███║ ░░╚██╔╝░░██║░░██║██║██║░╚███║ ██║██║░╚███║ ░░░██║░░░██║░░██║██║██████╔╝
╚═╝╚═╝░░╚══╝ ░░░╚═╝░░░╚═╝░░╚═╝╚═╝╚═╝░░╚══╝╚ ═╝╚═╝░░╚══╝░ ░░╚═╝░░░╚═╝░░╚═╝╚═╝╚═════╝░
░██╗░░░░░░░██╗░█████╗░██████╗░██╗░░░░░██████╗░
░██║░░██╗░░██║██╔══██╗██╔══██╗██║░░░░░██╔══██╗
░╚██╗████╗██╔╝██║░░██║██████╔╝██║░░░░░██║░░██║
░░████╔═████║░██║░░██║██╔══██╗██║░░░░░██║░░██║
░░╚██╔╝░╚██╔╝░╚█████╔╝██║░░██║███████╗██████╔╝
░░░╚═╝░░░╚═╝░░░╚════╝░╚═╝░░╚═╝╚══════╝╚═════╝░
*/
//--------------------------------------------------------------------------------------------------------------------//
}
/*
* Проверка на равенство: операторы == и !=.
Конструктор по умолчанию, создающий единичную матрицу. Для неквадратных матриц конструктор по умолчанию не требуется.
Конструктор, создающий матрицу из vector<vector<T> >. Должно быть можно создать матрицу из vector<vector<int> >.
Сложение, вычитание, операторы +=, -=. Сложение и вычитание матриц несоответствующих размеров не должно компилироваться.
Умножение на число.
Умножение матриц, работающие за max(M,N,K)**3. Для квадратных матриц должен поддерживаться еще и оператор *=. Попытка перемножить матрицы несоответствующих размеров должна приводить к ошибке компиляции.
Метод det(), возвращающий определитель матрицы за O(N**3). Взятие определителя от неквадратной матрицы не должно компилироваться.
Метод transposed(), возвращающий транспонированную матрицу.
Метод rank() - вычислить ранг матрицы.
Метод trace() - вычислить след матрицы.
Методы inverted() и invert() - вернуть обратную матрицу и обратить данную матрицу.
Методы getRow(long long) и getColumn(long long), возвращающие std::vector<Field> из соответствующих значений.
К матрице должен быть дважды применим оператор [], причём это должно работать как для неконстантных, так и для константных матриц. В первом случае содержимое матрицы должно быть можно таким способом поменять.
Другие способы изменения содержимого матрицы, кроме описанных выше, должны отсутствовать. Однако не запрещается реализовать дополнительные методы для выполнения каких-либо иных алгебраических операций или для удобства работы, если по названию и сигнатуре этих методов будет без комментариев понятно их действие.
Квадратные матрицы размера N должно быть можно объявлять всего с одним обязательным шаблонным параметром: SquareMatrix<N>.
*/
namespace TOOLS {
/*------------------------------------------------------------------------------------------------*/
typedef long long ll;
ll const sizeOfTree = 2e5 + 1;
ll const INF = 1e18;
/*------------------------------------------------------------------------------------------------*/
namespace FLOWS {
/*------------------------------------------------------------------------------------------------*/
#ifndef IN_OUT
std::ifstream in;
std::ofstream out;
#define IN_OUT 1
#endif //IN_OUT
}
//сдвигает элементы вектора по кругу
template<class T>
T &_slide(T &Vec, int timer = 1) {
T newT;
for (auto x:Vec) {
if (timer == 0)
newT.push_back(x);
else {
Vec.push_back(x);
timer--;
}
}
Vec = newT;
return Vec;
}
//ускоряшка
void _acceleration() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
}
//открываем файлик на считывание
void _file_open() {
using namespace FLOWS;
in.open("in.in");
out.open("out.out");
}
//закрываем файлик
void _file_close() {
using namespace FLOWS;
in.close();
out.close();
}
//берет остаток числа
template<typename T>
const T &mod(T &value, int &N) {
value %= dynamic_cast<T>(N);
return value;
}
/*-------------------------------------------------------------------------------- Prime checkers */
constexpr bool PrimeStatus(int n) {
int i = 2;
while (i <= std::sqrt(n)) {
if (n % i == 0) {
return false;
}
++i;
}
return true;
};
/*------------------------------------------------------------------------------------------------*/
}
namespace FIELDS::GALOIS {
/*------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------------------*/
/* Start --------------------------------------------------------------------------------- Finite */