結果

問題 No.2388 At Least K-Characters
ユーザー 👑 MizarMizar
提出日時 2023-07-12 16:30:50
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 83 ms / 4,000 ms
コード長 4,974 bytes
コンパイル時間 14,144 ms
コンパイル使用メモリ 394,280 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-07-05 03:36:00
合計ジャッジ時間 17,042 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 0 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,948 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 68 ms
6,944 KB
testcase_17 AC 62 ms
6,944 KB
testcase_18 AC 69 ms
6,944 KB
testcase_19 AC 69 ms
6,944 KB
testcase_20 AC 74 ms
6,944 KB
testcase_21 AC 83 ms
6,940 KB
testcase_22 AC 81 ms
6,940 KB
testcase_23 AC 82 ms
6,940 KB
testcase_24 AC 76 ms
6,940 KB
testcase_25 AC 70 ms
6,940 KB
testcase_26 AC 77 ms
6,940 KB
testcase_27 AC 77 ms
6,944 KB
testcase_28 AC 81 ms
6,944 KB
testcase_29 AC 73 ms
6,940 KB
testcase_30 AC 73 ms
6,940 KB
testcase_31 AC 72 ms
6,944 KB
testcase_32 AC 70 ms
6,944 KB
testcase_33 AC 70 ms
6,944 KB
testcase_34 AC 78 ms
6,944 KB
testcase_35 AC 83 ms
6,940 KB
testcase_36 AC 78 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::prelude::*;
fn main() {
    let mut input = String::new();
    std::io::stdin().read_to_string(&mut input).unwrap();
    const KMAX: usize = 26;
    let mut tokens = input.split_ascii_whitespace();
    let n = tokens.next().unwrap().parse::<usize>().unwrap();
    let m = tokens.next().unwrap().parse::<usize>().unwrap();
    let k = tokens.next().unwrap().parse::<usize>().unwrap();
    let s = tokens.next().unwrap();
    if n < 1 || n != s.len() || n > m || m > 500000 || k < 1 || k > KMAX {
        panic!();
    }
    let mut kbits = 0u32;
    let mut dp = [MInt(0); KMAX];
    let mut result = MInt(0);
    for c in s.bytes() {
        let mut ndp = [MInt(0); KMAX];
        let ci = (c - b'a') as usize;
        if ci >= KMAX {
            panic!();
        }
        for j in 0..ci {
            let kt = (kbits | (1 << j)).count_ones() - 1;
            ndp[kt as usize] += MInt(1);
        }
        kbits |= 1 << ci;
        if kbits.count_ones() >= (k as u32) {
            result += MInt(1);
        }
        for j in 1..KMAX {
            let je = dp[j - 1];
            ndp[j - 1] += MInt(j as u32) * je;
            ndp[j] += MInt((KMAX - j) as u32) * je;
        }
        ndp[KMAX - 1] += MInt(KMAX as u32) * dp[KMAX - 1];
        for &je in ndp[(k - 1)..KMAX].iter() {
            result += je;
        }
        dp = ndp;
    }
    if kbits.count_ones() >= (k as u32) {
        result -= MInt(1);
    }
    for _ in n..m {
        let mut ndp = [MInt(0); KMAX];
        for j in 1..KMAX {
            let je = dp[j - 1];
            ndp[j - 1] += MInt(j as u32) * je;
            ndp[j] += MInt((KMAX - j) as u32) * je;
        }
        ndp[KMAX - 1] += MInt(KMAX as u32) * dp[KMAX - 1];
        for &je in ndp[(k - 1)..KMAX].iter() {
            result += je;
        }
        dp = ndp;
    }
    println!("{}", result.val());
}
pub trait ModInt32Static {
    const MOD: u32;
    fn norm_impl(n: u32) -> u32;
    fn add_impl(lhs: u32, rhs: u32) -> u32;
    fn sub_impl(lhs: u32, rhs: u32) -> u32;
    fn mul_impl(lhs: u32, rhs: u32) -> u32;
}
macro_rules! modint32static_impl {
    ($modulus:literal, $st:ident) => {
        modint32static_impl!(@dol ($) $modulus, $st);
    };
    (@dol ($dol:tt) $modulus:literal, $st:ident) => {
        #[derive(Copy, Clone)]
        pub struct $st(u32);
        impl ModInt32Static for $st {
            const MOD: u32 = ($modulus as u32);
            #[inline]
            fn norm_impl(x: u32) -> u32 {
                x % Self::MOD
            }
            #[inline]
            fn add_impl(lhs: u32, rhs: u32) -> u32 {
                let v = u64::from(lhs) + u64::from(rhs);
                match v.checked_sub(u64::from(Self::MOD)) {
                    None => v as u32,
                    Some(w) => w as u32,
                }
            }
            #[inline]
            fn sub_impl(lhs: u32, rhs: u32) -> u32 {
                let (v, f) = lhs.overflowing_sub(rhs);
                v.wrapping_add(u32::from(f).wrapping_neg() & Self::MOD)
            }
            #[inline]
            fn mul_impl(lhs: u32, rhs: u32) -> u32 {
                (u64::from(lhs) * u64::from(rhs) % u64::from(Self::MOD)) as u32
            }
        }
        impl std::ops::Add for $st {
            type Output = Self;
            #[inline]
            fn add(self, rhs: Self) -> Self::Output {
                $st($st::add_impl(self.0, rhs.0))
            }
        }
        impl std::ops::Sub for $st {
            type Output = Self;
            #[inline]
            fn sub(self, rhs: Self) -> Self::Output {
                $st($st::sub_impl(self.0, rhs.0))
            }
        }
        impl std::ops::Mul for $st {
            type Output = Self;
            #[inline]
            fn mul(self, rhs: Self) -> Self::Output {
                $st($st::mul_impl(self.0, rhs.0))
            }
        }
        impl std::ops::Neg for $st {
            type Output = Self;
            #[inline]
            fn neg(self) -> Self::Output {
                $st($st::sub_impl(0, self.0))
            }
        }
        impl std::ops::AddAssign for $st {
            #[inline]
            fn add_assign(&mut self, rhs: Self) {
                *self = Self(Self::add_impl(self.0, rhs.0));
            }
        }
        impl std::ops::SubAssign for $st {
            #[inline]
            fn sub_assign(&mut self, rhs: Self) {
                *self = Self(Self::sub_impl(self.0, rhs.0));
            }
        }
        impl std::ops::MulAssign for $st {
            #[inline]
            fn mul_assign(&mut self, rhs: Self) {
                *self = Self(Self::mul_impl(self.0, rhs.0));
            }
        }
        impl $st {
            #[inline]
            pub fn new(n: u32) -> Self {
                Self(Self::norm_impl(n))
            }
            #[inline]
            pub fn val(&self) -> u32 {
                self.0
            }
        }
    };
}
modint32static_impl!(998244353, MInt);
0