Problem p03350 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define REP(i, n) FOR(i, 0, n)
#define each(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#define deb(x) cerr << #x << " = " << (x) << " , ";
#define debl cerr << " (L" << __LINE__ << ")" << endl;
#define sz(s) (int)((s).size())

#define clr(a) memset((a), 0, sizeof(a))
#define nclr(a) memset((a), -1, sizeof(a))
#define pb push_back
#define INRANGE(x, s, e) ((s) <= (x) && (x) < (e))
#define MP(x, y) make_pair((x), (y))

double pi = 3.14159265358979323846;

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template <typename T>
std::ostream &operator<<(std::ostream &os, const vector<T> &z) {
  os << "[ ";
  REP(i, z.size()) os << z[i] << ", ";
  return (os << "]" << endl);
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const set<T> &z) {
  os << "set( ";
  EACH(p, z) os << (*p) << ", ";
  return (os << ")" << endl);
}

template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const map<T, U> &z) {
  os << "{ ";
  EACH(p, z) os << (p->first) << ": " << (p->second) << ", ";
  return (os << "}" << endl);
}

template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const pair<T, U> &z) {
  return (os << "(" << z.first << ", " << z.second << ",)");
}

double get_time() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec + tv.tv_usec * 1e-6;
}

typedef unsigned int uint32_t;
struct RND {
  uint32_t x;
  uint32_t y;
  uint32_t z;
  uint32_t w;
  RND() {
    x = 123456789;
    y = 362436069;
    z = 521288629;
    w = 88675123;
  }
  void init(int seed) {
    x = 123456789;
    y = 362436069;
    z = 521288629;
    w = seed + 100;
    REP(i, 10) get();
  }
  uint32_t get() {
    uint32_t t;
    t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w;
  }
};
RND rnd;

ll double_to_ll(double d) {
  if (d > 0)
    return d + 0.5;
  return d - 0.5;
}

ll N, K;
int memo[1 << 20][240];
int target[1 << 20][22];

int rec(int b1, int l1, int b2, int l2) {
  int b = (b2 << l1) + b1;
  int l = (l1 + l2) * (l1 + l2 + 1) / 2 + l2;
  if (l1 + l2 > N)
    return 0;
  if (l1 + l2 == N) {
    return target[b][l1 + l2];
  }

  int &ret = memo[b][l];
  if (ret != -1)
    return ret;
  ll ans = 0;
  if (l1 == 0 && target[b2][l2]) {
    ans += 1;
  }
  if (l1 == 0) {
    ans += rec(b1, l1, 2 * b2, l2 + 1);
    ans += rec(b1, l1, 2 * b2 + 1, l2 + 1);
  } else if ((b1 >> (l1 - 1)) % 2 == 0) {
    ans += rec(b1, l1 - 1, 2 * b2, l2 + 1);
    ans += rec(b1, l1, 2 * b2 + 1, l2 + 1);
  } else if ((b1 >> (l1 - 1)) % 2 == 1) {
    ans += rec(b1 - (1 << (l1 - 1)), l1 - 1, 2 * b2 + 1, l2 + 1);
    ans += rec(b1, l1, 2 * b2, l2 + 1);
  } else {
    assert(false);
  }
  return ret = min(ans, K);
}

void _main(istream &inp) {
  inp >> N >> K;
  rep(i, N + 1) {
    string s;
    inp >> s;
    assert(s.size() == (1 << i));
    rep(j, (1 << i)) {
      if (s[j] == '1') {
        target[j][i] = 1;
      } else
        target[j][i] = 0;
    }
  }
  nclr(memo);

  for (int len = 20; len >= 0; len--) {
    rep(b, 1 << len) {
      string s;
      rep(i, len) {
        if ((b >> i) % 2 == 0)
          s.push_back('0');
        else
          s.push_back('1');
      }
      reverse(s.begin(), s.end());
      //			int res = rec(b, len, 0, 0);
      //			if(res!=0){
      //				debug(s);
      //				debug(res);
      //			}
      if (rec(b, len, 0, 0) >= K) {
        cout << s << endl;
        return;
      }
    }
  }
}

int main() {
  if (0) {
    ifstream ifs("test.txt");
    _main(ifs);
  } else {
    _main(cin);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <sys/time.h>
#include <utility>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define REP(i, n) FOR(i, 0, n)
#define each(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define EACH(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#define deb(x) cerr << #x << " = " << (x) << " , ";
#define debl cerr << " (L" << __LINE__ << ")" << endl;
#define sz(s) (int)((s).size())

#define clr(a) memset((a), 0, sizeof(a))
#define nclr(a) memset((a), -1, sizeof(a))
#define pb push_back
#define INRANGE(x, s, e) ((s) <= (x) && (x) < (e))
#define MP(x, y) make_pair((x), (y))

double pi = 3.14159265358979323846;

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template <typename T>
std::ostream &operator<<(std::ostream &os, const vector<T> &z) {
  os << "[ ";
  REP(i, z.size()) os << z[i] << ", ";
  return (os << "]" << endl);
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const set<T> &z) {
  os << "set( ";
  EACH(p, z) os << (*p) << ", ";
  return (os << ")" << endl);
}

template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const map<T, U> &z) {
  os << "{ ";
  EACH(p, z) os << (p->first) << ": " << (p->second) << ", ";
  return (os << "}" << endl);
}

template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const pair<T, U> &z) {
  return (os << "(" << z.first << ", " << z.second << ",)");
}

double get_time() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec + tv.tv_usec * 1e-6;
}

typedef unsigned int uint32_t;
struct RND {
  uint32_t x;
  uint32_t y;
  uint32_t z;
  uint32_t w;
  RND() {
    x = 123456789;
    y = 362436069;
    z = 521288629;
    w = 88675123;
  }
  void init(int seed) {
    x = 123456789;
    y = 362436069;
    z = 521288629;
    w = seed + 100;
    REP(i, 10) get();
  }
  uint32_t get() {
    uint32_t t;
    t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w;
  }
};
RND rnd;

ll double_to_ll(double d) {
  if (d > 0)
    return d + 0.5;
  return d - 0.5;
}

ll N, K;
int memo[1 << 19][240];
int target[1 << 20][22];

int rec(int b1, int l1, int b2, int l2) {
  int b = (b2 << l1) + b1;
  int l = (l1 + l2) * (l1 + l2 + 1) / 2 + l2;
  if (l1 + l2 > N)
    return 0;
  if (l1 + l2 == N) {
    return target[b][l1 + l2];
  }

  int &ret = memo[b][l];
  if (ret != -1)
    return ret;
  ll ans = 0;
  if (l1 == 0 && target[b2][l2]) {
    ans += 1;
  }
  if (l1 == 0) {
    ans += rec(b1, l1, 2 * b2, l2 + 1);
    ans += rec(b1, l1, 2 * b2 + 1, l2 + 1);
  } else if ((b1 >> (l1 - 1)) % 2 == 0) {
    ans += rec(b1, l1 - 1, 2 * b2, l2 + 1);
    ans += rec(b1, l1, 2 * b2 + 1, l2 + 1);
  } else if ((b1 >> (l1 - 1)) % 2 == 1) {
    ans += rec(b1 - (1 << (l1 - 1)), l1 - 1, 2 * b2 + 1, l2 + 1);
    ans += rec(b1, l1, 2 * b2, l2 + 1);
  } else {
    assert(false);
  }
  return ret = min(ans, K);
}

void _main(istream &inp) {
  inp >> N >> K;
  rep(i, N + 1) {
    string s;
    inp >> s;
    assert(s.size() == (1 << i));
    rep(j, (1 << i)) {
      if (s[j] == '1') {
        target[j][i] = 1;
      } else
        target[j][i] = 0;
    }
  }
  nclr(memo);

  for (int len = 20; len >= 0; len--) {
    rep(b, 1 << len) {
      string s;
      rep(i, len) {
        if ((b >> i) % 2 == 0)
          s.push_back('0');
        else
          s.push_back('1');
      }
      reverse(s.begin(), s.end());
      //			int res = rec(b, len, 0, 0);
      //			if(res!=0){
      //				debug(s);
      //				debug(res);
      //			}
      if (rec(b, len, 0, 0) >= K) {
        cout << s << endl;
        return;
      }
    }
  }
}

int main() {
  if (0) {
    ifstream ifs("test.txt");
    _main(ifs);
  } else {
    _main(cin);
  }
  return 0;
}