結果
| 問題 |
No.16 累乗の加算
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-06-15 13:06:22 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 1 ms / 5,000 ms |
| コード長 | 3,342 bytes |
| コンパイル時間 | 12,231 ms |
| コンパイル使用メモリ | 378,948 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-30 14:46:38 |
| 合計ジャッジ時間 | 13,121 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 14 |
ソースコード
use std::io::*;
use std::ops::*;
use std::str::FromStr;
use utils::*;
pub fn main() {
let i = stdin();
let mut o = Vec::new();
run(i.lock(), &mut o);
stdout().write_all(&o).unwrap();
}
fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
let mut i = CpReader::new(i);
let (x, n) = i.read2::<usize, usize>();
let a = i.read_vec::<usize>(n);
writeln!(o, "{}", solve(x, &a)).unwrap();
}
fn solve(x: usize, a: &[usize]) -> usize {
let x = ModInt::new(x);
let mut r = ModInt::new(0);
for &a in a {
r += x.pow(a);
}
r.0
}
mod utils {
use super::*;
pub struct CpReader<R: BufRead> {
r: R,
s: String,
}
macro_rules! fn_read {
{$f:ident($($v:ident: $t:ident),*)} => {
pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
let i = &mut self.read_line().split(' ');
$(
let $v = i.next().unwrap().parse().ok().unwrap();
)*
($($v),*)
}
};
}
impl<R: BufRead> CpReader<R> {
pub fn new(r: R) -> Self {
CpReader {
r: r,
s: String::new(),
}
}
pub fn read_line(&mut self) -> &str {
self.s.clear();
self.r.read_line(&mut self.s).unwrap();
self.s.trim()
}
fn_read! { read2(v1: T1, v2: T2) }
pub fn read_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
self.read_line()
.split(' ')
.take(n)
.map(|x| x.parse().ok().unwrap())
.collect()
}
}
#[derive(Debug, Clone, Copy)]
pub struct ModInt(pub usize);
const MOD_BASE: usize = 1000003;
impl ModInt {
pub fn new(n: usize) -> Self {
ModInt(n % MOD_BASE)
}
pub fn pow(self, mut exp: usize) -> Self {
let mut b = self;
let mut r = ModInt(1);
while exp != 0 {
if exp % 2 == 1 {
r *= b;
}
exp /= 2;
b = b * b;
}
r
}
}
macro_rules! ModInt_op {
(($ot:ident, $f:ident), ($ot_a:ident, $f_a:ident), $($rhs:ty, $e:expr,)*) => {
$(
impl $ot<$rhs> for ModInt {
type Output = Self;
fn $f(self, rhs: $rhs) -> Self {
Self::new(($e)(self, rhs))
}
}
impl $ot_a<$rhs> for ModInt {
fn $f_a(&mut self, rhs: $rhs) {
*self = Self::new(($e)(*self, rhs))
}
}
)*
}
}
ModInt_op!(
(Add, add),
(AddAssign, add_assign),
ModInt,
|l: ModInt, r: ModInt| l.0 + r.0,
usize,
|l: ModInt, r: usize| l.0 + r,
);
ModInt_op!(
(Mul, mul),
(MulAssign, mul_assign),
ModInt,
|l: ModInt, r: ModInt| l.0 * r.0,
usize,
|l: ModInt, r: usize| l.0 * r,
);
ModInt_op!(
(Sub, sub),
(SubAssign, sub_assign),
ModInt,
|l: ModInt, r: ModInt| MOD_BASE + l.0 - r.0,
usize,
|l: ModInt, r: usize| MOD_BASE + l.0 - r,
);
}