Problem p03350 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int N = 21, M = (1 << 21) + 100;
const db pi = acos(-1.0);
#define lowbit(x) ((x) & (-x))
#define sqr(x) (x) * (x)
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define per(i, a, b) for (register int i = a; i >= b; i--)
#define go(u, i)                                                               \
  for (register int i = head[u], v = sq[i].to; i; i = sq[i].nxt, v = sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

inline ll readll() {
  ll x = 0;
  int f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

int n, k, f[N][M], tr[N][M], low[M];
char s[M];

int main() {
  n = read();
  k = read();
  rep(i, 0, n) {
    scanf("%s", s);
    int tmp = (1 << i);
    rep(j, 0, (1 << i) - 1) f[i][tmp | j] = (s[j] - '0');
  }
  low[0] = -1;
  rep(i, 2, (2 << n) - 1) low[i] = low[i >> 1] + 1;
  rep(i, 2, (2 << n) - 1) {
    if ((i >> (low[i] - 1)) & 1) {
      tr[i][1] = i ^ (1 << low[i]);
      tr[i][0] = tr[tr[i][1]][0];
    } else {
      tr[i][0] = i ^ (3 << (low[i] - 1));
      tr[i][1] = tr[tr[i][0]][1];
    }
    // cout << i << " " << tr[i][0] << " " << tr[i][1] << endl;
  }
  // rep(i,1,(2<<n)-1) cout << low[i] << " ";cout << endl;
  per(i, n, 1) {
    rep(j, (1 << i), (2 << n) - 1) {
      if (!f[i][j])
        continue;
      int s = (j >> i), t = (j & ((1 << i) - 1)) ^ (1 << i);
      if (tr[t][0]) {
        f[low[tr[t][0]]][(((s << 1) ^ 1) << low[tr[t][0]]) ^ tr[t][0]] +=
            f[i][j];
      }
      if (tr[t][1]) {
        f[low[tr[t][1]]][(((s << 1) ^ 0) << low[tr[t][1]]) ^ tr[t][1]] +=
            f[i][j];
      }
      f[0][s] += f[i][j];
    }
  }
  int ans = 1;
  rep(i, 2, (2 << n) - 1) if ((f[0][i] >= k) && (low[i] > low[ans])) ans = i;
  // cout << ans << endl;
  per(i, low[ans] - 1, 0) printf("%d", (ans >> i) & 1);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double db;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int N = 21, M = (1 << 21) + 100;
const db pi = acos(-1.0);
#define lowbit(x) ((x) & (-x))
#define sqr(x) (x) * (x)
#define rep(i, a, b) for (register int i = a; i <= b; i++)
#define per(i, a, b) for (register int i = a; i >= b; i--)
#define go(u, i)                                                               \
  for (register int i = head[u], v = sq[i].to; i; i = sq[i].nxt, v = sq[i].to)
#define fir first
#define sec second
#define mkp make_pair
#define pb push_back
#define maxd 998244353
#define eps 1e-8
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

inline ll readll() {
  ll x = 0;
  int f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + (ch - '0');
    ch = getchar();
  }
  return x * f;
}

int n, k, f[N][M], tr[M][2], low[M];
char s[M];

int main() {
  n = read();
  k = read();
  rep(i, 0, n) {
    scanf("%s", s);
    int tmp = (1 << i);
    rep(j, 0, (1 << i) - 1) f[i][tmp | j] = (s[j] - '0');
  }
  low[0] = -1;
  rep(i, 2, (2 << n) - 1) low[i] = low[i >> 1] + 1;
  rep(i, 2, (2 << n) - 1) {
    if ((i >> (low[i] - 1)) & 1) {
      tr[i][1] = i ^ (1 << low[i]);
      tr[i][0] = tr[tr[i][1]][0];
    } else {
      tr[i][0] = i ^ (3 << (low[i] - 1));
      tr[i][1] = tr[tr[i][0]][1];
    }
    // cout << i << " " << tr[i][0] << " " << tr[i][1] << endl;
  }
  // rep(i,1,(2<<n)-1) cout << low[i] << " ";cout << endl;
  per(i, n, 1) {
    rep(j, (1 << i), (2 << n) - 1) {
      if (!f[i][j])
        continue;
      int s = (j >> i), t = (j & ((1 << i) - 1)) ^ (1 << i);
      if (tr[t][0]) {
        f[low[tr[t][0]]][(((s << 1) ^ 1) << low[tr[t][0]]) ^ tr[t][0]] +=
            f[i][j];
      }
      if (tr[t][1]) {
        f[low[tr[t][1]]][(((s << 1) ^ 0) << low[tr[t][1]]) ^ tr[t][1]] +=
            f[i][j];
      }
      f[0][s] += f[i][j];
    }
  }
  int ans = 1;
  rep(i, 2, (2 << n) - 1) if ((f[0][i] >= k) && (low[i] > low[ans])) ans = i;
  // cout << ans << endl;
  per(i, low[ans] - 1, 0) printf("%d", (ans >> i) & 1);
  return 0;
}