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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// common
#define BR "\\n"

#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef long double ld;

// config
#define MODE_DEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

// debug
#ifdef MODE_DEBUG
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  DUMP(x) << " (L" << __LINE__ << ")"                                          \\
          << " " << __FILE__ << endl
#define CHECK(exp, act)                                                        \\
  if (exp != act) {                                                            \\
    DUMP(exp);                                                                 \\
    DEBUG(act);                                                                \\
  }
#define STOP(e)                                                                \\
  CHECK(e, true);                                                              \\
  if (!(e))                                                                    \\
    exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp, act)
#define STOP(e)
#endif

template <class T> inline string toString(const vector<T> &x) {
  stringstream ss;
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
  stringstream ss;
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  string res;
  stringstream ss;
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

template <typename T, typename V> inline T mod(T v, V MOD) {
  return (v % MOD + MOD) % MOD;
}

int X, Y, Z;
vector<int> vs;
struct Masu {
  int E, A;
  Masu(int E, int A) : E(E), A(A) {}
  Masu() : E(-1), A(-1) {}
};
vector<Masu> ms;

double EPS = 1e-9;
int main() {
  while (true) {
    cin >> X >> Y >> Z;
    if (X == 0 && Y == 0 && Z == 0)
      break;
    vs = vector<int>(X);
    REP(i, X) cin >> vs[i];

    ms = vector<Masu>(Y + 1);
    REP(i, Z) {
      int N, E, A;
      cin >> N >> E >> A;
      ms[N] = Masu(E, A);
    }
    int MAXM = 6020;
    vector<vector<double>> dp(Y + 1, vector<double>(MAXM + 1000));
    dp[0][0] = 1.0;
    REP(p, Y) {
      REP(m, MAXM) {
        if (dp[p][m] < EPS)
          continue;
        REP(x, X) {
          int nxt = min(Y, p + vs[x]);
          switch (ms[nxt].E) {
          case 1: // move
            nxt = min(Y, nxt + ms[nxt].A);
            dp[nxt][m] += dp[p][m] / X;
            break;
          case 2: // add money
            dp[nxt][m + ms[nxt].A] += dp[p][m] / X;
            break;
          case 3: // remove money
            dp[nxt][max(0, m - ms[nxt].A)] += dp[p][m] / X;
            break;
          default: // no event
            dp[nxt][m] += dp[p][m] / X;
            break;
          }
        }
      }
    }

    double Exp = 0;
    REP(m, MAXM) Exp += m * dp[Y][m];
    cout << int(Exp) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// common
#define BR "\\n"

#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef long double ld;

// config
#define MODE_DEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

// debug
#ifdef MODE_DEBUG
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  DUMP(x) << " (L" << __LINE__ << ")"                                          \\
          << " " << __FILE__ << endl
#define CHECK(exp, act)                                                        \\
  if (exp != act) {                                                            \\
    DUMP(exp);                                                                 \\
    DEBUG(act);                                                                \\
  }
#define STOP(e)                                                                \\
  CHECK(e, true);                                                              \\
  if (!(e))                                                                    \\
    exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp, act)
#define STOP(e)
#endif

template <class T> inline string toString(const vector<T> &x) {
  stringstream ss;
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
  stringstream ss;
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  string res;
  stringstream ss;
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

template <typename T, typename V> inline T mod(T v, V MOD) {
  return (v % MOD + MOD) % MOD;
}

int X, Y, Z;
vector<int> vs;
struct Masu {
  int E, A;
  Masu(int E, int A) : E(E), A(A) {}
  Masu() : E(-1), A(-1) {}
};
vector<Masu> ms;

double dfs(int pos, int turn, int val) {
  if (pos >= Y) {
    // DUMP(pos);DUMP(turn);DUMP(val);
    return 1.0 * val / pow(1.0 * X, turn);
  }

  double res = 0;
  REP(i, X) {
    int nxt = vs[i] + pos;
    int nxtval = val;
    switch (ms[nxt].E) {
    case 1:
      nxt += ms[nxt].A;
      break;
    case 2:
      nxtval += ms[nxt].A;
      break;
    case 3:
      nxtval = max(0, nxtval - ms[nxt].A);
      break;
    }
    res += dfs(nxt, turn + 1, nxtval);
  }
  return res;
}

int main() {
  while (true) {
    cin >> X >> Y >> Z;
    if (X == 0 && Y == 0 && Z == 0)
      break;

    vs = vector<int>(X);
    REP(i, X) cin >> vs[i];
    ms = vector<Masu>(Y);
    REP(i, Z) {
      int N, E, A;
      cin >> N >> E >> A;
      ms[N] = Masu(E, A);
    }
    double Exp = dfs(0, 0, 0);
    stringstream ss;
    ss << Exp;
    ll res;
    ss >> res;
    cout << res << endl;
  }
  return 0;
}