Problem:
Score : 800 points

Problem Statement

Given is a string S consisting of 0 and 1. Find the number of strings, modulo 998244353, that can result from applying the following operation on S between 0 and K times (inclusive):

Choose a pair of integers i, j (1\leq i < j\leq |S|) such that the i-th and j-th characters of S are 0 and 1, respectively. Remove the j-th character from S and insert it to the immediate left of the i-th character.

Constraints

1 \leq |S| \leq 300

0 \leq K \leq 10^9

S consists of 0 and 1.

Input

Input is given from Standard Input in the following format:

S K

Output

Find the number of strings, modulo 998244353, that can result from applying the operation on S between 0 and K times (inclusive).

Sample Input 1

0101 1

Sample Output 1

4

Four strings, 0101, 0110, 1001, and 1010, can result.

Sample Input 2

01100110 2

Sample Output 2

14

Sample Input 3

1101010010101101110111100011011111011000111101110101010010101010101 20

Sample Output 3

113434815

Solution in Rust (1.42.0) (judge verdict: Accepted)

use crate::mod_int::ModInt;
use std::cmp::min;

const MOD: u64 = 998244353;
const MAX_K: usize = 310;

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

    let mut s = vec![];
    let t = sc.chars();
    let k: usize = min(sc.read(), MAX_K);
    let mut count = 0;
    for c in t.into_iter() {
        if c == '0' {
            s.push(count);
            count = 0;
        } else {
            count += 1;
        }
    }
    s.push(count);

    let total_ones = s.iter().sum::<usize>();
    let n = s.len() - 1;
    let mut dp = vec![vec![m(0); k + 1]; total_ones + 1];
    dp[0][0] = m(1);

    let mut cur_sum = 0;
    for i in 0..n {
        let s = s[i];
        let mut next = vec![vec![m(0); k + 1]; total_ones + 1];
        for before_ones in cur_sum..=total_ones {
            for before_k in 0..=k {
                for next_ones in before_ones..=total_ones {
                    let seg_ones = next_ones - before_ones;
                    if seg_ones < s && before_ones + seg_ones < cur_sum + s {
                        continue;
                    }
                    let cost = if seg_ones < s { 0 } else { seg_ones - s };
                    let next_k = before_k + cost;
                    if next_k > k {
                        continue;
                    }
                    next[next_ones][next_k] += dp[before_ones][before_k];
                }
            }
        }
        cur_sum += s;
        dp = next;
    }

    let mut ans = m(0);
    for k in 0..=k {
        for one in cur_sum..=total_ones {
            ans += dp[one][k];
        }
    }

    println!("{}", ans.v);
}

fn m(v: u64) -> ModInt<u64> {
    ModInt::new(v, MOD)
}
pub mod mod_int {
    use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, Sub, SubAssign};

    pub struct ModInt<T> {
        pub v: T,
        pub modulo: T,
    }

    impl<T> Copy for ModInt<T> where T: Copy {}
    impl<T> Clone for ModInt<T>
    where
        T: Copy,
    {
        fn clone(&self) -> Self {
            ModInt {
                v: self.v,
                modulo: self.modulo,
            }
        }
    }

    impl<T> Add<T> for ModInt<T>
    where
        T: Add<Output = T> + Sub<Output = T> + Rem<Output = T> + Copy + PartialOrd,
    {
        type Output = ModInt<T>;
        fn add(self, mut rhs: T) -> ModInt<T> {
            if rhs >= self.modulo {
                rhs = rhs % self.modulo;
            }
            let mut t = rhs + self.v;
            if t >= self.modulo {
                t = t - self.modulo;
            }
            ModInt {
                v: t,
                modulo: self.modulo,
            }
        }
    }

    impl<T> Add<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Add<T, Output = ModInt<T>>,
    {
        type Output = ModInt<T>;
        fn add(self, rhs: ModInt<T>) -> ModInt<T> {
            self + rhs.v
        }
    }

    impl<T> Sub<T> for ModInt<T>
    where
        T: Add<Output = T> + Sub<Output = T> + Rem<Output = T> + Copy + PartialOrd,
    {
        type Output = ModInt<T>;
        fn sub(self, rhs: T) -> ModInt<T> {
            let rhs = if rhs >= self.modulo {
                rhs % self.modulo
            } else {
                rhs
            };
            let value = if self.v < rhs {
                self.v + self.modulo
            } else {
                self.v
            };
            ModInt {
                v: value - rhs,
                modulo: self.modulo,
            }
        }
    }

    impl<T> Sub<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Sub<T, Output = ModInt<T>>,
    {
        type Output = ModInt<T>;
        fn sub(self, rhs: ModInt<T>) -> ModInt<T> {
            self - rhs.v
        }
    }

    impl<T> AddAssign<T> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Add<T, Output = ModInt<T>>,
    {
        fn add_assign(&mut self, other: T) {
            *self = *self + other;
        }
    }
    impl<T> AddAssign<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Add<ModInt<T>, Output = ModInt<T>>,
    {
        fn add_assign(&mut self, other: ModInt<T>) {
            *self = *self + other;
        }
    }

    impl<T> SubAssign<T> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Sub<T, Output = ModInt<T>>,
    {
        fn sub_assign(&mut self, other: T) {
            *self = *self - other;
        }
    }

    impl<T> SubAssign<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Sub<ModInt<T>, Output = ModInt<T>>,
    {
        fn sub_assign(&mut self, other: ModInt<T>) {
            *self = *self - other;
        }
    }

    impl<T> Div<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Div<T, Output = ModInt<T>>,
    {
        type Output = ModInt<T>;
        fn div(self, rhs: ModInt<T>) -> ModInt<T> {
            self / rhs.v
        }
    }

    impl<T> DivAssign<T> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Div<T, Output = ModInt<T>>,
    {
        fn div_assign(&mut self, rhs: T) {
            *self = *self / rhs
        }
    }
    impl<T> DivAssign<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Div<ModInt<T>, Output = ModInt<T>>,
    {
        fn div_assign(&mut self, rhs: ModInt<T>) {
            *self = *self / rhs
        }
    }

    impl<T> Mul<T> for ModInt<T>
    where
        T: Mul<Output = T> + Rem<Output = T> + Copy + PartialOrd,
    {
        type Output = ModInt<T>;

        fn mul(self, mut rhs: T) -> ModInt<T> {
            if rhs >= self.modulo {
                rhs = rhs % self.modulo;
            }
            let t = (self.v * rhs) % self.modulo;
            ModInt {
                v: t,
                modulo: self.modulo,
            }
        }
    }
    impl<T> Mul<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Mul<T, Output = ModInt<T>>,
    {
        type Output = ModInt<T>;
        fn mul(self, rhs: ModInt<T>) -> ModInt<T> {
            self * rhs.v
        }
    }

    impl<T> MulAssign<T> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Mul<T, Output = ModInt<T>>,
    {
        fn mul_assign(&mut self, rhs: T) {
            *self = *self * rhs;
        }
    }

    impl<T> MulAssign<ModInt<T>> for ModInt<T>
    where
        T: Copy,
        ModInt<T>: Mul<ModInt<T>, Output = ModInt<T>>,
    {
        fn mul_assign(&mut self, rhs: ModInt<T>) {
            *self = *self * rhs;
        }
    }

    impl<T> ModInt<T>
    where
        T: Copy,
        ModInt<T>: Mul<ModInt<T>, Output = ModInt<T>>,
    {
        pub fn general_pow(self, e: u64, one: ModInt<T>) -> ModInt<T> {
            let mut result = one;
            let mut cur = self;
            let mut e = e;
            while e > 0 {
                if e & 1 == 1 {
                    result *= cur;
                }
                e >>= 1;
                cur *= cur;
            }
            result
        }
    }

    impl Div<u64> for ModInt<u64> {
        type Output = ModInt<u64>;
        fn div(self, mut rhs: u64) -> ModInt<u64> {
            if rhs >= self.modulo {
                rhs %= self.modulo;
            }
            self * ModInt {
                v: rhs,
                modulo: self.modulo,
            }
            .pow(self.modulo - 2)
        }
    }

    impl ModInt<u64> {
        pub fn new(v: u64, modulo: u64) -> Self {
            Self {
                v: v % modulo,
                modulo,
            }
        }

        pub fn pow(self, e: u64) -> ModInt<u64> {
            self.general_pow(e, ModInt::new(1, self.modulo))
        }
    }
}

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) -> Self {
        Self(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().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()
    }
}