結果

問題 No.2636 No Waiting in Vain
ユーザー nautnaut
提出日時 2024-02-20 20:23:01
言語 Rust
(1.77.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 2,927 bytes
コンパイル時間 15,192 ms
コンパイル使用メモリ 242,032 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-02-20 20:23:18
合計ジャッジ時間 15,970 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 6 ms
6,676 KB
testcase_05 AC 6 ms
6,676 KB
testcase_06 AC 6 ms
6,676 KB
testcase_07 AC 6 ms
6,676 KB
testcase_08 AC 5 ms
6,676 KB
testcase_09 AC 6 ms
6,676 KB
testcase_10 AC 6 ms
6,676 KB
testcase_11 AC 5 ms
6,676 KB
testcase_12 AC 6 ms
6,676 KB
testcase_13 AC 6 ms
6,676 KB
testcase_14 AC 6 ms
6,676 KB
testcase_15 AC 6 ms
6,676 KB
testcase_16 AC 6 ms
6,676 KB
testcase_17 AC 6 ms
6,676 KB
testcase_18 AC 5 ms
6,676 KB
testcase_19 AC 6 ms
6,676 KB
testcase_20 AC 6 ms
6,676 KB
testcase_21 AC 6 ms
6,676 KB
testcase_22 AC 6 ms
6,676 KB
testcase_23 AC 6 ms
6,676 KB
testcase_24 AC 1 ms
6,676 KB
testcase_25 AC 1 ms
6,676 KB
testcase_26 AC 1 ms
6,676 KB
testcase_27 AC 1 ms
6,676 KB
testcase_28 AC 1 ms
6,676 KB
testcase_29 AC 1 ms
6,676 KB
testcase_30 AC 1 ms
6,676 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 1 ms
6,676 KB
testcase_33 AC 1 ms
6,676 KB
testcase_34 AC 1 ms
6,676 KB
testcase_35 AC 1 ms
6,676 KB
testcase_36 AC 1 ms
6,676 KB
testcase_37 AC 1 ms
6,676 KB
testcase_38 AC 1 ms
6,676 KB
testcase_39 AC 1 ms
6,676 KB
testcase_40 AC 1 ms
6,676 KB
testcase_41 AC 1 ms
6,676 KB
testcase_42 AC 1 ms
6,676 KB
testcase_43 AC 1 ms
6,676 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 N = input!(usize);
    let K = input!(usize);
    let S = input!(String).chars().collect::<Vec<_>>();

    let mut cnt = 0;
    let mut ncnt = 0;

    for i in 0..N {
        if S[i] == 'N' {
            if (i >= 1 && S[i - 1] != 'C') || i == 0 {
                cnt += 1;
            }

            ncnt += 1;
        }
    }

    let mut ans = 0;

    for k in 1..=N {
        if ncnt < K + k {
            break;
        }

        ans += combination::combination(cnt, k);
    }

    writeln!(out, "{}", ans % 998_244_353);
}

pub mod combination {
    const M: usize = 3 * 100_010;
    const MOD: usize = 998_244_353;

    const fn comb() -> ([usize; M], [usize; M], [usize; M]) {
        let mut f = [0; M];
        let mut f_inv = [0; M];
        let mut inv = [0; M];

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

        let mut i = 2;

        while i < M {
            f[i] = (f[i - 1] * i) % MOD;
            inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
            f_inv[i] = f_inv[i - 1] * inv[i] % MOD;

            i += 1;
        }

        return (f, f_inv, inv);
    }

    const _F: [usize; M] = comb().0;
    const _F_INV: [usize; M] = comb().1;
    const _INV: [usize; M] = comb().2;

    /// return nCk % MOD
    pub const fn combination(n: usize, k: usize) -> usize {
        if n < k {
            return 0;
        } else {
            return _F[n] * (_F_INV[k] * _F_INV[n - k] % MOD) % MOD;
        }
    }

    /// return n! % MOD
    pub const fn factorial(n: usize) -> usize {
        return _F[n];
    }
}

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())
            }
        }
    }
}
0