Submission #9437914


Source Code Expand

use mod_int::ModInt;
const MOD: usize = 998244353;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());

    let s: Vec<usize> = sc
        .read::<String>()
        .chars()
        .map(|c| c as usize - '0' as usize)
        .collect();
    let n = s.len();
    let balls = n * 2;
    let mut dp = vec![ModInt(0); balls + 1];
    dp[0] = ModInt(1);
    let mut reachable_red = 0;
    for i in 0..balls {
        let new_red = if i < n { s[i] } else { 0 };
        let new_blue = if i < n { 2 - s[i] } else { 0 };
        let mut next = vec![ModInt(0); balls + 1];
        let total = if i < n { 2 * i } else { 2 * n };
        let reachable_blue = total - reachable_red;
        for used_red in 0..(reachable_red + 1) {
            if i >= used_red {
                let used_blue = i - used_red;
                if used_blue <= reachable_blue && used_blue + 1 <= reachable_blue + new_blue {
                    next[used_red] += dp[used_red];
                }
            }
            if used_red + 1 <= reachable_red + new_red {
                next[used_red + 1] += dp[used_red];
            }
        }
        dp = next;
        reachable_red += new_red;
    }

    let r = s[0..n].iter().sum::<usize>();
    println!("{}", dp[r].0);
}

pub mod mod_int {
    use super::MOD;
    use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};

    type Num = usize;

    #[derive(Clone, Copy, Debug)]
    pub struct ModInt<T: Copy + Clone>(pub T);

    impl Add<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn add(self, rhs: ModInt<Num>) -> ModInt<Num> {
            self + rhs.0
        }
    }

    impl Add<Num> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn add(self, rhs: Num) -> ModInt<Num> {
            let mut t = rhs + self.0;
            if t >= MOD {
                t = t - MOD;
            }
            ModInt(t)
        }
    }

    impl Sub<Num> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn sub(self, rhs: Num) -> ModInt<Num> {
            let rhs = if rhs >= MOD { rhs % MOD } else { rhs };
            let value = if self.0 < rhs { self.0 + MOD } else { self.0 };
            ModInt(value - rhs)
        }
    }

    impl Sub<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn sub(self, rhs: ModInt<Num>) -> ModInt<Num> {
            self - rhs.0
        }
    }

    impl AddAssign<Num> for ModInt<Num> {
        fn add_assign(&mut self, other: Num) {
            *self = *self + other;
        }
    }
    impl AddAssign<ModInt<Num>> for ModInt<Num> {
        fn add_assign(&mut self, other: ModInt<Num>) {
            *self = *self + other;
        }
    }

    impl SubAssign<Num> for ModInt<Num> {
        fn sub_assign(&mut self, other: Num) {
            *self = *self - other;
        }
    }

    impl SubAssign<ModInt<Num>> for ModInt<Num> {
        fn sub_assign(&mut self, other: ModInt<Num>) {
            *self = *self - other;
        }
    }

    impl Div<Num> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn div(self, rhs: Num) -> ModInt<Num> {
            self * ModInt(rhs).pow(MOD - 2)
        }
    }

    impl Div<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn div(self, rhs: ModInt<Num>) -> ModInt<Num> {
            self / rhs.0
        }
    }

    impl DivAssign<Num> for ModInt<Num> {
        fn div_assign(&mut self, rhs: Num) {
            *self = *self / rhs
        }
    }
    impl DivAssign<ModInt<Num>> for ModInt<Num> {
        fn div_assign(&mut self, rhs: ModInt<Num>) {
            *self = *self / rhs
        }
    }

    impl Mul<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;

        fn mul(self, rhs: ModInt<Num>) -> ModInt<Num> {
            self * rhs.0
        }
    }
    impl Mul<Num> for ModInt<Num> {
        type Output = ModInt<Num>;

        fn mul(self, rhs: Num) -> ModInt<Num> {
            let t = (self.0 * rhs) % MOD;
            ModInt(t)
        }
    }

    impl MulAssign<Num> for ModInt<Num> {
        fn mul_assign(&mut self, rhs: Num) {
            *self = *self * rhs;
        }
    }

    impl MulAssign<ModInt<Num>> for ModInt<Num> {
        fn mul_assign(&mut self, rhs: ModInt<Num>) {
            *self = *self * rhs;
        }
    }

    impl ModInt<Num> {
        pub fn pow(self, e: usize) -> ModInt<Num> {
            let mut result = ModInt(1);
            let mut cur = self;
            let mut e = e;
            while e > 0 {
                if e & 1 == 1 {
                    result *= cur;
                }
                e >>= 1;
                cur *= cur;
            }
            result
        }
    }
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: std::ops::Deref<Target = str>>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}

Submission Info

Submission Time
Task F - Pass
User kenkoooo
Language Rust (1.15.1)
Score 900
Code Size 6075 Byte
Status AC
Exec Time 31 ms
Memory 4352 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 900 / 900
Status
AC × 3
AC × 31
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 27 ms 4352 KiB
02.txt AC 27 ms 4352 KiB
03.txt AC 26 ms 4352 KiB
04.txt AC 26 ms 4352 KiB
05.txt AC 26 ms 4352 KiB
06.txt AC 26 ms 4352 KiB
07.txt AC 26 ms 4352 KiB
08.txt AC 27 ms 4352 KiB
09.txt AC 26 ms 4352 KiB
10.txt AC 27 ms 4352 KiB
11.txt AC 6 ms 4352 KiB
12.txt AC 26 ms 4352 KiB
13.txt AC 31 ms 4352 KiB
14.txt AC 17 ms 4352 KiB
15.txt AC 17 ms 4352 KiB
16.txt AC 17 ms 4352 KiB
17.txt AC 31 ms 4352 KiB
18.txt AC 31 ms 4352 KiB
19.txt AC 31 ms 4352 KiB
20.txt AC 28 ms 4352 KiB
21.txt AC 15 ms 4352 KiB
22.txt AC 20 ms 4352 KiB
23.txt AC 19 ms 4352 KiB
24.txt AC 20 ms 4352 KiB
25.txt AC 21 ms 4352 KiB
26.txt AC 2 ms 4352 KiB
27.txt AC 2 ms 4352 KiB
28.txt AC 2 ms 4352 KiB
s1.txt AC 2 ms 4352 KiB
s2.txt AC 2 ms 4352 KiB
s3.txt AC 2 ms 4352 KiB