結果

問題 No.573 a^2[i] = a[i]
ユーザー tanzakutanzaku
提出日時 2017-10-06 23:22:05
言語 Rust
(1.72.1)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 2,932 bytes
コンパイル時間 994 ms
コンパイル使用メモリ 153,452 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-10 17:25:59
合計ジャッジ時間 2,582 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 0 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 0 ms
4,376 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,380 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,376 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 1 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,380 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 1 ms
4,376 KB
testcase_45 AC 2 ms
4,376 KB
testcase_46 AC 15 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: fields `n` and `inv` are never read
  --> Main.rs:8:5
   |
6  | struct Combination {
   |        ----------- fields in this struct
7  |     modular: usize,
8  |     n: usize,
   |     ^
9  |     fact: Vec<usize>,
10 |     inv: Vec<usize>,
   |     ^^^
   |
   = note: `Combination` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

#[allow(dead_code)]
const MOD: usize = 1_000000000 + 7;

#[derive(Debug)]
struct Combination {
    modular: usize,
    n: usize,
    fact: Vec<usize>,
    inv: Vec<usize>,
    invfact: Vec<usize>,
}

impl Combination {
    fn new(modular: usize, n: usize) -> Self {
        let mut fact = vec![0; n+1];
        let mut inv = vec![0; n+1];
        let mut invfact = vec![0; n+1];
        inv[1] = 1;
		fact[0] = 1;
		invfact[0] = 1;
        for i in 2..inv.len() {
            inv[i] = inv[modular % i] * (modular - modular / i) % modular;
        }
        for i in 1..inv.len() {
			fact[i] = i * fact[i-1] % modular;
			invfact[i] = inv[i] * invfact[i-1] % modular;
        }
        Combination {
            modular,
            n,
            fact,
            inv,
            invfact,
        }
    }

    fn comb(&self, n: usize, r: usize) -> usize {
        self.fact[n] * self.invfact[n-r] % self.modular * self.invfact[r] % self.modular
    }
}

fn modpow(n: usize, r: usize) -> usize {
    if r == 0 { return 1; }
    if r == 1 { return n; }
    let x = modpow(n, r / 2);
    if r % 2 == 0 {
        x * x % MOD
    } else {
        x * x % MOD * n % MOD
    }
}

fn main() {
    let mut cin = Scanner::new();
    // Σ C(n,i)*i^(n-i)
    let n = cin.next_usize();
    let c = Combination::new(MOD, n+1);
    let mut ans = 0;
    for i in 1..(n+1) {
        // println!("{} {} {}", i, c.comb(n, i), modpow(i, n-i));
        ans += c.comb(n, i) * modpow(i, n-i) % MOD;
    }
    println!("{}", ans % MOD);
}


struct Scanner {
    reader: std::io::Stdin,
    tokens: std::collections::VecDeque<String>,
}

impl Scanner {
    fn new() -> Self {
        Scanner {
            reader: std::io::stdin(),
            tokens: std::collections::VecDeque::new(),
        }
    }

    fn next(&mut self) -> String {
        if self.tokens.is_empty() {
            let mut s = String::new();
            loop {
                self.reader.read_line(&mut s).ok();
                let s = s.trim();
                if s.len() != 0 {
                    for it in s.split_whitespace() {
                        self.tokens.push_back(it.into())
                    }
                    break;
                }
            }
        }
        self.tokens.pop_front().unwrap()
    }

    fn next_generics<T>(&mut self) -> T where
        T: std::str::FromStr + std::marker::Copy,
        <T as std::str::FromStr>::Err: std::fmt::Debug
    {
        self.next().parse().unwrap()
    }
    
    #[allow(dead_code)] fn next_i32(&mut self) -> i32 { self.next_generics::<i32>() }
    #[allow(dead_code)] fn next_i64(&mut self) -> i64 { self.next_generics::<i64>() }
    #[allow(dead_code)] fn next_u64(&mut self) -> u64 { self.next_generics::<u64>() }
    #[allow(dead_code)] fn next_usize(&mut self) -> usize { self.next_generics::<usize>() }
    #[allow(dead_code)] fn next_isize(&mut self) -> isize { self.next_generics::<isize>() }
}

0