Problem p03222 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using ll = long long;
using Int = int;
#define int ll
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define erep(e, v) for (auto &&e : v)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? (int)1e9 : (int)1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T a) {
  for (auto itr : a) {
    if (itr != *a.begin())
      cout << " ";
    cout << itr;
  }
}
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
int MOD = 1000000007;
class dynamic_bitset {
public:
  long long bit;
  string sbit = "";
  unsigned long long bitsize = 0;
  dynamic_bitset(int _bit) : bit{_bit} {
    for (int t = 1, num = 1; bitsize == 0; t++, num *= 2) {
      if (num - 1 >= bit)
        bitsize = t;
    }
    for (int i = 0; i < bitsize; i++) {
      sbit = ((bit >> i & 1) == 1 ? "1" : "0") + sbit;
    }
  }
  dynamic_bitset(int _bit, int bitrange) : bit{_bit} {
    for (int t = 1, num = 1; bitsize == 0; t++, num *= 2) {
      if (num - 1 >= bit)
        bitsize = t;
    }
    for (int i = 0; i < bitrange; i++) {
      sbit = (i < bitsize and ((bit >> i & 1) == 1) ? "1" : "0") + sbit;
    }
  }
  auto test(int _idx) {
    assert(_idx < sbit.size() && "excution will be out of range");
    return sbit[_idx] == '1';
  }
  void set(int _idx) {
    assert(_idx < sbit.size() && "excution will be out of range");
    sbit[_idx] = '1';
  }
  auto size() { return sbit.size(); }
};

int ans = 0;
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  auto dp = vectors(100, 100, 0LL);
  dp[1][1] = 1;
  for (int j = 1; j <= h; j++) {
    for (int i = 1; i <= w; i++) {
      for (int bit = 0; bit < (1 << (w - 1)); bit++) {
        auto db = dynamic_bitset(bit, w - 1);
        bool isValid = true;
        rep(i, (int)db.size() - 1) {
          if (db.test(i) and db.test(i + 1))
            isValid = false;
        }
        if (!isValid)
          continue;
        if (i + 1 <= w and db.test(i - 1)) {
          dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j]) % MOD;
        } else if (i - 1 >= 1 and db.test(i - 2)) {
          dp[i - 1][j + 1] = (dp[i - 1][j + 1] + dp[i][j]) % MOD;
        } else {
          dp[i][j + 1] = (dp[i][j + 1] + dp[i][j]) % MOD;
        }
      }
    }
  }
  cout << dp[k][h + 1] << endl;
}


Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
using Int = int;
#define int ll
#define rep(i, n) for (int i = 0; i < n; i++)
#define loop(i, s, n) for (int i = s; i < n; i++)
#define erep(e, v) for (auto &&e : v)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? (int)1e9 : (int)1e18)
#define EPS 0.0000000001
using namespace std;
template <class T, class S> void cmin(T &a, const S &b) {
  if (a > b)
    a = b;
}
template <class T, class S> void cmax(T &a, const S &b) {
  if (a < b)
    a = b;
}
template <typename Head, typename Value>
auto vectors(const Head &head, const Value &v) {
  return vector<Value>(head, v);
}
template <typename Head, typename... Tail> auto vectors(Head x, Tail... tail) {
  auto inner = vectors(tail...);
  return vector<decltype(inner)>(x, inner);
}
template <class T> void join(T a) {
  for (auto itr : a) {
    if (itr != *a.begin())
      cout << " ";
    cout << itr;
  }
}
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
int W, H;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
bool valid(int x, int y) { return (0 <= x && x < W) && (0 <= y && y < H); }
int MOD = 1000000007;
class dynamic_bitset {
public:
  long long bit;
  string sbit = "";
  unsigned long long bitsize = 0;
  dynamic_bitset(int _bit) : bit{_bit} {
    for (int t = 1, num = 1; bitsize == 0; t++, num *= 2) {
      if (num - 1 >= bit)
        bitsize = t;
    }
    for (int i = 0; i < bitsize; i++) {
      sbit = ((bit >> i & 1) == 1 ? "1" : "0") + sbit;
    }
  }
  dynamic_bitset(int _bit, int bitrange) : bit{_bit} {
    for (int t = 1, num = 1; bitsize == 0; t++, num *= 2) {
      if (num - 1 >= bit)
        bitsize = t;
    }
    for (int i = 0; i < bitrange; i++) {
      sbit = (i < bitsize and ((bit >> i & 1) == 1) ? "1" : "0") + sbit;
    }
  }
  auto test(int _idx) {
    assert(_idx < sbit.size() && "excution will be out of range");
    return sbit[_idx] == '1';
  }
  void set(int _idx) {
    assert(_idx < sbit.size() && "excution will be out of range");
    sbit[_idx] = '1';
  }
  auto size() { return sbit.size(); }
};

int ans = 0;
signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  auto dp = vectors(300, 300, 0LL);
  dp[1][1] = 1;
  for (int j = 1; j <= h; j++) {
    for (int i = 1; i <= w; i++) {
      for (int bit = 0; bit < (1 << (w - 1)); bit++) {
        auto db = dynamic_bitset(bit, w - 1);
        bool isValid = true;
        rep(i, (int)db.size() - 1) {
          if (db.test(i) and db.test(i + 1))
            isValid = false;
        }
        if (!isValid)
          continue;
        if (i + 1 <= w and db.test(i - 1)) {
          dp[i + 1][j + 1] = (dp[i + 1][j + 1] + dp[i][j]) % MOD;
        } else if (i - 1 >= 1 and db.test(i - 2)) {
          dp[i - 1][j + 1] = (dp[i - 1][j + 1] + dp[i][j]) % MOD;
        } else {
          dp[i][j + 1] = (dp[i][j + 1] + dp[i][j]) % MOD;
        }
      }
    }
  }
  cout << dp[k][h + 1] << endl;
}