結果

問題 No.1414 東大文系数学2021第2問改
ユーザー ikdikd
提出日時 2021-03-01 21:40:24
言語 Rust
(1.77.0)
結果
AC  
実行時間 390 ms / 2,000 ms
コード長 3,171 bytes
コンパイル時間 575 ms
コンパイル使用メモリ 167,392 KB
実行使用メモリ 236,400 KB
最終ジャッジ日時 2024-04-14 03:51:57
合計ジャッジ時間 9,545 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 376 ms
236,176 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 342 ms
236,240 KB
testcase_05 AC 366 ms
236,284 KB
testcase_06 AC 339 ms
236,400 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 342 ms
236,196 KB
testcase_10 AC 390 ms
236,228 KB
testcase_11 AC 349 ms
236,228 KB
testcase_12 AC 344 ms
236,204 KB
testcase_13 AC 350 ms
236,192 KB
testcase_14 AC 373 ms
236,192 KB
testcase_15 AC 341 ms
236,268 KB
testcase_16 AC 341 ms
236,328 KB
testcase_17 AC 340 ms
236,352 KB
testcase_18 AC 342 ms
236,252 KB
testcase_19 AC 369 ms
236,296 KB
testcase_20 AC 369 ms
236,256 KB
testcase_21 AC 340 ms
233,388 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 348 ms
236,252 KB
testcase_24 AC 342 ms
236,220 KB
testcase_25 AC 346 ms
236,240 KB
testcase_26 AC 349 ms
236,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let stdin = std::io::stdin();
    let mut rd = ProconReader::new(stdin.lock());

    let n: usize = rd.get();
    let m: usize = rd.get();
    let k: usize = rd.get();

    let mo = 998244353;
    let (fac, inv_fac) = factorials(n + 1, mo);
    let binom = |a: usize, b: usize| {
        if a < b {
            0
        } else {
            fac[a] * inv_fac[b] % mo * inv_fac[a - b] % mo
        }
    };
    let mut ans = 0;
    for i in (1..).take_while(|&i| i * k <= m) {
        let c = binom(n - i * k, n - m) * binom(n - m + 1, i) % mo;
        if i % 2 == 1 {
            ans = (ans + c) % mo;
        } else {
            ans = (ans + mo - c) % mo;
        }
    }
    println!("{}", ans);
}

pub fn factorials(size: usize, mo: u64) -> (Vec<u64>, Vec<u64>) {
    let mut fac = vec![0; size];
    let mut inv = vec![0; size];
    let mut inv_fac = vec![0; size];
    fac[0] = 1;
    fac[1] = 1;
    inv[1] = 1;
    inv_fac[0] = 1;
    inv_fac[1] = 1;
    for i in 2..size {
        let i_u64 = i as u64;
        fac[i] = fac[i - 1] * i_u64 % mo;
        inv[i] = ((mo - inv[(mo as usize) % i]) * (mo / i_u64)).rem_euclid(mo);
        inv_fac[i] = inv_fac[i - 1] * inv[i] % mo;
    }
    (fac, inv_fac)
}

pub struct ProconReader<R> {
    r: R,
    l: String,
    i: usize,
}

impl<R: std::io::BufRead> ProconReader<R> {
    pub fn new(reader: R) -> Self {
        Self {
            r: reader,
            l: String::new(),
            i: 0,
        }
    }
    pub fn get<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        <T as std::str::FromStr>::Err: std::fmt::Debug,
    {
        self.skip_blanks();
        assert!(self.i < self.l.len()); // remain some character
        assert_ne!(&self.l[self.i..=self.i], " ");
        let rest = &self.l[self.i..];
        let len = rest.find(' ').unwrap_or_else(|| rest.len());
        // parse self.l[self.i..(self.i + len)]
        let val = rest[..len]
            .parse()
            .unwrap_or_else(|e| panic!("{:?}, attempt to read `{}`", e, rest));
        self.i += len;
        val
    }
    fn skip_blanks(&mut self) {
        loop {
            match self.l[self.i..].find(|ch| ch != ' ') {
                Some(j) => {
                    self.i += j;
                    break;
                }
                None => {
                    let mut buf = String::new();
                    let num_bytes = self
                        .r
                        .read_line(&mut buf)
                        .unwrap_or_else(|_| panic!("invalid UTF-8"));
                    assert!(num_bytes > 0, "reached EOF :(");
                    self.l = buf
                        .trim_end_matches('\n')
                        .trim_end_matches('\r')
                        .to_string();
                    self.i = 0;
                }
            }
        }
    }
    pub fn get_vec<T>(&mut self, n: usize) -> Vec<T>
    where
        T: std::str::FromStr,
        <T as std::str::FromStr>::Err: std::fmt::Debug,
    {
        (0..n).map(|_| self.get()).collect()
    }
    pub fn get_chars(&mut self) -> Vec<char> {
        self.get::<String>().chars().collect()
    }
}
0