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 C++ (GCC 9.2.1) (judge verdict: Accepted)

#include <iostream>
#include <cstdio>
#include <cassert>
#include <ctime>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define all(x) (x).begin(), (x).end()

#ifdef LOCAL
#define dbg(x) cerr << #x " = " << x << endl;
#define idbg(x) #x " = " << x
#include "pretty_print.h"
#else
#define dbg(x)
#define idbg(x) ""
#endif

#ifdef _WIN32
#  define LLD "%I64d"
#else
#  define LLD "%lld"
#endif

typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;

template <typename T> T sqr(T x) { return x * x; }
template <typename T> T abs(T x) { return x < 0? -x : x; }
template <typename T> T gcd(T a, T b) { return b? gcd(b, a % b) : a; }
template <typename T> bool chmin(T &x, const T& y) { if (x > y) { x = y; return true; } return false; }
template <typename T> bool chmax(T &x, const T& y) { if (x < y) { x = y; return true; } return false; }

int main(int /* argc */, char** /* argv */)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifdef LOCAL
    assert(freopen("inp", "r", stdin));
    assert(freopen("out", "w", stdout));
    #endif

    string s;
    int k;
    while (cin >> s >> k) {
        const int MOD = 998244353;
        int n = s.size();
        vector<vector<vector<int>>> f(n + 2, vector<vector<int>>(n + 2, vector<int>(n + 2, 0)));
        f[0][0][0] = 1;
        k = min(k, n);
        auto addmod = [&](int& x, const int d) {
            x += d;
            if (x >= MOD) {
                x -= MOD;
            }
            if (x < 0) {
                x += MOD;
            }
        };

        vector<int> a(n + 1, 0);
        for (int i = n; i >= 0; --i) {
            a[i] = s[i] == '1'? 1 + a[i + 1] : 0;
        }
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j <= k; ++j) {
                for (int d = 0; d <= k; ++d) {
                    if (!f[i][j][d]) {
                        continue;
                    }

                    addmod(f[i + 1][j][d], f[i][j][d]);
                    if (s[i] == '0') {
                        addmod(f[i][j + 1][d + 1], f[i][j][d]);
                    } else {
                        int x = i + a[i];
                        if (x < n && a[i] <= d) {
                            addmod(f[x + 1][j][d - a[i]], f[i][j][d]);
                        }
                    }
                }
            }
        }
        int ans = 0;
        for (int j = 0; j <= k; ++j) {
            for (int d = 0; d <= k; ++d) {
                if (a[n - d] == d) {
                    addmod(ans, f[n - d][j][d]);
                }
            }
        }
        cout << ans << "\n";
    }

    cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl;
    return 0;
}