アルゴリズムと数学 050

https://atcoder.jp/contests/math-and-algorithm/tasks/math_and_algorithm_aq

繰り返し2乗法というやつですね。

// Power
#![allow(non_snake_case)]


//////////////////// constants ////////////////////

const D: u64 = 1000000007;


//////////////////// library ////////////////////

fn read<T: std::str::FromStr>() -> T {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).ok();
    line.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
            .map(|e| e.parse().ok().unwrap()).collect()
}


//////////////////// process ////////////////////

fn read_input() -> (u64, u64) {
    let v = read_vec();
    (v[0], v[1])
}

fn f(a: u64, b: u64) -> u64 {
    if b == 1 {
        a
    }
    else if b % 2 == 0 {
        let n = f(a, b / 2);
        n * n % D
    }
    else {
        f(a, b - 1) * a % D
    }
}

fn main() {
    let (a, b) = read_input();
    println!("{}", f(a, b))
}