結果
問題 | No.117 組み合わせの数 |
ユーザー | reiyw |
提出日時 | 2018-10-31 05:53:53 |
言語 | Rust (1.77.0 + proconio) |
結果 |
AC
|
実行時間 | 194 ms / 5,000 ms |
コード長 | 3,856 bytes |
コンパイル時間 | 14,441 ms |
コンパイル使用メモリ | 379,768 KB |
実行使用メモリ | 40,200 KB |
最終ジャッジ日時 | 2024-11-19 11:18:12 |
合計ジャッジ時間 | 14,599 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ソースコード
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)*) => { #[allow(non_snake_case)] #[allow(unused_mut)] let mut $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(unused_macros)] macro_rules! debug { ($($a:expr),*) => { eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*); } } pub fn gcd_ext(a: u64, b: u64) -> (u64, i64, i64) { let (mut x, mut x_old) = (0i64, 1i64); let (mut y, mut y_old) = (1i64, 0i64); let (mut r, mut r_old) = (b as i64, a as i64); let mut q: i64; let mut t: i64; while r != 0 { q = r_old / r; t = r; r = r_old - q * r; r_old = t; t = x; x = x_old - q * x; x_old = t; t = y; y = y_old - q * y; y_old = t; } (r_old as u64, x_old, y_old) } pub fn modular_multiplicative_inverse(a: u64, m: u64) -> u64 { let (_, mut x, _) = gcd_ext(a, m); x %= m as i64; if x < 0 { x += m as i64; } x as u64 } pub fn factorial_table_mod(max: u32, _mod: u64) -> Vec<u64> { let mut table = vec![0; (max + 1) as usize]; table[0] = 1; for i in 0..(max as usize) { table[i + 1] = table[i] * ((i + 1) as u64) % _mod; } table } pub fn inv_factorial_table_mod(max: u32, max_fac: u64, _mod: u64) -> Vec<u64> { let mut table = vec![0; (max + 1) as usize]; table[max as usize] = modular_multiplicative_inverse(max_fac, _mod); for i in (0..(max as usize)).rev() { table[i] = table[i + 1] * ((i + 1) as u64) % _mod; } table } fn main() { input! { t: usize, queries: [String; t], } let _mod = 10u64.pow(9) + 7; let n_max = 10u32.pow(6) * 2; let fac = factorial_table_mod(n_max, _mod); let fac_inv = inv_factorial_table_mod(n_max, fac[n_max as usize], _mod); for s in queries { let v: Vec<_> = s.split(|c| c == '(' || c == ')' || c == ',').collect(); let n: usize = v[1].parse().unwrap(); let k: usize = v[2].parse().unwrap(); if v[0] == "C" && n >= k { let a = fac[n]; let b = fac_inv[k]; let c = fac_inv[n - k]; let bc = (b * c) % _mod; println!("{}", (a * bc) % _mod); } else if v[0] == "P" && n >= k { let a = fac[n]; let b = fac_inv[n - k]; println!("{}", (a * b) % _mod); } else if v[0] == "H" && n > 0 { let a = fac[n + k - 1]; let b = fac_inv[k]; let c = fac_inv[n - 1]; let bc = (b * c) % _mod; println!("{}", (a * bc) % _mod); } else if v[0] == "H" && n == 0 && k == 0 { println!("1"); } else { println!("0"); } } }