Problem p00232 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); 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()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

double memo[51][100 * 51 + 1];

int main() {
  while (true) {
    int x = getInt();
    int y = getInt();
    int z = getInt();

    int r[4];
    int ee[51];
    int ea[51];

    if (x + y + z == 0)
      break;

    REP(i, y + 1) REP(j, 100 * 50 + 1) memo[i][j] = 0;
    REP(i, y + 1) ee[i] = ea[i] = 0;

    REP(i, x) r[i] = getInt();
    REP(i, z) {
      int n = getInt();
      ee[n] = getInt();
      ea[n] = getInt();
    }

    memo[0][0] = 1.0;
    REP(i, y) {
      REP(j, i * 100 + 1) {
        REP(k, x) {
          int nextPos = i + r[k];
          int earn = j;
          if (nextPos > y)
            nextPos = y;

          switch (ee[nextPos]) {
          case 1:
            nextPos += ea[nextPos];
            break;
          case 2:
            earn += ea[nextPos];
            break;
          case 3:
            earn -= ea[nextPos];
            break;
          }

          if (nextPos > y)
            nextPos = y;
          if (earn < 0)
            earn = 0;
          memo[nextPos][earn] += memo[i][j] / x;
        }
      }
    }

    double ans = 0.0;
    REP(i, y * 100 + 1) ans += i * memo[y][i];

    print((int)floor(ans + 1e-10));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); 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()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

double memo[50][100 * 50 + 1];

int main() {
  while (true) {
    int x = getInt();
    int y = getInt();
    int z = getInt();

    int r[4];
    int ee[50];
    int ea[50];

    if (x + y + z == 0)
      break;

    REP(i, y + 1) REP(j, 100 * 50 + 1) memo[i][j] = 0;
    REP(i, y + 1) ee[i] = ea[i] = 0;

    REP(i, x) r[i] = getInt();
    REP(i, z) {
      int n = getInt();
      ee[n] = getInt();
      ea[n] = getInt();
    }

    memo[0][0] = 1.0;
    REP(i, y) {
      REP(j, i * 100 + 1) {
        REP(k, x) {
          int nextPos = i + r[k];
          int earn = j;
          if (nextPos > y)
            nextPos = y;

          switch (ee[nextPos]) {
          case 1:
            nextPos += ea[nextPos];
            break;
          case 2:
            earn += ea[nextPos];
            break;
          case 3:
            earn -= ea[nextPos];
            break;
          }

          if (nextPos > y)
            nextPos = y;
          if (earn < 0)
            earn = 0;
          memo[nextPos][earn] += memo[i][j] / x;
        }
      }
    }

    double ans = 0.0;
    REP(i, y * 100 + 1) ans += i * memo[y][i];

    print((int)floor(ans + 1e-10));
  }
  return 0;
}