結果
問題 | No.16 累乗の加算 |
ユーザー | frmon60 |
提出日時 | 2020-04-13 23:47:06 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 1 ms / 5,000 ms |
コード長 | 3,505 bytes |
コンパイル時間 | 12,335 ms |
コンパイル使用メモリ | 390,464 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-10-01 05:58:28 |
合計ジャッジ時間 | 13,241 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,812 KB |
testcase_01 | AC | 1 ms
6,812 KB |
testcase_02 | AC | 1 ms
6,820 KB |
testcase_03 | AC | 1 ms
6,820 KB |
testcase_04 | AC | 1 ms
6,820 KB |
testcase_05 | AC | 1 ms
6,820 KB |
testcase_06 | AC | 1 ms
6,816 KB |
testcase_07 | AC | 1 ms
6,820 KB |
testcase_08 | AC | 1 ms
6,816 KB |
testcase_09 | AC | 1 ms
6,816 KB |
testcase_10 | AC | 1 ms
6,820 KB |
testcase_11 | AC | 1 ms
6,820 KB |
testcase_12 | AC | 1 ms
6,820 KB |
testcase_13 | AC | 1 ms
6,816 KB |
ソースコード
macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { #[allow(unused_mut)] let mut s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::<Vec<char>>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } #[allow(dead_code)] type Pi = (i32, i32); fn pri<T: std::fmt::Display>(x: T) {println!("{}", x);} // Modint const MOD: u32 = 1_000_003; #[derive(Clone, Copy)] struct Mint(u32); impl std::ops::Add for Mint { type Output = Mint; fn add(self, x: Mint) -> Mint { let res = self.0 + x.0; Mint(if res >= MOD {res-MOD} else {res}) } } impl std::ops::AddAssign for Mint { fn add_assign(&mut self, x: Mint) { *self = *self + x; } } impl std::ops::Sub for Mint { type Output = Mint; fn sub(self, x: Mint) -> Mint { let res = self.0 + MOD - x.0; Mint(if res >= MOD {res-MOD} else {res}) } } impl std::ops::SubAssign for Mint { fn sub_assign(&mut self, x: Mint) { *self = *self - x; } } impl std::ops::Mul for Mint { type Output = Mint; fn mul(self, x: Mint) -> Mint {Mint((self.0 as u64 * x.0 as u64 % MOD as u64) as u32)} } impl std::ops::MulAssign for Mint { fn mul_assign(&mut self, x: Mint) { *self = *self * x; } } impl std::ops::Div for Mint { type Output = Mint; fn div(self, x: Mint) -> Mint { let mut a = x.0 as i64; //solves au + mv = 1 and u = a^-1 let mut m = MOD as i64; let mut u = 1i64; let mut v = 0i64; let mut buf: i64; while m != 0 { let t = a / m; a -= t * m; buf = a; a = m; m = buf; u -= t * v; buf = u; u = v; v = buf; } u %= m; if u < 0 {u += m;} self * Mint(u as u32) } } impl std::ops::DivAssign for Mint { fn div_assign(&mut self, x: Mint) { *self = *self / x; } } impl std::fmt::Display for Mint { fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result { write!(f, "{}", self.0) } } #[allow(dead_code)] impl Mint { pub fn pow(self, mut n: u32) -> Mint { let mut res = Mint(1); let mut r = self; while n > 0 { if n & 1 == 1 {res *= r;} r *= r; n >>= 1; } res } pub fn inv(self) -> Mint { assert!(self.0 > 0); self.pow(MOD - 2) } } fn main() { input!{ x: u32, n: usize, a: [u32; n], } let x = Mint(x); let mut ans = Mint(0); for e in a { ans += x.pow(e); } pri(ans); }