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

#include <iostream>
#include <vector>
using namespace std;

struct action {
  int type, val;
  action() {
    type = 0;
    val = 0;
  }
  action(int t, int v) {
    type = t;
    val = v;
  }
};

action cell[128];
double prob[128][5010][2];

int main() {
  int X, Y, Z;
  while (cin >> X >> Y >> Z, X || Y || Z) {
    vector<int> roulette;
    for (int i = 0; i < 128; i++) {
      cell[i] = action();
      for (int j = 0; j < 5010; j++) {
        prob[i][j][0] = prob[i][j][1] = 0;
      }
    }
    prob[0][0][0] = 1;
    for (int i = 0; i < X; i++) {
      int tmp;
      cin >> tmp;
      roulette.push_back(tmp);
    }
    for (int i = 0; i < 128; i++)
      cell[i] = action();
    for (int i = 0; i < Z; i++) {
      int N, E, A;
      cin >> N >> E >> A;
      cell[N] = action(E, A);
    }

    for (int i = 0; i <= Y; i++) {
      if (cell[i].type == 1)
        for (int j = 0; j < 5010; j++) {
          prob[i + cell[i].val][j][1] += prob[i][j][0];
        }
      for (int j = 0; j < roulette.size(); j++) {
        int add = 0;
        if (cell[i + roulette[j]].type == 2)
          add = cell[i + roulette[j]].val;
        else if (cell[i + roulette[j]].type == 3)
          add = -cell[i + roulette[j]].val;
        for (int k = 0; k < 5010; k++) {
          if (cell[i].type != 1)
            prob[i + roulette[j]][max(k + add, 0)][0] +=
                prob[i][k][0] / (double)roulette.size();
          prob[i + roulette[j]][max(k + add, 0)][0] +=
              prob[i][k][1] / (double)roulette.size();
        }
      }
    }

    double res = 0;
    for (int i = Y + 1; i < Y + 16; i++) {
      for (int j = 0; j < 5010; j++) {
        res += j * (prob[i][j][0] + prob[i][j][1]);
      }
    }
    res += 1e-9;
    cout << (int)res << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

#define double __float128

struct action {
  int type, val;
  action() {
    type = 0;
    val = 0;
  }
  action(int t, int v) {
    type = t;
    val = v;
  }
};

action cell[128];
double prob[128][5010];

int main() {
  int X, Y, Z;
  while (cin >> X >> Y >> Z, X || Y || Z) {
    vector<int> roulette;
    for (int i = 0; i < 128; i++) {
      cell[i] = action();
      for (int j = 0; j < 5010; j++) {
        prob[i][j] = 0;
      }
    }
    prob[0][0] = 1;
    for (int i = 0; i < X; i++) {
      int tmp;
      cin >> tmp;
      roulette.push_back(tmp);
    }
    for (int i = 0; i < 128; i++)
      cell[i] = action();
    for (int i = 0; i < Z; i++) {
      int N, E, A;
      cin >> N >> E >> A;
      cell[N] = action(E, A);
    }

    for (int i = 0; i <= Y; i++) {
      if (cell[i].type == 1)
        for (int j = 0; j < 5010; j++) {
          prob[i + 1][j] += prob[i][j];
        }
      else
        for (int j = 0; j < roulette.size(); j++) {
          int add = 0;
          if (cell[i + roulette[j]].type == 2)
            add = cell[i + roulette[j]].val;
          else if (cell[i + roulette[j]].type == 3)
            add = -cell[i + roulette[j]].val;
          for (int k = 0; k < 5010; k++) {
            prob[i + roulette[j]][max(k + add, 0)] +=
                prob[i][k] / (double)roulette.size();
          }
        }
    }

    double res = 0;
    for (int i = Y + 1; i < Y + 16; i++) {
      for (int j = 0; j < 5010; j++) {
        res += j * prob[i][j];
      }
    }
    res += 1e-9;
    cout << (int)res << endl;
  }
}