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
use std::{
    cmp::Ordering,
    fmt::Display,
    ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, RemAssign, Sub, SubAssign},
    str::FromStr,
};

use crate::Str;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Int {
    // List of digits, represent absolute value of the integer.
    digits: Vec<i8>,

    // Sign of integer, 1 is positive, -1 is negative, and 0 is zero.
    sign: i8,
}

impl Int {
    // Remove leading zeros.
    fn remove_leading_zeros(&mut self) {
        while self.digits.len() > 1 && self.digits.last().unwrap() == &0 {
            self.digits.pop();
        }
    }

    // Add leading zeros.
    fn add_leading_zeros(&mut self, mut n: usize) {
        while n > 0 {
            n -= 1;
            self.digits.push(0);
        }
    }

    // Test whether the characters represent an integer.
    fn is_integer(chars: &str, len: usize) -> bool {
        let have_sign = chars.as_bytes()[0] == b'+' || chars.as_bytes()[0] == b'-';
        if len == 0 || (len == 1 && have_sign) {
            return false;
        }

        for i in usize::from(have_sign)..len {
            if !chars.chars().nth(i).unwrap().is_ascii_digit() {
                return false;
            }
        }

        true
    }

    // Construct an integer with given characters.
    fn construct(&mut self, chars: &str, len: usize) {
        if !Self::is_integer(chars, len) {
            panic!("Error: Wrong integer literal.");
        }

        self.sign = if chars.as_bytes()[0] == b'-' { -1 } else { 1 };
        let s = (chars.as_bytes()[0] == b'+') || (chars.as_bytes()[0] == b'-'); // skip symbol
        for i in (usize::from(s)..len).rev() {
            self.digits.push((chars.as_bytes()[i] - b'0') as i8);
        }

        self.remove_leading_zeros();

        if self.digits.len() == 1 && self.digits[0] == 0 {
            self.sign = 0;
        }
    }

    // Increment the absolute value by 1 quickly.
    fn abs_inc(&mut self) {
        self.digits.push(0); // add a leading zero

        let mut i = 0;
        while self.digits[i] == 9 {
            i += 1;
        }
        self.digits[i] += 1;
        while i != 0 {
            i -= 1;
            self.digits[i] = 0;
        }

        self.remove_leading_zeros();
    }

    // Decrement the absolute value by 1 quickly.
    fn abs_dec(&mut self) {
        let mut i = 0;
        while self.digits[i] == 0 {
            i += 1;
        }
        self.digits[i] -= 1;
        while i != 0 {
            i -= 1;
            self.digits[i] = 9;
        }

        self.remove_leading_zeros();

        // if result is zero, set sign to 0
        self.sign = if self.digits.len() == 1 && self.digits[0] == 0 { 0 } else { self.sign };
    }

    /// Construct a new integer object.
    pub fn new() -> Self {
        Self { digits: [0].to_vec(), sign: 0 }
    }

    /// Count the number of digits in the integer (based 10).
    pub fn digits(&self) -> usize {
        if self.sign == 0 {
            0
        } else {
            self.digits.len()
        }
    }

    /// Determine whether the integer is zero quickly.
    pub fn is_zero(&self) -> bool {
        self.sign == 0
    }

    /// Determine whether the integer is positive quickly.
    pub fn is_positive(&self) -> bool {
        self.sign == 1
    }

    /// Determine whether the integer is negative quickly.
    pub fn is_negative(&self) -> bool {
        self.sign == -1
    }

    /// Determine whether the integer is even quickly.
    pub fn is_even(&self) -> bool {
        self.digits[0] % 2 == 0
    }

    /// Determine whether the integer is odd quickly.
    pub fn is_odd(&self) -> bool {
        self.digits[0] % 2 == 1
    }

    /// Increment the value by 1 quickly.
    pub fn inc(&mut self) -> &Self {
        if self.sign == 1 {
            self.abs_inc();
        } else if self.sign == -1 {
            self.abs_dec();
        }
        // self.sign == 0
        else {
            self.sign = 1;
            self.digits[0] = 1;
        }
        self
    }

    /// Decrement the value by 1 quickly.
    pub fn dec(&mut self) -> &Self {
        if self.sign == 1 {
            self.abs_dec();
        } else if self.sign == -1 {
            self.abs_inc();
        }
        // self.sign == 0
        else {
            self.sign = -1;
            self.digits[0] = 1;
        }
        self
    }

    /// Return the absolute value of self.
    pub fn abs(&self) -> Self {
        if self.sign == -1 {
            -self
        } else {
            self.clone()
        }
    }

    /// Return (self**exp) % module (module = 0 means does not perform module).
    pub fn pow(&self, exp: &Int, module: &Int) -> Self {
        if exp.is_negative() {
            return Self::new();
        }

        // fast power algorithm

        let mut num = self.clone();
        let mut n = exp.clone();
        let mut result = Self::from(1); // self**0 == 1

        while !n.is_zero() {
            if n.is_odd() {
                result = if module.is_zero() { &result * &num } else { &(&result * &num) % module };
            }
            num = if module.is_zero() { &num * &num } else { &(&num * &num) % module };
            n /= &Int::from(2); // integer divide
        }
        result
    }

    /// Return the factorial of self.
    pub fn factorial(&self) -> Self {
        if self.sign == -1 {
            panic!("Error: Negative integer have no factorial.");
        }

        let mut result = Int::from(1); // 0! == 1
        let mut i = self.clone();
        // fast judgement, fast decrement
        while i.is_positive() {
            result *= &i;
            i.dec();
        }
        result
    }

    /// Return the square root of self using Newton's method.
    pub fn sqrt(&self) -> Self {
        if self.sign == -1 {
            panic!("Error: Cannot compute square root of a negative integer.");
        }

        if self.is_zero() {
            return Self::new();
        }
        // can not be omitted, otherwise will enter an infinite loop due to precision problem
        else if self < &Self::from(4) {
            return Self::from(1);
        }

        // as far as possible to reduce the number of iterations
        let mut cur_sqrt = self / &Int::from(2);
        let mut pre_sqrt = Int::from(2);

        while cur_sqrt != pre_sqrt {
            pre_sqrt = cur_sqrt.clone();
            cur_sqrt = &(&cur_sqrt + &(self / &cur_sqrt)) / &Int::from(2);
        }

        cur_sqrt
    }

    /// Calculate the greatest common divisor of two integers using Euclidean algorithm.
    pub fn gcd(int1: &Int, int2: &Int) -> Int {
        let mut a = int1.clone();
        let mut b = int2.clone();

        // a, b = b, a % b until b == 0
        while !b.is_zero() {
            let t = b.clone();
            b = &a % &b;
            a = t;
        }

        a // a is GCD
    }

    /// Calculate the least common multiple of two integers.
    pub fn lcm(int1: &Int, int2: &Int) -> Int {
        if int1.is_zero() || int2.is_zero() {
            return Int::new();
        }

        (int1 * int2) / Int::gcd(int1, int2) // LCM = (int1 * int2) / GCD
    }
}

/*
Construct
*/

impl From<&str> for Int {
    fn from(value: &str) -> Self {
        let mut obj = Self { digits: [].to_vec(), sign: 0 };
        obj.construct(value, value.len());
        obj
    }
}

impl From<Str> for Int {
    fn from(value: Str) -> Self {
        let len = value.len() as usize;
        let mut obj = Self { digits: [].to_vec(), sign: 0 };
        obj.construct(String::from(value).as_str(), len);
        obj
    }
}

impl From<i32> for Int {
    fn from(mut value: i32) -> Self {
        if value == 0 {
            return Self::new();
        }

        // value != 0
        let mut obj = Self { digits: [].to_vec(), sign: 0 };
        obj.sign = if value > 0 { 1 } else { -1 };
        value = value.abs();
        while value > 0 {
            obj.digits.push((value % 10) as i8);
            value /= 10;
        }
        obj
    }
}

#[derive(Debug, PartialEq, Eq)]
pub struct ParseIntError;

impl FromStr for Int {
    type Err = ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = s.trim();

        if !Self::is_integer(s, s.len()) {
            return Err(ParseIntError);
        }

        Ok(Int::from(s))
    }
}

impl Default for Int {
    fn default() -> Self {
        Self::new()
    }
}

/*
Function
*/

impl PartialOrd for Int {
    fn partial_cmp(&self, that: &Self) -> Option<Ordering> {
        if self.sign != that.sign {
            // self is +, that is - or 0
            if self.sign == 1 {
                return Some(Ordering::Greater);
            }
            // self is -, that is + or 0
            else if self.sign == -1 {
                return Some(Ordering::Less);
            }
            // self is 0, that is + or -
            else {
                return if that.sign == 1 { Some(Ordering::Less) } else { Some(Ordering::Greater) };
            }
        }

        // the sign of two integers is the same

        if self.digits.len() != that.digits.len() {
            if self.sign == 1 {
                return if self.digits.len() > that.digits.len() {
                    Some(Ordering::Greater)
                } else {
                    Some(Ordering::Less)
                };
            } else {
                return if self.digits.len() > that.digits.len() {
                    Some(Ordering::Less)
                } else {
                    Some(Ordering::Greater)
                };
            }
        }

        for i in (0..self.digits.len()).rev() {
            if self.digits[i] != that.digits[i] {
                if self.sign == 1 {
                    return if self.digits[i] > that.digits[i] {
                        Some(Ordering::Greater)
                    } else {
                        Some(Ordering::Less)
                    };
                } else {
                    return if self.digits[i] > that.digits[i] {
                        Some(Ordering::Less)
                    } else {
                        Some(Ordering::Greater)
                    };
                }
            }
        }

        Some(Ordering::Equal)
    }
}

impl Neg for &Int {
    type Output = Int;

    fn neg(self) -> Self::Output {
        Int {
            digits: self.digits.clone(),
            sign: -self.sign,
        }
    }
}

impl Neg for Int {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self {
            digits: self.digits,
            sign: -self.sign,
        }
    }
}

impl Add<&Int> for &Int {
    type Output = Int;

    fn add(self, rhs: &Int) -> Self::Output {
        // if one of the operands is zero, just return another one
        if self.sign == 0 || rhs.sign == 0 {
            return if self.sign == 0 { rhs.clone() } else { self.clone() };
        }

        // if the operands are of opposite signs, perform subtraction
        if self.sign == 1 && rhs.sign == -1 {
            return self - &-rhs;
        } else if self.sign == -1 && rhs.sign == 1 {
            return rhs - &-self;
        }

        // the sign of two integers is the same and not zero

        // prepare variables
        let size = std::cmp::max(self.digits.len(), rhs.digits.len()) + 1;

        let mut num1 = self.clone();
        num1.add_leading_zeros(size - 1 - num1.digits.len());

        let mut num2 = rhs.clone();
        num2.add_leading_zeros(size - 1 - num2.digits.len());

        let mut result = Int {
            digits: [0].to_vec(),
            sign: self.sign,
        };

        result.add_leading_zeros(size - 1); // result initially has a 0

        // simulate the vertical calculation
        let a = &num1.digits;
        let b = &num2.digits;
        let c = &mut result.digits;
        for i in 0..(size - 1) {
            c[i] += a[i] + b[i];
            c[i + 1] = c[i] / 10;
            c[i] %= 10;
        }

        // remove leading zeros and return result
        result.remove_leading_zeros();
        result
    }
}

impl Add for Int {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        &self + &rhs
    }
}

impl Sub<&Int> for &Int {
    type Output = Int;

    fn sub(self, rhs: &Int) -> Self::Output {
        // if one of the operands is zero
        if self.sign == 0 || rhs.sign == 0 {
            return if self.sign == 0 { -rhs } else { self.clone() };
        }

        // if the operands are of opposite signs, perform addition
        if self.sign != rhs.sign {
            return self + &-rhs;
        }

        // the sign of two integers is the same and not zero

        // prepare variables
        let size = std::cmp::max(self.digits.len(), rhs.digits.len());

        let mut num1 = self.clone();
        num1.add_leading_zeros(size - num1.digits.len());

        let mut num2 = rhs.clone();
        num2.add_leading_zeros(size - num2.digits.len());

        let mut result = Int {
            digits: [0].to_vec(),
            sign: self.sign,
        };

        // let num1.abs() >= num2.abs()
        if if self.sign == 1 { num1 < num2 } else { num1 > num2 } {
            std::mem::swap(&mut num1, &mut num2);
            result = -result;
        }
        result.add_leading_zeros(size - 1); // result initially has a 0

        // simulate the vertical calculation, assert a >= b
        let a = &mut num1.digits;
        let b = &num2.digits;
        let c = &mut result.digits;
        for i in 0..size {
            // carry
            if a[i] < b[i] {
                a[i + 1] -= 1;
                a[i] += 10;
            }
            c[i] = a[i] - b[i];
        }

        // remove leading zeros
        result.remove_leading_zeros();

        // if result is zero, set sign to 0
        result.sign = if result.digits.len() == 1 && result.digits[0] == 0 { 0 } else { result.sign };

        // return result
        result
    }
}

impl Sub for Int {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        &self - &rhs
    }
}

impl Mul<&Int> for &Int {
    type Output = Int;

    fn mul(self, rhs: &Int) -> Self::Output {
        // if one of the operands is zero, just return zero
        if self.sign == 0 || rhs.sign == 0 {
            return Int::new();
        }

        // the sign of two integers is not zero

        // prepare variables
        let size = self.digits.len() + rhs.digits.len();

        let mut result = Int {
            digits: [0].to_vec(),
            sign: if self.sign == rhs.sign { 1 } else { -1 }, // the sign is depends on the sign of operands
        };
        result.add_leading_zeros(size - 1); // result initially has a 0

        // simulate the vertical calculation
        let a = &self.digits;
        let b = &rhs.digits;
        let c = &mut result.digits;
        for i in 0..a.len() {
            for j in 0..b.len() {
                c[i + j] += a[i] * b[j];
                c[i + j + 1] += c[i + j] / 10;
                c[i + j] %= 10;
            }
        }

        // remove leading zeros and return
        result.remove_leading_zeros();
        result
    }
}

impl Mul for Int {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        &self * &rhs
    }
}

impl Div<&Int> for &Int {
    type Output = Int;

    fn div(self, rhs: &Int) -> Self::Output {
        // if rhs is zero, panic
        if rhs.sign == 0 {
            panic!("Error: Divide by zero.");
        }

        // if self is zero or self.abs() < rhs.abs(), just return zero
        if self.sign == 0 || self.digits.len() < rhs.digits.len() {
            return Int::new();
        }

        // the sign of two integers is not zero

        // prepare variables
        let size = self.digits.len() - rhs.digits.len() + 1;

        let mut num1 = self.clone().abs();

        let mut tmp = Int { digits: [0].to_vec(), sign: 1 }; // intermediate variable for rhs * 10^i

        let mut result = Int {
            digits: [0].to_vec(),
            sign: if self.sign == rhs.sign { 1 } else { -1 },
        };
        result.add_leading_zeros(size - 1); // result initially has a 0

        // calculation
        let b = &rhs.digits;
        let c = &mut result.digits;
        for i in (0..size).rev() {
            tmp.digits = [0].repeat(i);
            tmp.digits.append(&mut b.clone()); // tmp = rhs * 10^i in O(N)

            // <= 9 loops, so O(1)
            while num1 >= tmp {
                c[i] += 1;
                num1 -= &tmp;
            }
        }

        // if result is zero, set sign to 0
        result.sign = if result.digits.len() == 1 && result.digits[0] == 0 { 0 } else { result.sign };

        // remove leading zeros and return
        result.remove_leading_zeros();
        result
    }
}

impl Div for Int {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        &self / &rhs
    }
}

impl Rem<&Int> for &Int {
    type Output = Int;

    fn rem(self, rhs: &Int) -> Self::Output {
        self - &(&(self / rhs) * rhs)
    }
}

impl Rem for Int {
    type Output = Self;

    fn rem(self, rhs: Self) -> Self::Output {
        &self % &rhs
    }
}

impl AddAssign<&Int> for Int {
    fn add_assign(&mut self, rhs: &Int) {
        *self = &*self + rhs;
    }
}

impl SubAssign<&Int> for Int {
    fn sub_assign(&mut self, rhs: &Int) {
        *self = &*self - rhs;
    }
}

impl MulAssign<&Int> for Int {
    fn mul_assign(&mut self, rhs: &Int) {
        *self = &*self * rhs;
    }
}

impl DivAssign<&Int> for Int {
    fn div_assign(&mut self, rhs: &Int) {
        *self = &*self / rhs;
    }
}

impl RemAssign<&Int> for Int {
    fn rem_assign(&mut self, rhs: &Int) {
        *self = &*self % rhs;
    }
}

/*
Display
*/

impl Display for Int {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.sign == -1 {
            write!(f, "-")?;
        }

        for i in (0..self.digits.len()).rev() {
            write!(f, "{}", (self.digits[i] as u8 + b'0') as char)?;
        }

        Ok(())
    }
}