Skip to main content

rustgym/leetcode/
_372_super_pow.rs

1struct Solution;
2
3impl Solution {
4    fn super_pow(a: i32, mut b: Vec<i32>) -> i32 {
5        let a = a % 1337;
6        if let Some(last) = b.pop() {
7            Self::pow_mod(Self::super_pow(a, b) % 1337, 10) * Self::pow_mod(a, last) % 1337
8        } else {
9            1
10        }
11    }
12
13    fn pow_mod(a: i32, k: i32) -> i32 {
14        let mut res = 1;
15        for _ in 0..k {
16            res *= a;
17            res %= 1337;
18        }
19        res
20    }
21}
22
23#[test]
24fn test() {
25    let a = 2;
26    let b = vec![3];
27    let res = 8;
28    assert_eq!(Solution::super_pow(a, b), res);
29    let a = 2;
30    let b = vec![1, 0];
31    let res = 1024;
32    assert_eq!(Solution::super_pow(a, b), res);
33}