Problem p03350 (C++)

Buggy submission (status: Runtime Error):
// Author -- xyr2005

#include <bits/stdc++.h>

#define lowbit(x) ((x) & (-(x)))
#define Finline __inline__ __attribute__((always_inline))
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pl;
using std::max;
using std::min;

const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const ll INF = 0x3f3f3f3f3f3f3f3f;

Finline uint rnd() {
  static uint seed = 416;
  return seed += 0x71dad4bfu, seed ^= seed >> 5, seed += 0xc6f74d88u,
         seed ^= seed << 17, seed += 0x25e6561u, seed ^= seed >> 13;
}
template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) {
  return (!b) ? a : gcd(b, a % b);
}
template <typename _Tp> Finline _Tp abs(const _Tp &a) {
  return a >= 0 ? a : -a;
}
template <typename _Tp> Finline void chmax(_Tp &a, const _Tp &b) {
  (a < b) && (a = b);
}
template <typename _Tp> Finline void chmin(_Tp &a, const _Tp &b) {
  (b < a) && (a = b);
}
template <typename _Tp> Finline void read(_Tp &x) {
  char ch(getchar());
  bool f(false);
  while (!isdigit(ch))
    f |= ch == 45, ch = getchar();
  x = ch & 15, ch = getchar();
  while (isdigit(ch))
    x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
  f && (x = -x);
}
template <typename _Tp, typename... Args>
Finline void read(_Tp &t, Args &...args) {
  read(t);
  read(args...);
}
template <typename _Tp, typename... Args>
Finline _Tp min(const _Tp &a, const _Tp &b, const Args &...args) {
  return a < b ? min(a, args...) : min(b, args...);
}
template <typename _Tp, typename... Args>
Finline _Tp max(const _Tp &a, const _Tp &b, const Args &...args) {
  return a < b ? max(b, args...) : max(a, args...);
}
Finline int read_str(char *s) {
  char ch(getchar());
  while (ch == ' ' || ch == '\r' || ch == '\n')
    ch = getchar();
  char *tar = s;
  *tar = ch, ch = getchar();
  while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
    *(++tar) = ch, ch = getchar();
  return tar - s + 1;
}

const int N = (1 << 21) + 5;
int pool[N << 2], *p = pool;
int *dp[N];
int len[N], num[N], fir[N][2];
char t[N];
int main() {
  int n, k;
  read(n, k);
  int cur = 0;
  for (int i = 0; i <= n; ++i) {
    read_str(t);
    for (int j = 0; j < 1 << i; ++j) {
      len[cur] = i, num[cur] = j;
      dp[cur] = p, p += (1 << (n - i + 1));
      if (t[j] == '1')
        dp[0][cur] = 1;
      fir[cur][0] = fir[cur][1] = -1;
      for (int k = i - 1; k >= 0; --k)
        if (fir[cur][(j >> k) & 1] == -1)
          fir[cur][(j >> k) & 1] = k;
      ++cur;
    }
  }
  cur = 0;
  int ans = 0, len = 0;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j < 1 << i; ++j) {
      int tmp = 0;
      for (int p = 0; p <= n - i; ++p) {
        for (int q = 0; q < 1 << p; ++q) {
          if (~fir[tmp][0])
            dp[(1 << (i + 1)) - 1 + (j << 1)]
              [(1 << fir[tmp][0]) - 1 + (q & ((1 << fir[tmp][0]) - 1))] +=
                dp[cur][tmp];
          if (~fir[tmp][1])
            dp[(1 << (i + 1)) - 1 + (j << 1 | 1)]
              [(1 << fir[tmp][1]) - 1 + (q & ((1 << fir[tmp][1]) - 1))] +=
                dp[cur][tmp];
          if (tmp)
            dp[cur][0] += dp[cur][tmp];
          ++tmp;
        }
      }
      if (dp[cur][0] >= k && i > len)
        ans = j, len = i;
      ++cur;
    }
  }
  for (int i = len - 1; i >= 0; --i)
    putchar(48 + ((ans >> i) & 1));
  putchar('\n');
  return 0;
}


Fixed submission:
// Author -- xyr2005

#include <bits/stdc++.h>

#define lowbit(x) ((x) & (-(x)))
#define Finline __inline__ __attribute__((always_inline))
#define DEBUG                                                                  \
  fprintf(stderr, "Running on Line %d in Function %s\n", __LINE__, __FUNCTION__)
#define SZ(x) ((int)x.size())
#define mkpr std::make_pair
#define pb push_back

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef std::pair<int, int> pi;
typedef std::pair<ll, ll> pl;
using std::max;
using std::min;

const int inf = 0x3f3f3f3f, Inf = 0x7fffffff;
const ll INF = 0x3f3f3f3f3f3f3f3f;

Finline uint rnd() {
  static uint seed = 416;
  return seed += 0x71dad4bfu, seed ^= seed >> 5, seed += 0xc6f74d88u,
         seed ^= seed << 17, seed += 0x25e6561u, seed ^= seed >> 13;
}
template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) {
  return (!b) ? a : gcd(b, a % b);
}
template <typename _Tp> Finline _Tp abs(const _Tp &a) {
  return a >= 0 ? a : -a;
}
template <typename _Tp> Finline void chmax(_Tp &a, const _Tp &b) {
  (a < b) && (a = b);
}
template <typename _Tp> Finline void chmin(_Tp &a, const _Tp &b) {
  (b < a) && (a = b);
}
template <typename _Tp> Finline void read(_Tp &x) {
  char ch(getchar());
  bool f(false);
  while (!isdigit(ch))
    f |= ch == 45, ch = getchar();
  x = ch & 15, ch = getchar();
  while (isdigit(ch))
    x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar();
  f && (x = -x);
}
template <typename _Tp, typename... Args>
Finline void read(_Tp &t, Args &...args) {
  read(t);
  read(args...);
}
template <typename _Tp, typename... Args>
Finline _Tp min(const _Tp &a, const _Tp &b, const Args &...args) {
  return a < b ? min(a, args...) : min(b, args...);
}
template <typename _Tp, typename... Args>
Finline _Tp max(const _Tp &a, const _Tp &b, const Args &...args) {
  return a < b ? max(b, args...) : max(a, args...);
}
Finline int read_str(char *s) {
  char ch(getchar());
  while (ch == ' ' || ch == '\r' || ch == '\n')
    ch = getchar();
  char *tar = s;
  *tar = ch, ch = getchar();
  while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF)
    *(++tar) = ch, ch = getchar();
  return tar - s + 1;
}

const int N = (1 << 21) + 5;
int pool[N << 5], *p = pool;
int *dp[N];
int len[N], num[N], fir[N][2];
char t[N];
int main() {
  int n, k;
  read(n, k);
  int cur = 0;
  for (int i = 0; i <= n; ++i) {
    read_str(t);
    for (int j = 0; j < 1 << i; ++j) {
      len[cur] = i, num[cur] = j;
      dp[cur] = p, p += (1 << (n - i + 1));
      if (t[j] == '1')
        dp[0][cur] = 1;
      fir[cur][0] = fir[cur][1] = -1;
      for (int k = i - 1; k >= 0; --k)
        if (fir[cur][(j >> k) & 1] == -1)
          fir[cur][(j >> k) & 1] = k;
      ++cur;
    }
  }
  cur = 0;
  int ans = 0, len = 0;
  for (int i = 0; i <= n; ++i) {
    for (int j = 0; j < 1 << i; ++j) {
      int tmp = 0;
      for (int p = 0; p <= n - i; ++p) {
        for (int q = 0; q < 1 << p; ++q) {
          if (~fir[tmp][0])
            dp[(1 << (i + 1)) - 1 + (j << 1)]
              [(1 << fir[tmp][0]) - 1 + (q & ((1 << fir[tmp][0]) - 1))] +=
                dp[cur][tmp];
          if (~fir[tmp][1])
            dp[(1 << (i + 1)) - 1 + (j << 1 | 1)]
              [(1 << fir[tmp][1]) - 1 + (q & ((1 << fir[tmp][1]) - 1))] +=
                dp[cur][tmp];
          if (tmp)
            dp[cur][0] += dp[cur][tmp];
          ++tmp;
        }
      }
      if (dp[cur][0] >= k && i > len)
        ans = j, len = i;
      ++cur;
    }
  }
  for (int i = len - 1; i >= 0; --i)
    putchar(48 + ((ans >> i) & 1));
  putchar('\n');
  return 0;
}