結果
問題 | No.573 a^2[i] = a[i] |
ユーザー |
|
提出日時 | 2017-10-11 21:10:10 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 42 ms / 2,000 ms |
コード長 | 3,809 bytes |
コンパイル時間 | 12,403 ms |
コンパイル使用メモリ | 392,432 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-17 09:45:21 |
合計ジャッジ時間 | 14,132 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 47 |
ソースコード
#[allow(unused_imports)]use std::cmp::{max, min};#[allow(unused_imports)]use std::collections::{HashMap, HashSet};mod util {use std::io::stdin;use std::str::FromStr;use std::fmt::Debug;#[allow(dead_code)]pub fn line() -> String {let mut line: String = String::new();stdin().read_line(&mut line).unwrap();line.trim().to_string()}#[allow(dead_code)]pub fn get<T: FromStr>() -> Twhere<T as FromStr>::Err: Debug,{let mut line: String = String::new();stdin().read_line(&mut line).unwrap();line.trim().parse().unwrap()}#[allow(dead_code)]pub fn gets<T: FromStr>() -> Vec<T>where<T as FromStr>::Err: Debug,{let mut line: String = String::new();stdin().read_line(&mut line).unwrap();line.split_whitespace().map(|t| t.parse().unwrap()).collect()}#[allow(dead_code)]pub fn get2<T: FromStr, U: FromStr>() -> (T, U)where<T as FromStr>::Err: Debug,<U as FromStr>::Err: Debug,{let mut line: String = String::new();stdin().read_line(&mut line).unwrap();let mut iter = line.split_whitespace();(iter.next().unwrap().parse().unwrap(),iter.next().unwrap().parse().unwrap(),)}#[allow(dead_code)]pub fn get3<S: FromStr, T: FromStr, U: FromStr>() -> (S, T, U)where<S as FromStr>::Err: Debug,<T as FromStr>::Err: Debug,<U as FromStr>::Err: Debug,{let mut line: String = String::new();stdin().read_line(&mut line).unwrap();let mut iter = line.split_whitespace();(iter.next().unwrap().parse().unwrap(),iter.next().unwrap().parse().unwrap(),iter.next().unwrap().parse().unwrap(),)}}#[allow(unused_macros)]macro_rules! debug {($x: expr) => {println!("{}: {:?}", stringify!($x), $x)}}// (gcd, x, y)fn extgcd(a: i64, b: i64) -> (i64, i64, i64) {if b == 0 {(a, 1, 0)} else {let (gcd, x, y) = extgcd(b, a % b);(gcd, y, x - (a / b) * y)}}fn mod_pow(x: u64, n: u64, m: u64) -> u64 {let mut res = 1;let mut x = x;let mut n = n;while n > 0 {if n & 1 == 1 {res = (res * x) % m;}x = (x * x) % m;n = n >> 1;}res}fn mod_inverse(a: u64, m: u64) -> u64 {let (_, x, _) = extgcd(a as i64, m as i64);((m as i64 + x) as u64 % m) % m}fn fact_table(len: usize, m: u64) -> Vec<u64> {let mut res = vec![1; len];for i in 1..len {res[i] = (i as u64 * res[i - 1]) % m;}res}// (a mod p, e when n! = a p^e)fn mod_fact(n: u64, p: u64, fact: &Vec<u64>) -> (u64, u64) {if n == 0 {(1, 0)} else {let (a, b) = mod_fact(n / p, p, fact);let e = b + n / p;if n / p % 2 != 0 {(a * (p - fact[(n % p) as usize]) % p, e)} else {(a * fact[(n % p) as usize] % p, e)}}}fn mod_comb(n: u64, k: u64, p: u64, fact: &Vec<u64>) -> u64 {if n < k {0} else {let (a1, e1) = mod_fact(n, p, fact);let (a2, e2) = mod_fact(k, p, fact);let (a3, e3) = mod_fact(n - k, p, fact);if e1 > e2 + e3 {0} else {a1 * mod_inverse(a2 * a3 % p, p) % p}}}const M: u64 = 1000000007;fn main() {let n: u64 = util::get();let fact = fact_table(200000, M);let mut a = 0;for i in 1..n + 1 {let p = mod_comb(n, i, M, &fact) * mod_pow(i, n - i, M) % M;a = (a + p) % M;}println!("{}", a);}