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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;
int x, y, z;
vector<int> eve;
vector<int> board;
vector<int> n;
double dp[(1 << 13)][51] = {0.0};

int main() {
  while (true) {
    cin >> x >> y >> z;
    if (x == 0 && y == 0 && z == 0)
      break;
    memset(dp, 0, sizeof(dp));
    REP(i, x) {
      int temp;
      cin >> temp;
      n.PB(temp);
    }
    eve = vector<int>(y + 1, 0);
    board = vector<int>(y + 1, 0);
    REP(i, z) {
      int a, b, c;
      cin >> a >> b >> c;
      board[a] = b;
      eve[a] = c;
    }

    dp[0][0] = 1.0;
    REP(i, y) REP(j, (1 << 13)) REP(k, x) {
      int np = n[k] + i;
      np = min(np, y);
      int a = board[np];
      if (a == 1) {
        dp[j][min(y, np + eve[np])] += dp[j][i] / (double)(x);
      } else if (a == 2) {
        dp[min(5000, j + eve[np])][np] += dp[j][i] / (double)(x);
      } else if (a == 3) {
        dp[max(0, j - eve[np])][np] += dp[j][i] / (double)(x);
      } else {
        dp[j][np] += dp[j][i] / (double)(x);
      }
    }
    double ans = 0.0;
    REP(i, 5001) { ans += dp[i][y] * i; }
    cout << (int)(ans) << endl;
    n.clear();
    eve.clear();
    board.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)

#define PB push_back
#define INF INT_MAX / 3
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;
int x, y, z;
vector<int> eve;
vector<int> board;
vector<int> n;
int dp[5001][51][51] = {-1};

int saiki(int p, int m, int c) {

  m = max(m, 0);
  if (p >= y)
    return dp[m][c][p] = 10000 * m / pow(x, c);
  if (dp[m][c][p] > 0)
    return dp[m][c][p];
  int ans = 0;
  REP(i, x) {
    int a, np;
    np = p + n[i];
    np = min(y, np);
    a = board[np];
    if (a == 1) {
      ans += saiki(np + eve[np], m, c + 1);
    } else if (a == 2) {
      ans += saiki(np, m + eve[np], c + 1);
    } else if (a == 3) {
      ans += saiki(np, m - eve[np], c + 1);
    } else {
      ans += saiki(np, m, c + 1);
    }
  }

  return dp[m][c][p] = max(0, ans);
}

int main() {
  while (true) {
    cin >> x >> y >> z;
    if (x == 0 && y == 0 && z == 0)
      break;
    memset(dp, -1, sizeof(dp));
    REP(i, x) {
      int temp;
      cin >> temp;
      n.PB(temp);
    }
    eve = vector<int>(y + 1, 0);
    board = vector<int>(y + 1, 0);
    REP(i, z) {
      int a, b, c;
      cin >> a >> b >> c;
      board[a] = b;
      eve[a] = c;
    }
    cout << (int)(saiki(0, 0, 0) / 10000) << endl;
    n.clear();
    eve.clear();
    board.clear();
  }
  return 0;
}