Problem p02635 (C++) — buggy submission, verdict: Time Limit Exceeded

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <complex>
#include <numeric>
#include <utility>

#include <iomanip>
#include <iostream>
#include <sstream>

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

// namespace
using namespace std;

// type alias
using ll = long long;
using ull = unsigned long long;
using comp = complex<double>;

// constant
static const ll MOD = 998244353LL;
static const double PI = 3.14159265358979323846;

// conversion
inline ll toint(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class t> inline string tostring(t x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// print
#define RET(x) return cout << x << endl, 0;

// for loop
#define REP(i, a, b) for ((i) = (ll)(a); (i) < (ll)(b); (i)++)
#define REPD(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); (i)++)
#define REPI(v, vs) for (auto &v : vs)

// debug
#ifdef LOCAL_ENV
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << #x << " = " << (x) << " (l" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#else
#define DUMP(x)
#define DEBUG(x)
#endif

#define MAX_VALUE 9223372036854775807

template <ull N, class T, class... Args, std::enable_if_t<N == 0, int> = 0>
auto make_multiple_vector(Args... args) {
  return T(args...);
}

template <ull N, class T, class... Args, std::enable_if_t<N != 0, int> = 0>
auto make_multiple_vector(ull size, Args... args) {
  using value_type =
      std::decay_t<decltype(make_multiple_vector<N - 1, T>(args...))>;
  return vector<value_type>(size, make_multiple_vector<N - 1, T>(args...));
}

ll getModValue(const ll &n, ll mod) { return (n % mod + mod) % mod; }

// computational complexity: o(log(max(a, b)))
inline pair<ll, ll> getBezoutsIdentitySolution(ll a, ll b) {
  if (b == 0) {
    return {1, 0};
  }
  auto sol = getBezoutsIdentitySolution(b, a % b);
  return {sol.second, sol.first - (a / b) * sol.second};
}

// computational complexity: o(log(max(n, mod)))
inline ll getModInverse(const ll &n, ll mod) {
  auto sol = getBezoutsIdentitySolution(n, mod);
  if (n * sol.first + mod * sol.second != 1) {
    return -1;
  }
  return getModValue(sol.first, mod);
}

class ResidueInteger {
private:
  ll mod_;
  ll n_;

public:
  ResidueInteger() : mod_(MOD), n_(getModValue(0, mod_)) {}
  ResidueInteger(ll n) : mod_(MOD), n_(getModValue(n, mod_)) {}
  ResidueInteger(ll n, ll mod) : mod_(mod), n_(getModValue(n, mod_)) {}

  friend ResidueInteger operator+(const ResidueInteger &lhs,
                                  const ResidueInteger &rhs);
  friend ResidueInteger operator+(const ResidueInteger &lhs, const ll &rhs);
  friend ResidueInteger operator+(const ll &lhs, const ResidueInteger &rhs);
  friend ResidueInteger &operator+=(ResidueInteger &lhs,
                                    const ResidueInteger &rhs);
  friend ResidueInteger &operator+=(ResidueInteger &lhs, const ll &rhs);

  friend ResidueInteger operator-(const ResidueInteger &lhs,
                                  const ResidueInteger &rhs);
  friend ResidueInteger operator-(const ResidueInteger &lhs, const ll &rhs);
  friend ResidueInteger operator-(const ll &lhs, const ResidueInteger &rhs);
  friend ResidueInteger &operator-=(ResidueInteger &lhs,
                                    const ResidueInteger &rhs);
  friend ResidueInteger &operator-=(ResidueInteger &lhs, const ll &rhs);

  friend ResidueInteger operator*(const ResidueInteger &lhs,
                                  const ResidueInteger &rhs);
  friend ResidueInteger operator*(const ResidueInteger &lhs, const ll &rhs);
  friend ResidueInteger operator*(const ll &lhs, const ResidueInteger &rhs);
  friend ResidueInteger &operator*=(ResidueInteger &lhs,
                                    const ResidueInteger &rhs);
  friend ResidueInteger &operator*=(ResidueInteger &lhs, const ll &rhs);

  friend ResidueInteger operator/(const ResidueInteger &lhs,
                                  const ResidueInteger &rhs);
  friend ResidueInteger operator/(const ResidueInteger &lhs, const ll &rhs);
  friend ResidueInteger operator/(const ll &lhs, const ResidueInteger &rhs);
  friend ResidueInteger &operator/=(ResidueInteger &lhs,
                                    const ResidueInteger &rhs);
  friend ResidueInteger &operator/=(ResidueInteger &lhs, const ll &rhs);

  friend bool operator==(const ResidueInteger &lhs, const ResidueInteger &rhs);
  friend bool operator==(const ResidueInteger &lhs, const ll &rhs);
  friend bool operator==(const ll &lhs, const ResidueInteger &rhs);

  ll n() const { return n_; }

  friend std::ostream &operator<<(std::ostream &lhs, const ResidueInteger &rhs);
  friend std::istream &operator>>(std::istream &lhs, ResidueInteger &rhs);
};

inline ResidueInteger operator+(const ResidueInteger &lhs,
                                const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  return {lhs.n_ + rhs.n_, lhs.mod_};
}

inline ResidueInteger operator+(const ResidueInteger &lhs, const ll &rhs) {
  return {lhs.n_ + getModValue(rhs, lhs.mod_), lhs.mod_};
}

inline ResidueInteger operator+(const ll &lhs, const ResidueInteger &rhs) {
  return {getModValue(lhs, rhs.mod_) + rhs.n_, rhs.mod_};
}

inline ResidueInteger &operator+=(ResidueInteger &lhs,
                                  const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  lhs.n_ = getModValue(lhs.n_ + rhs.n_, lhs.mod_);
  return lhs;
}

inline ResidueInteger &operator+=(ResidueInteger &lhs, const ll &rhs) {
  lhs.n_ = getModValue(lhs.n_ + getModValue(rhs, lhs.mod_), lhs.mod_);
  return lhs;
}

inline ResidueInteger operator-(const ResidueInteger &lhs,
                                const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  return {lhs.n_ - rhs.n_, lhs.mod_};
}

inline ResidueInteger operator-(const ResidueInteger &lhs, const ll &rhs) {
  return {lhs.n_ - getModValue(rhs, lhs.mod_), lhs.mod_};
}

inline ResidueInteger operator-(const ll &lhs, const ResidueInteger &rhs) {
  return {getModValue(lhs, rhs.mod_) - rhs.n_, rhs.mod_};
}

inline ResidueInteger &operator-=(ResidueInteger &lhs,
                                  const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  lhs.n_ = getModValue(lhs.n_ - rhs.n_, lhs.mod_);
  return lhs;
}

inline ResidueInteger &operator-=(ResidueInteger &lhs, const ll &rhs) {
  lhs.n_ = getModValue(lhs.n_ - getModValue(rhs, lhs.mod_), lhs.mod_);
  return lhs;
}

inline ResidueInteger operator*(const ResidueInteger &lhs,
                                const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  return {lhs.n_ * rhs.n_, lhs.mod_};
}

inline ResidueInteger operator*(const ResidueInteger &lhs, const ll &rhs) {
  return {lhs.n_ * getModValue(rhs, lhs.mod_), lhs.mod_};
}

inline ResidueInteger operator*(const ll &lhs, const ResidueInteger &rhs) {
  return {getModValue(lhs, rhs.mod_) * rhs.n_, rhs.mod_};
}

inline ResidueInteger &operator*=(ResidueInteger &lhs,
                                  const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  lhs.n_ = getModValue(lhs.n_ * rhs.n_, lhs.mod_);
  return lhs;
}

inline ResidueInteger &operator*=(ResidueInteger &lhs, const ll &rhs) {
  lhs.n_ = getModValue(lhs.n_ * getModValue(rhs, lhs.mod_), lhs.mod_);
  return lhs;
}

inline ResidueInteger operator/(const ResidueInteger &lhs,
                                const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  return {lhs.n_ * getModInverse(rhs.n_, lhs.mod_), lhs.mod_};
}

inline ResidueInteger operator/(const ResidueInteger &lhs, const ll &rhs) {
  return {lhs.n_ * getModInverse(getModValue(rhs, lhs.mod_), lhs.mod_),
          lhs.mod_};
}

inline ResidueInteger operator/(const ll &lhs, const ResidueInteger &rhs) {
  return {getModValue(lhs, rhs.mod_) * getModInverse(rhs.n_, rhs.mod_),
          rhs.mod_};
}

inline ResidueInteger &operator/=(ResidueInteger &lhs,
                                  const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  lhs.n_ = getModValue(lhs.n_ * getModInverse(rhs.n_, lhs.mod_), lhs.mod_);
  return lhs;
}

inline ResidueInteger &operator/=(ResidueInteger &lhs, const ll &rhs) {
  lhs.n_ = getModValue(
      lhs.n_ * getModInverse(getModValue(rhs, lhs.mod_), lhs.mod_), lhs.mod_);
  return lhs;
}

inline bool operator==(const ResidueInteger &lhs, const ResidueInteger &rhs) {
  return (lhs.n_ == rhs.n_) && (lhs.mod_ == rhs.mod_);
}

inline bool operator==(const ResidueInteger &lhs, const ll &rhs) {
  return lhs.n_ == getModValue(rhs, lhs.mod_);
}

bool operator==(const ll &lhs, const ResidueInteger &rhs) {
  return getModValue(lhs, rhs.mod_) == rhs.n_;
}

std::ostream &operator<<(std::ostream &lhs, const ResidueInteger &rhs) {
  return lhs << rhs.n_;
}

std::istream &operator>>(std::istream &lhs, ResidueInteger &rhs) {
  lhs >> rhs.n_;
  return lhs;
}

using rll = ResidueInteger;

vector<rll> get_exclamations(ull n) {
  vector<rll> exclamations(n + 1ULL);
  exclamations[0] = 1LL;
  for (ll i = 1; i <= n; ++i)
    exclamations[i] = exclamations[i - 1] * i;

  return exclamations;
}

int solve() {
  string S;
  ll K;
  cin >> S >> K;

  vector<ll> one_counts;
  ll one_count = 0;
  REPD(i, 0, S.size()) {
    if (S[i] == '0') {
      one_counts.emplace_back(one_count);
      one_count = 0;
    } else {
      ++one_count;
    }
  }
  one_counts.emplace_back(one_count);
  K = min(K, accumulate(one_counts.begin(), one_counts.end(), 0LL));

  // aaa
  // aaa
  auto dps = make_multiple_vector<3, rll>(one_counts.size(), K + 1, K + 1);
  dps[one_counts.size() - 1][0][0] = 1;
  for (ll i = one_counts.size() - 1; i >= 1; --i) {
    for (ll j = 0; j <= K; j++) {
      for (ll k = 0; k <= j; k++) {
        for (ll l = 1; l <= one_counts[i]; ++l) {
          if (k + l > K || j + l > K)
            continue;
          dps[i - 1][j + l][k + l] += dps[i][j][k];
        }
        for (ll l = 0; l <= k; ++l) {
          dps[i - 1][j][l] += dps[i][j][k];
        }
      }
    }
  }

  rll ans = 0;
  for (ll i = 0; i <= K; i++) {
    for (ll j = 0; j <= K; j++) {
      ans += dps[0][i][j];
    }
  }
  RET(ans);

  return 0;
}

// main function
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();
  // ll t;
  // cin >> t;
  // REPD(i, 0, t) solve();

  return 0;
}


Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <deque>
#include <functional>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <complex>
#include <numeric>
#include <utility>

#include <iomanip>
#include <iostream>
#include <sstream>

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

// namespace
using namespace std;

// type alias
using ll = long long;
using ull = unsigned long long;
using comp = complex<double>;

// constant
static const ll MOD = 998244353LL;
static const double PI = 3.14159265358979323846;

// conversion
inline ll toint(string s) {
  ll v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class t> inline string tostring(t x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// print
#define RET(x) return cout << x << endl, 0;

// for loop
#define REP(i, a, b) for ((i) = (ll)(a); (i) < (ll)(b); (i)++)
#define REPD(i, a, b) for (ll i = (ll)(a); (i) < (ll)(b); (i)++)
#define REPI(v, vs) for (auto &v : vs)

// debug
#ifdef LOCAL_ENV
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << #x << " = " << (x) << " (l" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#else
#define DUMP(x)
#define DEBUG(x)
#endif

#define MAX_VALUE 9223372036854775807

template <ull N, class T, class... Args, std::enable_if_t<N == 0, int> = 0>
auto make_multiple_vector(Args... args) {
  return T(args...);
}

template <ull N, class T, class... Args, std::enable_if_t<N != 0, int> = 0>
auto make_multiple_vector(ull size, Args... args) {
  using value_type =
      std::decay_t<decltype(make_multiple_vector<N - 1, T>(args...))>;
  return vector<value_type>(size, make_multiple_vector<N - 1, T>(args...));
}

ll getModValue(const ll &n, ll mod) { return (n % mod + mod) % mod; }

// computational complexity: o(log(max(a, b)))
inline pair<ll, ll> getBezoutsIdentitySolution(ll a, ll b) {
  if (b == 0) {
    return {1, 0};
  }
  auto sol = getBezoutsIdentitySolution(b, a % b);
  return {sol.second, sol.first - (a / b) * sol.second};
}

// computational complexity: o(log(max(n, mod)))
inline ll getModInverse(const ll &n, ll mod) {
  auto sol = getBezoutsIdentitySolution(n, mod);
  if (n * sol.first + mod * sol.second != 1) {
    return -1;
  }
  return getModValue(sol.first, mod);
}

class ResidueInteger {
private:
  ll mod_;
  ll n_;

public:
  ResidueInteger() : mod_(MOD), n_(getModValue(0, mod_)) {}
  ResidueInteger(ll n) : mod_(MOD), n_(getModValue(n, mod_)) {}
  ResidueInteger(ll n, ll mod) : mod_(mod), n_(getModValue(n, mod_)) {}

  friend ResidueInteger operator+(const ResidueInteger &lhs,
                                  const ResidueInteger &rhs);
  friend ResidueInteger operator+(const ResidueInteger &lhs, const ll &rhs);
  friend ResidueInteger operator+(const ll &lhs, const ResidueInteger &rhs);
  friend ResidueInteger &operator+=(ResidueInteger &lhs,
                                    const ResidueInteger &rhs);
  friend ResidueInteger &operator+=(ResidueInteger &lhs, const ll &rhs);

  friend ResidueInteger operator-(const ResidueInteger &lhs,
                                  const ResidueInteger &rhs);
  friend ResidueInteger operator-(const ResidueInteger &lhs, const ll &rhs);
  friend ResidueInteger operator-(const ll &lhs, const ResidueInteger &rhs);
  friend ResidueInteger &operator-=(ResidueInteger &lhs,
                                    const ResidueInteger &rhs);
  friend ResidueInteger &operator-=(ResidueInteger &lhs, const ll &rhs);

  friend ResidueInteger operator*(const ResidueInteger &lhs,
                                  const ResidueInteger &rhs);
  friend ResidueInteger operator*(const ResidueInteger &lhs, const ll &rhs);
  friend ResidueInteger operator*(const ll &lhs, const ResidueInteger &rhs);
  friend ResidueInteger &operator*=(ResidueInteger &lhs,
                                    const ResidueInteger &rhs);
  friend ResidueInteger &operator*=(ResidueInteger &lhs, const ll &rhs);

  friend ResidueInteger operator/(const ResidueInteger &lhs,
                                  const ResidueInteger &rhs);
  friend ResidueInteger operator/(const ResidueInteger &lhs, const ll &rhs);
  friend ResidueInteger operator/(const ll &lhs, const ResidueInteger &rhs);
  friend ResidueInteger &operator/=(ResidueInteger &lhs,
                                    const ResidueInteger &rhs);
  friend ResidueInteger &operator/=(ResidueInteger &lhs, const ll &rhs);

  friend bool operator==(const ResidueInteger &lhs, const ResidueInteger &rhs);
  friend bool operator==(const ResidueInteger &lhs, const ll &rhs);
  friend bool operator==(const ll &lhs, const ResidueInteger &rhs);

  ll n() const { return n_; }

  friend std::ostream &operator<<(std::ostream &lhs, const ResidueInteger &rhs);
  friend std::istream &operator>>(std::istream &lhs, ResidueInteger &rhs);
};

inline ResidueInteger operator+(const ResidueInteger &lhs,
                                const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  return {lhs.n_ + rhs.n_, lhs.mod_};
}

inline ResidueInteger operator+(const ResidueInteger &lhs, const ll &rhs) {
  return {lhs.n_ + getModValue(rhs, lhs.mod_), lhs.mod_};
}

inline ResidueInteger operator+(const ll &lhs, const ResidueInteger &rhs) {
  return {getModValue(lhs, rhs.mod_) + rhs.n_, rhs.mod_};
}

inline ResidueInteger &operator+=(ResidueInteger &lhs,
                                  const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  lhs.n_ = getModValue(lhs.n_ + rhs.n_, lhs.mod_);
  return lhs;
}

inline ResidueInteger &operator+=(ResidueInteger &lhs, const ll &rhs) {
  lhs.n_ = getModValue(lhs.n_ + getModValue(rhs, lhs.mod_), lhs.mod_);
  return lhs;
}

inline ResidueInteger operator-(const ResidueInteger &lhs,
                                const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  return {lhs.n_ - rhs.n_, lhs.mod_};
}

inline ResidueInteger operator-(const ResidueInteger &lhs, const ll &rhs) {
  return {lhs.n_ - getModValue(rhs, lhs.mod_), lhs.mod_};
}

inline ResidueInteger operator-(const ll &lhs, const ResidueInteger &rhs) {
  return {getModValue(lhs, rhs.mod_) - rhs.n_, rhs.mod_};
}

inline ResidueInteger &operator-=(ResidueInteger &lhs,
                                  const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  lhs.n_ = getModValue(lhs.n_ - rhs.n_, lhs.mod_);
  return lhs;
}

inline ResidueInteger &operator-=(ResidueInteger &lhs, const ll &rhs) {
  lhs.n_ = getModValue(lhs.n_ - getModValue(rhs, lhs.mod_), lhs.mod_);
  return lhs;
}

inline ResidueInteger operator*(const ResidueInteger &lhs,
                                const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  return {lhs.n_ * rhs.n_, lhs.mod_};
}

inline ResidueInteger operator*(const ResidueInteger &lhs, const ll &rhs) {
  return {lhs.n_ * getModValue(rhs, lhs.mod_), lhs.mod_};
}

inline ResidueInteger operator*(const ll &lhs, const ResidueInteger &rhs) {
  return {getModValue(lhs, rhs.mod_) * rhs.n_, rhs.mod_};
}

inline ResidueInteger &operator*=(ResidueInteger &lhs,
                                  const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  lhs.n_ = getModValue(lhs.n_ * rhs.n_, lhs.mod_);
  return lhs;
}

inline ResidueInteger &operator*=(ResidueInteger &lhs, const ll &rhs) {
  lhs.n_ = getModValue(lhs.n_ * getModValue(rhs, lhs.mod_), lhs.mod_);
  return lhs;
}

inline ResidueInteger operator/(const ResidueInteger &lhs,
                                const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  return {lhs.n_ * getModInverse(rhs.n_, lhs.mod_), lhs.mod_};
}

inline ResidueInteger operator/(const ResidueInteger &lhs, const ll &rhs) {
  return {lhs.n_ * getModInverse(getModValue(rhs, lhs.mod_), lhs.mod_),
          lhs.mod_};
}

inline ResidueInteger operator/(const ll &lhs, const ResidueInteger &rhs) {
  return {getModValue(lhs, rhs.mod_) * getModInverse(rhs.n_, rhs.mod_),
          rhs.mod_};
}

inline ResidueInteger &operator/=(ResidueInteger &lhs,
                                  const ResidueInteger &rhs) {
  if (lhs.mod_ != rhs.mod_) {
    throw invalid_argument("The mods of both operands should be equal.");
  }
  lhs.n_ = getModValue(lhs.n_ * getModInverse(rhs.n_, lhs.mod_), lhs.mod_);
  return lhs;
}

inline ResidueInteger &operator/=(ResidueInteger &lhs, const ll &rhs) {
  lhs.n_ = getModValue(
      lhs.n_ * getModInverse(getModValue(rhs, lhs.mod_), lhs.mod_), lhs.mod_);
  return lhs;
}

inline bool operator==(const ResidueInteger &lhs, const ResidueInteger &rhs) {
  return (lhs.n_ == rhs.n_) && (lhs.mod_ == rhs.mod_);
}

inline bool operator==(const ResidueInteger &lhs, const ll &rhs) {
  return lhs.n_ == getModValue(rhs, lhs.mod_);
}

bool operator==(const ll &lhs, const ResidueInteger &rhs) {
  return getModValue(lhs, rhs.mod_) == rhs.n_;
}

std::ostream &operator<<(std::ostream &lhs, const ResidueInteger &rhs) {
  return lhs << rhs.n_;
}

std::istream &operator>>(std::istream &lhs, ResidueInteger &rhs) {
  lhs >> rhs.n_;
  return lhs;
}

using rll = ResidueInteger;

vector<rll> get_exclamations(ull n) {
  vector<rll> exclamations(n + 1ULL);
  exclamations[0] = 1LL;
  for (ll i = 1; i <= n; ++i)
    exclamations[i] = exclamations[i - 1] * i;

  return exclamations;
}

int solve() {
  std::string s;
  ll K;
  std::cin >> s >> K;

  K = min(K, ll(s.size()));
  vector<ll> a;
  REPD(i, 0, s.length()) if (s[i] == '0') a.emplace_back(i);
  a.emplace_back(s.length());

  ll n = a.size() - 1;
  auto dp = make_multiple_vector<2, rll>(K + 1, K + 1);
  dp[0][0] = 1;
  for (ll i = n - 1; i >= 0; i--) {
    auto swp = make_multiple_vector<2, rll>(K + 1, K + 1);
    for (ll j = 0; j <= K; j++)
      for (ll k = 0; k <= K; k++) {
        for (ll nj = 0; nj <= j + a[i + 1] - a[i] - 1; nj++) {
          ll nk = k + std::max(ll{0}, nj - j);
          if (nj <= K && nk <= K)
            swp.at(nj).at(nk) += dp.at(j).at(k);
        }
      }
    swp.swap(dp);
  }

  rll ans = 0;
  for (auto &&v : dp)
    for (rll &x : v)
      ans += x;
  RET(ans);

  return 0;
}

// main function
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();
  // ll t;
  // cin >> t;
  // REPD(i, 0, t) solve();

  return 0;
}