耗时半天编写出来的高精度模板(支持 + - * / % += -= *= /= %=等原生运算符)

UPD On 7/10/2024 支持负数,但除法仍有问题

UPD On 7/12/2024 修了除法和减法的问题

UPD On 8/4/2025 加了两个版本

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
#ifndef LZJ_BIGNUM
#define LZJ_BIGNUM

#include <string.h>
#include <algorithm>
#include <stdexcept>
#include <iostream>

struct big_num {
int a[150];
int& operator[](int i) {
if (i > a[0] || i < 1) throw std::out_of_range("Index out of bounds");
return a[i];
}
int& operator()(int i) {
if (i > a[0] || i < 1) throw std::out_of_range("Index out of bounds");
return a[a[0] - i + 1];
}

friend std::istream& operator>>(std::istream& in, big_num& a) {
std::string s;
in >> s;
reverse(s.begin(), s.end());
size_t last_non_zero = s.find_last_not_of('0');
if (last_non_zero != std::string::npos) {
s = s.substr(0, last_non_zero + 1);
} else {
s = "0";
}
a.a[0] = s.empty() ? 0 : s.size();
for (int i = 1; i <= a.a[0]; ++i) a.a[i] = s[i-1] - '0';
return in;
}

friend std::ostream& operator<<(std::ostream& out, const big_num& a) {
if (a.a[0] == 0) {
out << '0';
} else {
for (int i = a.a[0]; i >= 1; --i) out << a.a[i];
}
return out;
}

big_num() { memset(a, 0, sizeof a); }

template<typename T>
big_num(T b) {
memset(a, 0, sizeof a);
int top = 0;
do {
a[++top] = b % 10;
b /= 10;
} while (b > 0);
a[0] = top;
}

big_num(const big_num& oth) {
memcpy(a, oth.a, sizeof a);
}

template<typename T>
void operator = (T b) {
memset(a, 0, sizeof a);
int top = 0;
while (b) {
this->a[++top] = b % 10;
b /= 10;
}
this->a[0] = top;
}

std::string change_to_string() const {
std::string s;
if (a[0] == 0) {
s += '0';
return s;
}
for (int i = a[0]; i >= 1; --i) {
s += (a[i] + '0');
}
return s;
}

void string_to(std::string s) {
reverse(s.begin(), s.end());
a[0] = s.size();
for (int i = 1, j = a[0] - 1; i <= a[0]; ++i, --j) a[i] = s[j] - '0';
}

friend big_num operator+(const big_num& a, const big_num& b) {
big_num ans;
ans.a[0] = std::max(a.a[0], b.a[0]);
for (int i = 1; i <= ans.a[0]; ++i) {
ans.a[i] += a.a[i] + b.a[i];
ans.a[i+1] += ans.a[i] / 10;
ans.a[i] %= 10;
}
if (ans.a[ans.a[0]+1] > 0) ans.a[0]++;
return ans;
}

friend void operator += (big_num& a, const big_num& b) {
a = a + b;
}

friend big_num operator-(const big_num& a, const big_num& b) {
if (a < b) throw std::invalid_argument("Negative result not supported");
big_num ans;
ans.a[0] = a.a[0];
for (int i = 1; i <= ans.a[0]; ++i) {
ans.a[i] += a.a[i] - b.a[i];
if (ans.a[i] < 0) {
ans.a[i] += 10;
ans.a[i+1]--;
}
}
while (ans.a[0] > 0 && ans.a[ans.a[0]] == 0) ans.a[0]--;
if (ans.a[0] == 0) ans.a[0] = 1;
return ans;
}

friend void operator -= (big_num& a, const big_num& b) {
a = a - b;
}

friend big_num operator*(const big_num& a, const big_num& b) {
big_num c;
c.a[0] = a.a[0] + b.a[0];
for (int i = 1; i <= a.a[0]; i++) {
for (int j = 1; j <= b.a[0]; j++) {
c.a[i+j-1] += a.a[i] * b.a[j];
}
}

for (int k = 1; k <= c.a[0]; ++k) {
c.a[k+1] += c.a[k] / 10;
c.a[k] %= 10;
}
while (c.a[0] > 0 && c.a[c.a[0]] == 0) c.a[0]--;
if (c.a[0] == 0) c.a[0] = 1;
return c;
}

friend void operator *= (big_num& a, const big_num& b) {
a = a * b;
}

long long change_to_long_long() const {
const long long MAX_LL = std::numeric_limits<long long>::max();
long long s = 0;
for (int i = a[0]; i >= 1; --i) {
if (s > (MAX_LL - a[i]) / 10) {
throw std::overflow_error("Overflow in conversion");
}
s = s * 10 + a[i];
}
return s;
}

friend big_num operator / (const big_num& a, const long long& b) {
if (b == 0) throw std::invalid_argument("Cannot be divided by zero");
big_num ans;
std::string a1 = a.change_to_string();
long long b1 = b;
std::string c1;
int aa[155]{}, c[155]{};
int lena = a1.size(), lenc = 1;
for (int i = 0; i < lena; i++) {
aa[i + 1] = a1[i] - '0';
}
for (int i = 1; i <= lena; i++) {
c[i] = aa[i] / b1;
aa[i + 1] += aa[i] % b1 * 10;
}
while (lenc < lena && c[lenc] == 0) lenc++;
while (lenc <= lena) c1 += c[lenc++] + '0';
reverse(c1.begin(), c1.end());
ans.string_to(c1);
return ans;
}

friend void operator /= (big_num& a, const long long& b) {
if (b == 0) {
throw 1919810;
}
a = a / b;
}

friend big_num operator % (const big_num& a, const long long& b) {
big_num bb;
bb = b;
big_num tmp = a / b;
big_num tmp2 = tmp * bb;
return a - tmp2;
}

friend void operator %= (big_num& a, const long long& b) {
a = a % b;
}

friend bool operator < (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] < b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] < b.a[i];
}
return false;
}

friend bool operator > (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] > b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] > b.a[i];
}
return false;
}

friend bool operator <= (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] <= b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] <= b.a[i];
}
return true;
}

friend bool operator >= (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] >= b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] >= b.a[i];
}
return false;
}

friend bool operator == (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return false;
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return false;
}
return true;
}

friend bool operator != (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return true;
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return true;
}
return false;
}
};

#endif
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
#ifndef LZJ_BIGNUM 
#define LZJ_BIGNUM

#include <string.h>
#include <algorithm>
#include <stdexcept>
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>

template<int K = 4>
struct big_num {
static const int BASE = pow(10, K);
static const int WIDTH = K;

int a[150];
int& operator[](int i) {
if (i > a[0] || i < 1) throw std::out_of_range("Index out of bounds");
return a[i];
}
int& operator()(int i) {
if (i > a[0] || i < 1) throw std::out_of_range("Index out of bounds");
return a[a[0] - i + 1];
}

friend std::istream& operator>>(std::istream& in, big_num& a) {
std::string s;
in >> s;
reverse(s.begin(), s.end());
size_t last_non_zero = s.find_last_not_of('0');
if (last_non_zero != std::string::npos) {
s = s.substr(0, last_non_zero + 1);
} else {
s = "0";
}

a.a[0] = 0;
for (int i = 0, len = s.size(); i < len; i += WIDTH) {
int end = std::min(i + WIDTH, len);
std::string part = s.substr(i, end - i);
reverse(part.begin(), part.end());
a.a[++a.a[0]] = std::stoi(part);
}
return in;
}

friend std::ostream& operator<<(std::ostream& out, const big_num& a) {
if (a.a[0] == 0) {
out << '0';
} else {
out << a.a[a.a[0]];
for (int i = a.a[0] - 1; i >= 1; --i) {
out << std::setw(WIDTH) << std::setfill('0') << a.a[i];
}
}
return out;
}

big_num() { memset(a, 0, sizeof a); }

template<typename T>
big_num(T b) {
memset(a, 0, sizeof a);
int top = 0;
do {
a[++top] = b % BASE;
b /= BASE;
} while (b > 0);
a[0] = top;
}

big_num(const big_num& oth) {
memcpy(a, oth.a, sizeof a);
}

template<typename T>
void operator = (T b) {
memset(a, 0, sizeof a);
int top = 0;
while (b) {
this->a[++top] = b % BASE;
b /= BASE;
}
this->a[0] = top;
}

std::string change_to_string() const {
std::string s;
if (a[0] == 0) {
s += '0';
return s;
}
s += std::to_string(a[a[0]]);
for (int i = a[0] - 1; i >= 1; --i) {
std::string part = std::to_string(a[i]);
s += std::string(WIDTH - part.size(), '0') + part;
}
return s;
}

void string_to(std::string s) {
reverse(s.begin(), s.end());
a[0] = 0;
for (int i = 0, len = s.size(); i < len; i += WIDTH) {
int end = std::min(i + WIDTH, len);
std::string part = s.substr(i, end - i);
reverse(part.begin(), part.end());
a[++a[0]] = std::stoi(part);
}
}

friend big_num operator+(const big_num& a, const big_num& b) {
big_num ans;
ans.a[0] = std::max(a.a[0], b.a[0]);
for (int i = 1; i <= ans.a[0]; ++i) {
ans.a[i] += a.a[i] + b.a[i];
ans.a[i+1] += ans.a[i] / BASE;
ans.a[i] %= BASE;
}
if (ans.a[ans.a[0]+1] > 0) ans.a[0]++;
return ans;
}

friend void operator += (big_num& a, const big_num& b) {
a = a + b;
}

friend big_num operator-(const big_num& a, const big_num& b) {
if (a < b) throw std::invalid_argument("Negative result not supported");
big_num ans;
ans.a[0] = a.a[0];
for (int i = 1; i <= ans.a[0]; ++i) {
ans.a[i] += a.a[i] - b.a[i];
if (ans.a[i] < 0) {
ans.a[i] += BASE;
ans.a[i+1]--;
}
}
while (ans.a[0] > 0 && ans.a[ans.a[0]] == 0) ans.a[0]--;
if (ans.a[0] == 0) ans.a[0] = 1;
return ans;
}

friend void operator -= (big_num& a, const big_num& b) {
a = a - b;
}

friend big_num operator*(const big_num& a, const big_num& b) {
big_num c;
c.a[0] = a.a[0] + b.a[0];
for (int i = 1; i <= a.a[0]; i++) {
for (int j = 1; j <= b.a[0]; j++) {
c.a[i+j-1] += a.a[i] * b.a[j];
}
}

for (int k = 1; k <= c.a[0]; ++k) {
c.a[k+1] += c.a[k] / BASE;
c.a[k] %= BASE;
}
while (c.a[0] > 0 && c.a[c.a[0]] == 0) c.a[0]--;
if (c.a[0] == 0) c.a[0] = 1;
return c;
}

friend void operator *= (big_num& a, const big_num& b) {
a = a * b;
}

long long change_to_long_long() const {
const long long MAX_LL = std::numeric_limits<long long>::max();
long long s = 0;
for (int i = a[0]; i >= 1; --i) {
if (s > (MAX_LL - a[i]) / BASE) {
throw std::overflow_error("Overflow in conversion");
}
s = s * BASE + a[i];
}
return s;
}

friend big_num operator / (const big_num& a, const long long& b) {
if (b == 0) throw std::invalid_argument("Cannot be divided by zero");
big_num ans;
std::string a1 = a.change_to_string();
long long b1 = b;
std::string c1;
int aa[155]{}, c[155]{};
int lena = a1.size(), lenc = 1;
for (int i = 0; i < lena; i++) {
aa[i + 1] = a1[i] - '0';
}
for (int i = 1; i <= lena; i++) {
c[i] = aa[i] / b1;
aa[i + 1] += aa[i] % b1 * 10;
}
while (lenc < lena && c[lenc] == 0) lenc++;
while (lenc <= lena) c1 += c[lenc++] + '0';
reverse(c1.begin(), c1.end());
ans.string_to(c1);
return ans;
}

friend void operator /= (big_num& a, const long long& b) {
if (b == 0) {
throw 1919810;
}
a = a / b;
}

friend big_num operator % (const big_num& a, const long long& b) {
big_num bb;
bb = b;
big_num tmp = a / b;
big_num tmp2 = tmp * bb;
return a - tmp2;
}

friend void operator %= (big_num& a, const long long& b) {
a = a % b;
}

friend bool operator < (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] < b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] < b.a[i];
}
return false;
}

friend bool operator > (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] > b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] > b.a[i];
}
return false;
}

friend bool operator <= (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] <= b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] <= b.a[i];
}
return true;
}

friend bool operator >= (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] >= b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] >= b.a[i];
}
return true;
}

friend bool operator == (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return false;
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return false;
}
return true;
}

friend bool operator != (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return true;
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return true;
}
return false;
}
};

#endif
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
#ifndef BIG_NUM_H
#define BIG_NUM_H
#ifndef _GLIBCXX_CCTYPE
#include<bits/stdc++.h>
#endif
#ifdef HAVE_NEGATIVE
namespace std {
struct big_num {
short a[150]; bool nag;
short int& operator [] (int i) {
if (i > this->a[0]) {
throw 114514;
}
return this->a[i];
}
short int& operator () (int i) {
if (i > this->a[0]) {
throw 114514;
}
return this->a[this->a[0] - i + 1];
}
friend istream& operator >> (istream& in, big_num& a) {
string s;
in >> s;
if (s[0] == '-') a.nag = 1, s.erase(s.begin());
reverse(s.begin(), s.end());
a.a[0] = s.size();
for (int i = 1; i <= a.a[0]; ++i) a.a[i] = s[i - 1] ^ 48;
return in;
}
friend ostream& operator << (ostream& out, const big_num& a) {
if (a.a[0] == 0) {
out << '0';
}
else {
if (a.nag) out << '-';
for (int i = a.a[0]; i >= 1; --i) {
out << a.a[i];
}
}
return out;
}
big_num() { memset(a, 0, sizeof a); nag = 0; }
template<typename T>
big_num(T b) {
memset(a, 0, sizeof a);
if (b < 0) this->nag = 1, b = -b;
else this->nag = 0;
int top = 0;
while (b) {
this->a[++top] = b % 10;
b /= 10;
}
this->a[0] = top;
}
big_num(const big_num& oth) {
memcpy(a, oth.a, sizeof a);
nag = oth.nag;
}
template<typename T>
void operator = (T b) {
memset(a, 0, sizeof a);
if (b < 0) this->nag = 1, b = -b;
int top = 0;
while (b) {
this->a[++top] = b % 10;
b /= 10;
}
this->a[0] = top;
}
string change_to_string() const {
string s;
if (a[0] == 0) {
s += '0';
return s;
}
if (nag) s += '-';
for (int i = a[0]; i >= 1; --i) {
s += (a[i] + '0');
}
return s;
}
void string_to(string s) {
if (s[0] == '-') nag = 1, s.erase(s.begin());
reverse(s.begin(), s.end());
a[0] = s.size();
for (int i = 1, j = a[0] - 1; i <= a[0]; ++i, --j) a[i] = s[j] - '0';
}
long long change_to_long_long() const {
long long s = 0;
if (a[0] == 0) {
return s;
}
for (int i = a[0]; i >= 1; --i) {
s *= 10;
s += a[i];
}
if (nag) s = -s;
return s;
}
big_num re() const {
big_num oth(*this);
oth.nag = !nag;
return oth;
}
friend big_num operator + (const big_num& a, const big_num& b) {
if (a > 0 && b < 0) return a - b.re();
if (a < 0 && b > 0) return b - a.re();
if (a < 0 && b < 0) return (a.re() + b.re()).re();
big_num ans;
ans.a[0] = a.a[0] > b.a[0] ? a.a[0] : b.a[0];
for (register int i = 1; i <= ans.a[0]; ++i) {
ans.a[i] += a.a[i] + b.a[i];
ans.a[i + 1] += ans.a[i] / 10;
ans.a[i] %= 10;
}
if (ans.a[ans.a[0] + 1]) ans.a[0]++;
return ans;
}
friend void operator += (big_num& a, const big_num& b) {
a = a + b;
}
friend big_num operator - (const big_num& a, const big_num& b) {
if (a < 0 && b < 0) return (a.re() - b.re()).re();
if (a < b) return (b - a).re();
if (a > 0 && b < 0) return a + b.re();
if (a < 0 && b > 0) return a + b.re();
big_num ans;
ans.a[0] = a.a[0];
for (register int i = 1; i <= ans.a[0]; ++i) {
ans.a[i] += a.a[i] - b.a[i];
if (ans.a[i] < 0) --ans.a[i + 1], ans.a[i] += 10;
}
while (ans.a[ans.a[0]] == 0 && ans.a[0] > 0) --ans.a[0];
return ans;
}
friend void operator -= (big_num& a, const big_num& b) {
a = a - b;
}
friend big_num operator * (const big_num& a, const big_num& b) {
big_num c;
c.a[0] = a.a[0] + b.a[0];
c.nag = a.nag ^ b.nag;
for (int i = 1; i <= a.a[0]; i++) {
for (int j = 1; j <= b.a[0]; j++) {
c.a[i + j - 1] += a.a[i] * b.a[j];
if (c.a[i + j - 1] >= 10) {
c.a[i + j] += c.a[i + j - 1] / 10;
c.a[i + j - 1] = c.a[i + j - 1] % 10;
}
}
}
while (c.a[c.a[0]] == 0 && c.a[0] > 1) c.a[0]--;
return c;
}
friend void operator *= (big_num& a, const big_num& b) {
a = a * b;
}
friend big_num operator / (const big_num& a, const long long& b) {
if (b == 0) {
throw 1919810;
}
big_num ans;
string a1;
if (b < 0) a1 = a.re().change_to_string();
else a1 = a.change_to_string();
long long b1 = b;
string c1;
int aa[155]{}, c[155]{};
int lena = a1.size(), lenc = 1;
for (int i = 0; i < lena; i++) {
aa[i + 1] = a1[i] - '0';
}
for (int i = 1; i <= lena; i++) {
c[i] = aa[i] / b1;
aa[i + 1] += aa[i] % b1 * 10;
}
while (lenc < lena && c[lenc] == 0) lenc++;
while (lenc <= lena) c1 += c[lenc++] + '0';
reverse(c1.begin(), c1.end());
if (a.nag ^ (b < 0)) c1.insert(c1.begin(), '-');
ans.string_to(c1);
return ans;
}
friend void operator /= (big_num& a, const long long& b) {
if (b == 0) {
throw 1919810;
}
a = a / b;
}
//本人太弱了,不会编高精除以高精,只能用低精代替了
friend big_num operator / (const big_num& a, const big_num& bb) {
if (bb.a[1] == 0 && bb.a[0] == 1) {
throw 1919810;
}
long long b;
if (bb.nag) b = bb.re().change_to_long_long();
else b = bb.change_to_long_long();
string a1;
if (a.nag) a1 = a.re().change_to_string();
else a1 = a.change_to_string();
long long b1 = b;
string c1;
big_num ans;
int aa[155]{}, c[155]{};
int lena = a1.size(), lenc = 1;
for (int i = 0; i < lena; i++) {
aa[i + 1] = a1[i] - '0';
}
for (int i = 1; i <= lena; i++) {
c[i] = aa[i] / b1;
aa[i + 1] += aa[i] % b1 * 10;
}
while (lenc < lena && c[lenc] == 0) lenc++;
while (lenc <= lena) c1 += c[lenc++] + '0';
reverse(c1.begin(), c1.end());
if (a.nag ^ (bb.nag)) c1.insert(c1.begin(), '-');
ans.string_to(c1);
return ans;
}
friend void operator /= (big_num& a, big_num& b) {
if (b[1] == 0 && b[0] == 1) {
throw 1919810;
}
a = a / b;
}
friend big_num operator % (const big_num& a, const long long& b) {
big_num bb;
bb = b;
big_num tmp = a / b;
big_num tmp2 = tmp * bb;
return a - tmp2;
}
friend void operator %= (big_num& a, const long long& b) {
a = a % b;
}
friend bool operator < (const big_num& a, const big_num& b) {
if (!a.nag && !b.nag) {
if (a.a[0] != b.a[0]) return a.a[0] < b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] < b.a[i];
}
return false;
}
if (a.nag && !b.nag) {
return true;
}
if (!a.nag && b.nag) {
return false;
}
if (a.nag && b.nag) {
return b.re() < a.re();
}
}
friend bool operator > (const big_num& a, const big_num& b) {
if (!a.nag && !b.nag) {
if (a.a[0] != b.a[0]) return a.a[0] > b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] > b.a[i];
}
return false;
}
if (a.nag && !b.nag) {
return false;
}
if (!a.nag && b.nag) {
return true;
}
if (a.nag && b.nag) {
return b.re() > a.re();
}
}
friend bool operator <= (const big_num& a, const big_num& b) {
if (!a.nag && !b.nag) {
if (a.a[0] != b.a[0]) return a.a[0] <= b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] <= b.a[i];
}
return true;
}
if (a.nag && !b.nag) {
return true;
}
if (!a.nag && b.nag) {
return false;
}
if (a.nag && b.nag) {
return b.re() <= a.re();
}
}
friend bool operator >= (const big_num& a, const big_num& b) {
if (!a.nag && !b.nag) {
if (a.a[0] != b.a[0]) return a.a[0] >= b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] >= b.a[i];
}
return false;
}
if (a.nag && !b.nag) {
return false;
}
if (!a.nag && b.nag) {
return true;
}
if (a.nag && b.nag) {
return b.re() >= a.re();
}
}
friend bool operator == (const big_num& a, const big_num& b) {
if (a.nag != b.nag) return false;
if (a.a[0] != b.a[0]) return false;
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return false;
}
return true;
}
friend bool operator != (const big_num& a, const big_num& b) {
if (a.nag != b.nag) return true;
if (a.a[0] != b.a[0]) return true;
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return true;
}
return false;
}
};
}
#else
namespace std {
struct big_num {
short a[150];
short int& operator [] (int i) {
if (i > this->a[0]) {
throw 114514;
}
return this->a[i];
}
short int& operator () (int i) {
if (i > this->a[0]) {
throw 114514;
}
return this->a[this->a[0] - i + 1];
}
friend istream& operator >> (istream& in, big_num& a) {
string s;
in >> s;
reverse(s.begin(), s.end());
a.a[0] = s.size();
for (int i = 1; i <= a.a[0]; ++i) a.a[i] = s[i - 1] ^ 48;
return in;
}
friend ostream& operator << (ostream& out, const big_num& a) {
if (a.a[0] == 0) {
out << '0';
}
else {
for (int i = a.a[0]; i >= 1; --i) {
out << a.a[i];
}
}
return out;
}
big_num() { memset(a, 0, sizeof a); }
template<typename T>
big_num(T b) {
memset(a, 0, sizeof a);
int top = 0;
while (b) {
this->a[++top] = b % 10;
b /= 10;
}
this->a[0] = top;
}
big_num(const big_num& oth) {
memcpy(a, oth.a, sizeof a);
}
template<typename T>
void operator = (T b) {
memset(a, 0, sizeof a);
int top = 0;
while (b) {
this->a[++top] = b % 10;
b /= 10;
}
this->a[0] = top;
}
string change_to_string() const {
string s;
if (a[0] == 0) {
s += '0';
return s;
}
for (int i = a[0]; i >= 1; --i) {
s += (a[i] + '0');
}
return s;
}
void string_to(string s) {
reverse(s.begin(), s.end());
a[0] = s.size();
for (int i = 1, j = a[0] - 1; i <= a[0]; ++i, --j) a[i] = s[j] - '0';
}
long long change_to_long_long() const {
long long s = 0;
if (a[0] == 0) {
return s;
}
for (int i = a[0]; i >= 1; --i) {
s *= 10;
s += a[i];
}
return s;
}
friend big_num operator + (const big_num& a, const big_num& b) {
big_num ans;
ans.a[0] = a.a[0] > b.a[0] ? a.a[0] : b.a[0];
for (register int i = 1; i <= ans.a[0]; ++i) {
ans.a[i] += a.a[i] + b.a[i];
ans.a[i + 1] += ans.a[i] / 10;
ans.a[i] %= 10;
}
if (ans.a[ans.a[0] + 1]) ans.a[0]++;
return ans;
}
friend void operator += (big_num& a, const big_num& b) {
a = a + b;
}
friend big_num operator - (const big_num& a, const big_num& b) {
big_num ans;
ans.a[0] = a.a[0];
for (register int i = 1; i <= ans.a[0]; ++i) {
ans.a[i] += a.a[i] - b.a[i];
if (ans.a[i] < 0) --ans.a[i + 1], ans.a[i] += 10;
}
while (ans.a[ans.a[0]] == 0 && ans.a[0] > 0) --ans.a[0];
return ans;
}
friend void operator -= (big_num& a, const big_num& b) {
a = a - b;
}
friend big_num operator * (const big_num& a, const big_num& b) {
big_num c;
c.a[0] = a.a[0] + b.a[0];
for (int i = 1; i <= a.a[0]; i++) {
for (int j = 1; j <= b.a[0]; j++) {
c.a[i + j - 1] += a.a[i] * b.a[j];
if (c.a[i + j - 1] >= 10) {
c.a[i + j] += c.a[i + j - 1] / 10;
c.a[i + j - 1] = c.a[i + j - 1] % 10;
}
}
}
while (c.a[c.a[0]] == 0 && c.a[0] > 1) c.a[0]--;
return c;
}
friend void operator *= (big_num& a, const big_num& b) {
a = a * b;
}
friend big_num operator / (const big_num& a, const long long& b) {
if (b == 0) {
throw 1919810;
}
big_num ans;
string a1 = a.change_to_string();
long long b1 = b;
string c1;
int aa[155]{}, c[155]{};
int lena = a1.size(), lenc = 1;
for (int i = 0; i < lena; i++) {
aa[i + 1] = a1[i] - '0';
}
for (int i = 1; i <= lena; i++) {
c[i] = aa[i] / b1;
aa[i + 1] += aa[i] % b1 * 10;
}
while (lenc < lena && c[lenc] == 0) lenc++;
while (lenc <= lena) c1 += c[lenc++] + '0';
reverse(c1.begin(), c1.end());
ans.string_to(c1);
return ans;
}
friend void operator /= (big_num& a, const long long& b) {
if (b == 0) {
throw 1919810;
}
a = a / b;
}
//本人太弱了,不会编高精除以高精,只能用低精代替了
friend big_num operator / (const big_num& a, const big_num& bb) {
if (bb.a[1] == 0 && bb.a[0] == 1) {
throw 1919810;
}
long long b = bb.change_to_long_long();
string a1 = a.change_to_string();
long long b1 = b;
string c1;
big_num ans;
int aa[155]{}, c[155]{};
int lena = a1.size(), lenc = 1;
for (int i = 0; i < lena; i++) {
aa[i + 1] = a1[i] - '0';
}
for (int i = 1; i <= lena; i++) {
c[i] = aa[i] / b1;
aa[i + 1] += aa[i] % b1 * 10;
}
while (lenc < lena && c[lenc] == 0) lenc++;
while (lenc <= lena) c1 += c[lenc++] + '0';
reverse(c1.begin(), c1.end());
ans.string_to(c1);
return ans;
}
friend void operator /= (big_num& a, big_num& b) {
if (b[1] == 0 && b[0] == 1) {
throw 1919810;
}
a = a / b;
}
friend big_num operator % (const big_num& a, const long long& b) {
big_num bb;
bb = b;
big_num tmp = a / b;
big_num tmp2 = tmp * bb;
return a - tmp2;
}
friend void operator %= (big_num& a, const long long& b) {
a = a % b;
}
friend bool operator < (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] < b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] < b.a[i];
}
return false;
}
friend bool operator > (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] > b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] > b.a[i];
}
return false;
}
friend bool operator <= (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] <= b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] <= b.a[i];
}
return true;
}
friend bool operator >= (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return a.a[0] >= b.a[0];
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return a.a[i] >= b.a[i];
}
return false;
}
friend bool operator == (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return false;
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return false;
}
return true;
}
friend bool operator != (const big_num& a, const big_num& b) {
if (a.a[0] != b.a[0]) return true;
for (int i = a.a[0]; i >= 1; --i) {
if (a.a[i] != b.a[i]) return true;
}
return false;
}
};
}
#endif
#endif