use std::fs::File; use std::io::Read; // use std::cmp; fn pow_speedt_with_mod(mut p: i64, mut q: i64, m: i64) -> i64 { p %= m; let mut r = p; let mut ret: i64 = 1; while q > 0 { ret *= if q % 2 == 1 {r} else {1}; r *= r; r %= m; q /= 2; ret %= m; } return ret; } fn main() { let inputstatus = 0; let mut buf = String::new(); let filename = "inputrust.txt"; if inputstatus == 2 { let mut f = File::open(filename).expect("file not found"); f.read_to_string(&mut buf) .expect("something went wrong reading the file"); } else { std::io::stdin().read_to_string(&mut buf).unwrap(); } let mut iter = buf.split_whitespace(); const CMAX: usize = 100000; let a: i64 = iter.next().unwrap().parse().unwrap(); let b: i64 = iter.next().unwrap().parse().unwrap(); let c: i64 = iter.next().unwrap().parse().unwrap(); let cycle = a / c; let modulo = a % c; let mut ans : i64 = 0; let mut sum1 : i64 = 0; let mut sum2 : i64 = 0; let mut v: [i64; CMAX] = [0; CMAX]; for i in 0..c as usize { v[i] = pow_speedt_with_mod((i + 1) as i64, b, c) as i64; } for v in &v[..c as usize] { sum1 += v; } sum1 %= c; for v in &v[..modulo as usize] { sum2 += v; } sum2 %= c; ans = (sum1 * cycle + sum2) % c; println!("{}", ans); }