Problem p00850 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef long long ll;

bool flag[1501];
int v[1501];
int n;

bool can(int a, int rest) {
  while (rest--)
    a += a;
  return a >= n;
}

bool dfs(int now, int d, int len, int ma) {
  if (d == 0)
    return flag[n];
  if (d == 1)
    return flag[abs(now - n)];
  if (!can(ma, d))
    return 0;
  // cout << now << " " << d << endl;
  // cout << "v = ";
  // REP(i, len) cout << v[i] << " ";
  // cout << endl;
  for (int i = len - 1; i >= 0; i--) {
    int next = v[i] + now;
    if (next <= n * 3 / 2 && !flag[next]) {
      v[len] = next;
      flag[next] = 1;
      if (dfs(next, d - 1, len + 1, max(ma, next)))
        return 1;
      flag[next] = 0;
    }
    next = now - v[i];
    if (next > 0 && !flag[next]) {
      v[len] = next;
      flag[next] = 1;
      if (dfs(next, d - 1, len + 1, ma))
        return 1;
      flag[next] = 0;
    }
  }
  return 0;
}

int main() {
  while (cin >> n, n) {
    // for (n=1;n<=1000;++n) {
    REP(p, 14) {
      if (p == 13) {
        cout << p << endl;
        break;
      }
      // cout << "p = " << p << endl;
      memset(flag, 0, sizeof(flag));
      flag[1] = 1;
      v[0] = 1;
      if (dfs(1, p, 1, 1)) {
        cout << p << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef long long ll;

bool flag[1501];
int v[1501];
int p;
int n;

bool can(int a, int ma, int rest) {
  a += ma;
  while (rest--)
    a += a;
  return a >= n;
}

bool dfs(int now, int d, int len, int ma) {
  if (now == n)
    return 1;
  if (d == 0)
    return 0;
  if (d == 1)
    return flag[abs(now - n)];
  // cout << now << " " << d << endl;
  // cout << "v = ";
  // REP(i, len) cout << v[i] << " ";
  // cout << endl;
  for (int i = len - 1; i >= 0; i--) {
    int next = v[i] + now;
    if (next <= n * 3 / 2 && !flag[next] && can(next, ma, d)) {
      v[len] = next;
      flag[next] = 1;
      if (dfs(next, d - 1, len + 1, max(ma, next)))
        return 1;
      flag[next] = 0;
    }
    next = abs(v[i] - now);
    if (next > 0 && !flag[next] && can(next, ma, d)) {
      v[len] = next;
      flag[next] = 1;
      if (dfs(next, d - 1, len + 1, max(ma, next)))
        return 1;
      flag[next] = 0;
    }
  }
  return 0;
}

int main() {

  while (cin >> n, n) {
    // for (n=1;n<=1000;++n) {
    if (n == 1) {
      cout << 0 << endl;
      continue;
    }
    for (p = 1;; p++) {
      // cout << "p = " << p << endl;
      memset(flag, 0, sizeof(flag));
      flag[1] = 1;
      v[0] = 1;
      if (dfs(1, p, 1, 1)) {
        cout << p << endl;
        break;
      }
    }
  }
}