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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#ifdef __BORLANDC__

#else
// #define double __float128
#endif

typedef pair<int, int> pii;

vector<int> v;
vector<pii> board;

bool input() {
  int x, y, z;
  cin >> x >> y >> z;
  if (x == 0 && y == 0 && z == 0)
    return false;
  v.resize(x);
  board.clear();
  board.resize(y);
  rep(i, x) cin >> v[i];
  rep(i, z) {
    int n, a, e;
    cin >> n >> a >> e;
    board[n] = pii(a, e);
  }
  return true;
}

const int N = 66;
const int M = N * 111;
double dp[N][M][2];

double solve() {

  rep(i, N) rep(j, M) rep(k, 2) dp[i][j][k] = 0;

  dp[0][0][0] = 1;

  rep(i, board.size()) {
    rep(p, 2) {
      int kind = board[i].first;
      int value = board[i].second;

      if (kind == 0 || p == 1) {
        rep(j, v.size()) {
          rep(k, M) { dp[i + v[j]][k][0] += dp[i][k][p] * (1.0 / v.size()); }
        }
      } else {
        if (kind == 1) {
          rep(k, M) { dp[i + value][k][1] += dp[i][k][p]; }
        }
        if (kind == 2) {
          rep(j, v.size()) {
            rep(k, M - value) {
              dp[i + v[j]][k + value][0] += dp[i][k][p] * (1.0 / v.size());
            }
          }
        }
        if (kind == 3) {
          rep(j, v.size()) {
            rep(k, M) {
              int pos = max(0, k - value);
              dp[i + v[j]][pos][0] += dp[i][k][p] * (1.0 / v.size());
            }
          }
        }
      }
    }
  }

  double ret = 0;
  reps(i, board.size(), board.size() + 16) {
    rep(j, M) {
      rep(k, 2) { ret += dp[i][j][k] * j; }
    }
  }
  return ret;
}

void call() { printf("%d\\n", int(solve() + 1e-9)); }

int main() {
  while (input())
    call();
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

#ifdef __BORLANDC

#else
#define double __float128
#endif

typedef pair<int, int> pii;

vector<int> v;
vector<pii> board;

bool input() {
  int x, y, z;
  cin >> x >> y >> z;
  if (x == 0 && y == 0 && z == 0)
    return false;
  v.resize(x);
  board.resize(y);
  rep(i, x) cin >> v[i];
  rep(i, z) {
    int n, a, e;
    cin >> n >> a >> e;
    board[n] = pii(a, e);
  }
  return true;
}

const int N = 66;
const int M = N * 111;
double dp[N][M][2];

double solve() {

  rep(i, N) rep(j, M) rep(k, 2) dp[i][j][k] = 0;

  dp[0][0][0] = 1;

  rep(i, board.size()) {
    rep(p, 2) {
      int kind = board[i].first;
      int value = board[i].second;

      int m = 0;
      rep(j, i) if (board[j].first == 2) m += board[j].second;
      m++;

      if (kind == 0 || p == 1) {
        rep(j, v.size()) {
          rep(k, m) { dp[i + v[j]][k][0] += dp[i][k][p] / v.size(); }
        }
      } else {
        if (kind == 1) {
          rep(k, m) { dp[i + value][k][1] += dp[i][k][p]; }
        }
        if (kind == 2) {
          rep(j, v.size()) {
            rep(k, m) { dp[i + v[j]][k + value][0] += dp[i][k][p] / v.size(); }
          }
        }
        if (kind == 3) {
          rep(j, v.size()) {
            rep(k, m) {
              int pos = max(0, k - value);
              dp[i + v[j]][pos][0] += dp[i][k][p] / v.size();
            }
          }
        }
      }
    }
  }

  double ret = 0;
  reps(i, board.size(), N) {
    rep(j, M) {
      rep(k, 2) { ret += dp[i][j][k] * j; }
    }
  }
  return ret;
}

void call() { printf("%d\\n", int(solve() + 0.000001)); }

int main() {
  while (input())
    call();
}