結果

問題 No.2527 H and W
ユーザー nautnaut
提出日時 2023-11-04 02:01:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 3,607 bytes
コンパイル時間 11,574 ms
コンパイル使用メモリ 166,760 KB
実行使用メモリ 25,292 KB
最終ジャッジ日時 2023-11-04 02:02:02
合計ジャッジ時間 3,302 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
25,292 KB
testcase_01 AC 19 ms
25,292 KB
testcase_02 AC 20 ms
25,292 KB
testcase_03 AC 20 ms
25,292 KB
testcase_04 AC 30 ms
25,292 KB
testcase_05 AC 31 ms
25,292 KB
testcase_06 AC 31 ms
25,292 KB
testcase_07 AC 21 ms
25,292 KB
testcase_08 AC 20 ms
25,292 KB
testcase_09 AC 21 ms
25,292 KB
testcase_10 AC 21 ms
25,292 KB
testcase_11 AC 30 ms
25,292 KB
testcase_12 AC 28 ms
25,292 KB
testcase_13 AC 30 ms
25,292 KB
testcase_14 AC 23 ms
25,292 KB
testcase_15 AC 31 ms
25,292 KB
testcase_16 AC 20 ms
25,292 KB
testcase_17 AC 26 ms
25,292 KB
testcase_18 AC 22 ms
25,292 KB
testcase_19 AC 21 ms
25,292 KB
testcase_20 AC 23 ms
25,292 KB
testcase_21 AC 22 ms
25,292 KB
testcase_22 AC 22 ms
25,292 KB
testcase_23 AC 22 ms
25,292 KB
testcase_24 AC 22 ms
25,292 KB
testcase_25 AC 21 ms
25,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let H = input!(usize);
    let W = input!(usize);
    let K = input!(usize);

    const MOD: usize = 998_244_353;
    let mut ans = 0;

    let CT = CalcCombination::init(1_000_010, MOD);

    let divs = calc_divisors(K);

    for d in divs {
        if d == K / d {
            if H >= d && W >= d {
                ans += 2 * CT.C(H, H - d) * CT.C(W, W - d);
            }
        } else {
            // H - h = d, W - w = K / d
            if H >= d && W >= K / d {
                ans += CT.C(H, H - d) * CT.C(W, W - K / d);
            }

            // // W - w = d, H - h = K / d
            if W >= d && H >= K / d {
                ans += CT.C(W, W - d) * CT.C(H, H - K / d);
            }
        }

        ans %= MOD;

        // println!("{} {}", d, ans);
    }

    ans *= 499122177;
    ans %= MOD;

    writeln!(out, "{}", ans);
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}

fn calc_divisors(n: usize) -> Vec<usize> {
    let mut ret = vec![];

    for i in 1..=n {
        if i * i > n {
            break;
        }

        if n % i != 0 {
            continue;
        }

        ret.push(i);

        if n / i != i {
            ret.push(n / i);
        }
    }

    ret.sort();
    return ret;
}

#[allow(non_snake_case, dead_code)]
struct CalcCombination {
    max_value: usize,
    MOD: usize,
    f: Vec<usize>,
    fi: Vec<usize>,
    inv: Vec<usize>,
}

impl CalcCombination {
    #[allow(non_snake_case, dead_code)]
    fn init(max_value: usize, MOD: usize) -> Self {
        let mut f = vec![0; max_value + 1];
        let mut fi = vec![0; max_value + 1];
        let mut inv = vec![0; max_value + 1];

        f[0] = 1;
        f[1] = 1;
        fi[0] = 1;
        fi[1] = 1;
        inv[1] = 1;

        for i in 2..=max_value {
            f[i] = (f[i - 1] * i) % MOD;
            inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
            fi[i] = fi[i - 1] * inv[i] % MOD;
        }

        return Self {
            max_value: max_value,
            MOD: MOD,
            f: f,
            fi: fi,
            inv: inv,
        };
    }

    #[allow(non_snake_case, dead_code)]
    fn C(&self, N: usize, K: usize) -> usize {
        if N < K {
            return 0;
        } else {
            return self.f[N] * (self.fi[K] * self.fi[N - K] % self.MOD) % self.MOD;
        }
    }
}
0