Problem p03350 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

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

  cin >> N >> K;
  vector<string> s(N + 1);
  for (auto &i : s)
    cin >> i;
  vector<bitset<1024 * 1024>> bs(N + 1);
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < 1 << i; j++) {
      if (s[i][j] == '1')
        bs[i][j].flip();
    }
  }
  vector<vector<vector<int>>> num(N + 1);
  for (int i = 0; i <= N; i++) {
    num[i].resize(1 << i, vector<int>(1 << (N + 2 - i), 0));
  }
  for (int i = 0; i < 1 << N; i++) {
    if (bs[N].test(i)) {
      num[N][i][1] = 1;
      if (i & 1)
        num[N][i][3] = 1;
      else
        num[N][i][2] = 1;
    }
  }
  vector<int> box(1 << (N + 2));
  for (int i = 0; i < (N + 2); i++) {
    box[1 << i]++;
  }
  for (int i = 1; i < 1 << (N + 2); i++) {
    box[i] += box[i - 1];
  }

  for (int i = N - 1; i > 0; i--) {
    for (int j = 0; j < num[i].size(); j++) {
      if (bs[i].test(j)) {
        num[i][j][1]++;
        if (j & 1)
          num[i][j][3]++;
        else
          num[i][j][2]++;
      }

      for (int k = 1; k < num[i + 1][0].size(); k++) {
        num[i][j][k] += num[i + 1][j * 2][k];
        num[i][j][k] += num[i + 1][j * 2 + 1][k];

        if (j & 1) {
          int dif = box[k] - 1;
          int nxk = k;
          nxk += 1 << (dif + 1);

          num[i][j][nxk] += num[i + 1][j * 2][k];
          num[i][j][nxk] += num[i + 1][j * 2 + 1][k];

          if (num[i + 1][j * 2].size() > nxk && nxk > 1) {
            num[i][j][nxk] -= num[i + 1][j * 2][nxk];
            num[i][j][nxk] -= num[i + 1][j * 2 + 1][nxk];
          }

        }

        else {
          int dif = box[k] - 1;
          int nxk = k;
          nxk -= 1 << dif;
          nxk += 1 << (dif + 1);
          // cout << k << " " << nxk << endl;
          // if(num[i][j].size()<=nxk)assert(0);
          num[i][j][nxk] += num[i + 1][j * 2][k];
          num[i][j][nxk] += num[i + 1][j * 2 + 1][k];
          if (num[i + 1][j * 2].size() > nxk && nxk > 1) {
            num[i][j][nxk] -= num[i + 1][j * 2][nxk];
            num[i][j][nxk] -= num[i + 1][j * 2 + 1][nxk];
          }
        }
      }
    }
  }
  string ans;
  for (int j = 1; j < 1 << (N + 1); j++) {
    if (num[1][0][j] + num[1][1][j] < K)
      continue;
    int dig = box[j] - 1;
    if (dig < ans.size())
      continue;
    string st(dig, '0');
    for (int k = 0; k < dig; k++) {
      if ((j >> k) & 1)
        st[k] = '1';
    }
    reverse(st.begin(), st.end());
    if (st.size() > ans.size())
      ans = st;
  }
  cout << ans << endl;
}

Fixed submission:
#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

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

  cin >> N >> K;
  if (N == 0) {
    cout << "" << endl;
    return 0;
  }
  vector<string> s(N + 1);
  for (auto &i : s)
    cin >> i;
  vector<bitset<1024 * 1024>> bs(N + 1);
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j < 1 << i; j++) {
      if (s[i][j] == '1')
        bs[i][j].flip();
    }
  }
  vector<vector<vector<int>>> num(N + 1);
  for (int i = 0; i <= N; i++) {
    num[i].resize(1 << i, vector<int>(1 << (N + 2 - i), 0));
  }
  for (int i = 0; i < 1 << N; i++) {
    if (bs[N].test(i)) {
      num[N][i][1] = 1;
      if (i & 1)
        num[N][i][3] = 1;
      else
        num[N][i][2] = 1;
    }
  }
  vector<int> box(1 << (N + 2));
  for (int i = 0; i < (N + 2); i++) {
    box[1 << i]++;
  }
  for (int i = 1; i < 1 << (N + 2); i++) {
    box[i] += box[i - 1];
  }

  for (int i = N - 1; i > 0; i--) {
    for (int j = 0; j < num[i].size(); j++) {
      if (bs[i].test(j)) {
        num[i][j][1]++;
        if (j & 1)
          num[i][j][3]++;
        else
          num[i][j][2]++;
      }

      for (int k = 1; k < num[i + 1][0].size(); k++) {
        num[i][j][k] += num[i + 1][j * 2][k];
        num[i][j][k] += num[i + 1][j * 2 + 1][k];

        if (j & 1) {
          int dif = box[k] - 1;
          int nxk = k;
          nxk += 1 << (dif + 1);

          num[i][j][nxk] += num[i + 1][j * 2][k];
          num[i][j][nxk] += num[i + 1][j * 2 + 1][k];

          if (num[i + 1][j * 2].size() > nxk && nxk > 1) {
            num[i][j][nxk] -= num[i + 1][j * 2][nxk];
            num[i][j][nxk] -= num[i + 1][j * 2 + 1][nxk];
          }

        }

        else {
          int dif = box[k] - 1;
          int nxk = k;
          nxk -= 1 << dif;
          nxk += 1 << (dif + 1);
          // cout << k << " " << nxk << endl;
          // if(num[i][j].size()<=nxk)assert(0);
          num[i][j][nxk] += num[i + 1][j * 2][k];
          num[i][j][nxk] += num[i + 1][j * 2 + 1][k];
          if (num[i + 1][j * 2].size() > nxk && nxk > 1) {
            num[i][j][nxk] -= num[i + 1][j * 2][nxk];
            num[i][j][nxk] -= num[i + 1][j * 2 + 1][nxk];
          }
        }
      }
    }
  }
  string ans;
  for (int j = 1; j < 1 << (N + 1); j++) {
    if (num[1][0][j] + num[1][1][j] < K)
      continue;
    int dig = box[j] - 1;
    if (dig < ans.size())
      continue;
    string st(dig, '0');
    for (int k = 0; k < dig; k++) {
      if ((j >> k) & 1)
        st[k] = '1';
    }
    reverse(st.begin(), st.end());
    if (st.size() > ans.size())
      ans = st;
  }
  cout << ans << endl;
}