結果

問題 No.498 ワープクリスタル (給料日編)
ユーザー hatoohatoo
提出日時 2017-10-15 13:48:07
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 4,513 bytes
コンパイル時間 16,335 ms
コンパイル使用メモリ 388,764 KB
実行使用メモリ 63,884 KB
最終ジャッジ日時 2024-11-17 17:42:54
合計ジャッジ時間 13,867 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 33 ms
63,648 KB
testcase_05 AC 2 ms
6,820 KB
testcase_06 AC 31 ms
63,652 KB
testcase_07 AC 36 ms
63,816 KB
testcase_08 AC 33 ms
63,768 KB
testcase_09 AC 32 ms
63,708 KB
testcase_10 AC 3 ms
6,820 KB
testcase_11 AC 8 ms
11,624 KB
testcase_12 AC 2 ms
6,816 KB
testcase_13 AC 3 ms
6,820 KB
testcase_14 AC 2 ms
6,820 KB
testcase_15 AC 2 ms
6,820 KB
testcase_16 AC 2 ms
6,816 KB
testcase_17 AC 2 ms
6,816 KB
testcase_18 AC 32 ms
63,712 KB
testcase_19 AC 31 ms
63,808 KB
testcase_20 AC 32 ms
63,664 KB
testcase_21 AC 32 ms
63,680 KB
testcase_22 AC 33 ms
63,644 KB
testcase_23 AC 33 ms
63,580 KB
testcase_24 AC 33 ms
63,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#[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>() -> T
    where
        <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_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 (gx, gy, k): (i64, i64, usize) = util::get3();
    let xyn: Vec<(i64, i64, usize)> = (0..k).map(|_| util::get3()).collect();

    let fact = fact_table(100000, M);
    let mut stack: Vec<(i64, i64, [usize; 5])> = Vec::new();
    stack.push((0, 0, [0; 5]));
    let mut ans = if gx == 0 && gy == 0 { 1 } else { 0 };

    for i in 0..k {
        let (x, y, n) = xyn[i];
        let mut next = stack.clone();
        for j in 1..n + 1 {
            let dx = x * j as i64;
            let dy = y * j as i64;
            for &(cx, cy, nums) in &stack {
                let mut cpy = nums.clone();
                cpy[i] = j;
                let nx = cx + dx;
                let ny = cy + dy;
                if nx == gx && ny == gy {
                    let mut m = cpy.iter().sum::<usize>();
                    let mut p = 1;
                    for l in 0..i + 1 {
                        p = p * mod_comb(m as u64, cpy[l] as u64, M, &fact) % M;
                        m -= cpy[l];
                    }
                    ans = (ans + p) % M;
                }
                next.push((nx, ny, cpy));
            }
        }
        stack = next;
    }

    println!("{}", ans);
}
0