alexjercan/bugnet huggingface dataset:
Problem p00232 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
double EPS = 1e-10;
int INF = 1e9;
double dp[100][5001];
int v[100], e1[100], e2[100];
int main() {
  int x, y, z;
  while (cin >> x >> y >> z && (x || y || z)) {
    REP(i, x) cin >> v[i];
    REP(i, 100) e1[i] = e2[i] = 0;
    REP(i, z) {
      int a, b, c;
      cin >> a >> b >> c;
      e1[a] = b;
      e2[a] = c;
    }
    REP(i, y + 1) REP(j, 5001) dp[i][j] = 0.0;
    dp[0][0] = 1.0;
    int t, s;
    REP(i, y) {
      REP(j, 5001) {
        if (dp[i][j] == 0.0)
          continue;
        REP(k, x) {
          t = min(y, i + v[k]);
          s = j;
          if (e1[t] == 1) {
            t = min(y, t + e2[t]);
          } else if (e1[t] == 2) {
            s += e2[t];
          } else if (e1[t] == 3) {
            s = max(0, s - e2[t]);
          }

          dp[t][s] += dp[i][j] * (1.0 / x);
        }
      }
    }
    double ans = 0.0;
    /*REP(i,50){
          REP(j,100)cout<<dp[i][j]<<" ";cout<<endl;
    }*/
    REP(i, 5001) ans += dp[y][i] * (double)i;
    cout << (int)ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
double EPS = 1e-10;
int INF = 1e9;
double dp[50][5001];
int v[4], e1[51], e2[51];
int main() {
  int x, y, z;
  while (cin >> x >> y >> z && (x || y || z)) {
    REP(i, x) cin >> v[i];
    REP(i, 51) e1[i] = e2[i] = 0;
    REP(i, z) {
      int a, b, c;
      cin >> a >> b >> c;
      e1[a] = b;
      e2[a] = c;
    }
    REP(i, 50) REP(j, 5001) dp[i][j] = 0.0;
    dp[0][0] = 1.0;
    int t, s;
    REP(i, y) {
      REP(j, 5001) {
        if (dp[i][j] == 0.0)
          continue;
        REP(k, x) {
          t = min(y, i + v[k]);
          s = j;
          if (e1[t] == 1) {
            t = min(y, i + e2[t]);
          } else if (e1[t] == 2) {
            s += e2[t];
          } else if (e1[t] == 3) {
            s = max(0, s - e2[t]);
          }

          dp[t][s] += dp[i][j] / (double)x;
        }
      }
    }
    double ans = 0.0;
    /*REP(i,50){
          REP(j,100)cout<<dp[i][j]<<" ";cout<<endl;
    }*/
    REP(i, 5001) ans += dp[y][i] * (double)i;
    cout << (int)ans << endl;
  }
}
Problem p00232 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  int X, Y, Z;
  int V[4];
  int E[51];
  int A[51];
  double dp[51][5001];
  while (1) {
    REP(i, 51) {
      E[i] = 0;
      A[i] = 0;
    }
    REP(i, 51) REP(j, 5001) dp[i][j] = 0;
    cin >> X >> Y >> Z;
    if (X == 0 && Y == 0 && Z == 0)
      break;
    REP(i, X) cin >> V[i];
    REP(i, Z) {
      int n, e, a;
      cin >> n >> e >> a;
      E[n] = e;
      A[n] = a;
    }

    // dp[i][j] i??????????????????j????????£??????????¢????
    dp[0][0] = 1.0;
    REP(i, Y) {
      REP(j, 5001) {
        REP(k, X) {
          int next = i + V[k];
          int money = j;
          if (next >= Y)
            next = Y;
          if (E[next] == 1) {
            next += A[next];
            if (next >= Y)
              next = Y;
          } else if (E[next] == 2) {
            money += A[next];
          } else if (E[next] == 3) {
            money -= A[next];
          }
          if (money < 0)
            money = 0;
          dp[next][money] += dp[i][j] / (double)X;
        }
      }
    }
    double sum = 0;
    REP(i, 5001) sum += i * dp[Y][i];
    cout << (int)sum << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll int n, x;
  ll int a[2000];
  ll int k[2000];
  ll int data[2000][2000]; // i?????? j??????????????§
  ll int maxkaisu = 0;
  ll int sum = 0;
  cin >> n >> x;
  REP(i, n) cin >> a[i];
  REP(i, n) { // j??????i????????£?????????
    vector<pair<ll int, ll int>> p;
    REP(j, n) {
      if (i == j)
        continue;
      int t;
      t = i - j;
      if (t < 0)
        t += n;
      p.pb(mp(a[j] + x * t, t));
    }
    sort(All(p));
    ll int kaisu;
    if (p[0].first >= a[i]) {
      kaisu = 0;
      k[i] = a[i];
    } else {
      kaisu = p[0].second;
      k[i] = p[0].first - x * p[0].second;
    }
    maxkaisu = max(kaisu, maxkaisu);
  }
  REP(i, n) sum += k[i];
  // REP(i,n) cout << k[i] << " ";
  cout << sum + x * maxkaisu << endl;
}
Problem p00232 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

double dp[51][5001];
int v[4], e[51], a[51];
int main() {
  int x, y, z;
  while (scanf("%d%d%d", &x, &y, &z), x) {
    memset(e, 0, sizeof(e));
    memset(dp, 0, sizeof(dp));
    rep(i, x) scanf("%d", &v[i]);
    rep(i, z) {
      int n;
      scanf("%d", &n);
      scanf("%d%d", &e[n], &a[n]);
    }
    dp[0][0] = 1;
    rep(i, y) rep(j, 5001) {
      if (dp[i][j] == 0)
        continue;
      rep(k, x) {
        int t = min(y, i + v[k]);
        if (!e[t])
          dp[t][j] += dp[i][j] / x;
        if (e[t] == 1)
          dp[min(y, t + a[t])][j] += dp[i][j] / x;
        if (e[t] == 2)
          dp[t][j + a[t]] += dp[i][j] / x;
        if (e[t] == 3)
          dp[t][max(0, j - a[t])] += dp[i][j] / x;
      }
    }
    double ans = 0;
    rep(i, 5001) ans += i * dp[y][i];
    printf("%d\\n", (int)ans);
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

float dp[101][51][5001];
int e[54], a[54], v[5];
int main() {
  int x, y, z;
  while (scanf("%d%d%d", &x, &y, &z), x) {
    memset(e, 0, sizeof(e));
    memset(a, 0, sizeof(a));
    memset(dp, 0, sizeof(dp));
    rep(i, x) scanf("%d", &v[i]);
    rep(i, z) {
      int n;
      scanf("%d", &n);
      scanf("%d%d", &e[n], &a[n]);
    }
    dp[0][0][0] = 1;
    rep(i, 101) rep(j, y) rep(t, 5001) {
      if (!dp[i][j][t])
        continue;
      if (e[j] == 1) {
        int b = min(y, j + a[j]);
        if (e[b] == 2)
          dp[i + 1][b][t + a[b]] += dp[i][j][t];
        else if (e[b] == 3)
          dp[i + 1][b][max(0, t - a[b])] += dp[i][j][t];
        else
          dp[i + 1][b][t] += dp[i][j][t];
        continue;
      }
      rep(k, x) {
        int b = min(y, j + v[k]);
        if (e[b] == 2)
          dp[i + 1][b][t + a[b]] += dp[i][j][t] / x;
        else if (e[b] == 3)
          dp[i + 1][b][max(0, t - a[b])] += dp[i][j][t] / x;
        else
          dp[i + 1][b][t] += dp[i][j][t] / x;
      }
    }
    float sum = 0;
    rep(i, 101) rep(j, 5001) { sum += j * dp[i][y][j]; }
    printf("%d\\n", (int)sum);
  }
}
Problem p00232 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
double fie[51][10101] = {1};
int main() {
  while (1) {
    int X, Y, Z;
    int V[6];
    int E[53][2] = {0};
    for (int i = 0; i < 51; i++) {
      for (int j = 0; j < 10101; j++) {
        fie[i][j] = 0;
      }
    }
    fie[0][0] = 1;
    cin >> X >> Y >> Z;
    if (X == 0 && Y == 0 & Z == 0)
      break;
    for (int i = 0; i < X; i++)
      cin >> V[i];
    for (int i = 0; i < Z; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      E[a][0] = b, E[a][1] = c;
    }
    for (int i = 0; i < Y; i++) {
      for (int j = 0; j <= 5000; j++) {
        for (int k = 0; k < X; k++) {
          if (Y <= i + V[k] ||
              (E[i + V[k]][0] == 1 && Y <= i + V[k] + E[i + V[k]][1]))
            fie[Y][j] += fie[i][j] / X;
          else if (E[i + V[k]][0] == 0)
            fie[i + V[k]][j] += fie[i][j] / X;
          else if (E[i + V[k]][0] == 1)
            fie[i + V[k] + E[i + V[k]][1]][j] += fie[i][j] / X;
          else if (E[i + V[k]][0] == 2)
            fie[i + V[k]][j + E[i + V[k]][1]] += fie[i][j] / X;
          else if (E[i + V[k]][0] == 3)
            fie[i + V[k]][max(0, j - E[i + V[k]][1])] += fie[i][j] / X;
        }
      }
    }
    double ans = 0;
    for (int i = 0; i <= 5100; i++)
      ans += fie[Y][i] * i;
    cout << (int)ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
double fie[51][5101] = {1};
int main() {
  while (1) {
    int X, Y, Z;
    int V[6];
    int E[53][2] = {0};
    for (int i = 0; i < 51; i++) {
      for (int j = 0; j < 5101; j++) {
        fie[i][j] = 0;
      }
    }
    fie[0][0] = 1;
    cin >> X >> Y >> Z;
    if (X == 0 && Y == 0 & Z == 0)
      break;
    for (int i = 0; i < X; i++)
      cin >> V[i];
    for (int i = 0; i < Z; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      E[a][0] = b, E[a][1] = c;
    }
    for (int i = 0; i < Y; i++) {
      for (int j = 0; j <= 5000; j++) {
        for (int k = 0; k < X; k++) {
          if (Y <= i + V[k])
            fie[Y][j] += fie[i][j] / X;
          else if (E[i + V[k]][0] == 0)
            fie[i + V[k]][j] += fie[i][j] / X;
          else if (E[i + V[k]][0] == 1)
            fie[i + V[k] + E[i + V[k]][1]][j] += fie[i][j] / X;
          else if (E[i + V[k]][0] == 2)
            fie[i + V[k]][j + E[i + V[k]][1]] += fie[i][j] / X;
          else if (E[i + V[k]][0] == 3)
            fie[i + V[k]][max(0, j - E[i + V[k]][1])] += fie[i][j] / X;
        }
      }
    }
    double ans = 0;
    for (int i = 0; i <= 5100; i++)
      ans += fie[Y][i] * i;
    printf("%.0f\\n", max(0.0, ans - 0.5));
  }
  return 0;
}
Problem p00232 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

#define Y 50
#define M 5000

#define max(a, b) (a > b ? a : b)
#define min(a, b) (a > b ? b : a)

struct Event {
  int e, v;
};

int main() {
  int x, y, z, r[4], n, next, money;
  double p[Y + 1][M + 1], ans;
  Event ev[Y + 1];
  while (1) {
    cin >> x >> y >> z;
    if (!x && !y && !z)
      break;
    for (int i = 0; i < x; ++i)
      cin >> r[i];
    for (int i = 0; i <= y; ++i) {
      for (int j = 0; j <= M; ++j)
        p[i][j] = 0.0;
      ev[i].e = ev[i].v = 0;
    }
    for (int i = 0; i < z; ++i) {
      cin >> n;
      cin >> ev[n].e >> ev[n].v;
    }
    p[0][0] = 1.0;
    ans = 0.0;
    for (int i = 0; i < y; ++i) {
      for (int j = 0; j <= M; ++j) {
        for (int k = 0; k < x; ++k) {
          next = min(i + r[k], y);
          money = j;
          if (ev[next].e == 1)
            next = min(next + ev[next].v, y);
          else if (ev[next].e == 2)
            money += ev[next].v;
          else if (ev[next].e == 3)
            money = max(money - ev[next].v, 0);
          p[next][money] += p[i][j] / (double)x;
        }
      }
    }
    for (int i = 0; i <= M; ++i)
      ans += p[y][i] * (double)i;
    cout << (int)ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#define Y 50
#define M 5001

#define max(a, b) (a > b ? a : b)
#define min(a, b) (a > b ? b : a)

struct Event {
  int e, v;
};

int main() {
  int x, y, z, r[4], n;
  double p[Y][M], one, ans;
  Event ev[Y];
  while (1) {
    cin >> x >> y >> z;
    if (!x && !y && !z)
      break;
    for (int i = 0; i < x; ++i)
      cin >> r[i];
    one = 1 / (double)x;
    for (int i = 0; i <= y; ++i) {
      for (int j = 0; j < M; ++j)
        p[i][j] = 0.0;
    }
    for (int i = 0; i < z; ++i) {
      cin >> n;
      cin >> ev[n].e >> ev[n].v;
    }
    p[0][0] = 1.0;
    ans = 0.0;
    for (int i = 0; i < y; ++i) {
      for (int j = 0; j < M; ++j) {
        if (ev[i].e == 1) {
          p[min(i + ev[i].v, y)][j] += p[i][j];
        } else {
          for (int k = 0; k < x; ++k) {
            if (ev[i].e == 2) {
              p[min(i + r[k], y)][j + ev[i].v] += one * p[i][j];
            } else if (ev[i].e == 3) {
              p[min(i + r[k], y)][max(j - ev[i].v, 0)] += one * p[i][j];
            } else {
              p[min(i + r[k], y)][j] += one * p[i][j];
            }
          }
        }
      }
    }
    for (int i = 0; i < M; ++i)
      ans += p[y][i] * i;
    cout << (int)ans << endl;
  }
  return 0;
}
Problem p00232 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int X, Y, Z;
vector<int> V;
vector<int> pl, gt;
vector<vector<double>> dp;

double calc(int v, int m) {
  if (v == Y)
    return (double)m;
  if (dp[v][m] > -1.)
    return dp[v][m];

  double sum = 0;
  for (int i = 0; i < V.size(); i++) {
    int tv = min(Y, v + V[i]);
    int tm = max(0, m + gt[tv]);
    tv = min(Y, tv + pl[tv]);

    sum += calc(tv, tm);
  }

  return dp[v][m] = sum / V.size();
}

int main() {
  while (scanf("%d %d %d", &X, &Y, &Z), X) {
    V.resize(X);
    pl = vector<int>(Y + 1, 0);
    gt = vector<int>(Y + 1, 0);
    dp = vector<vector<double>>(Y + 1, vector<double>(6000, -1.));

    for (int i = 0; i < X; i++) {
      scanf("%d", &V[i]);
    }

    for (int i = 0; i < Z; i++) {
      int N, E, A;
      scanf("%d %d %d", &N, &E, &A);
      if (E == 1)
        pl[N] = A;
      if (E == 2)
        gt[N] = A;
      if (E == 3)
        gt[N] = -A;
    }

    printf("%d\\n", (int)calc(0, 0));
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int X, Y, Z;
vector<int> V;
vector<int> pl, gt;
vector<vector<double>> dp;

double calc(int v, int m) {
  if (v == Y)
    return (double)m;
  if (dp[v][m] > -1.)
    return dp[v][m];

  double sum = 0;
  for (int i = 0; i < V.size(); i++) {
    int tv = min(Y, v + V[i]);
    int tm = max(0, m + gt[v]);
    tv += pl[tv];

    sum += calc(tv, tm);
  }

  return dp[v][m] = sum / V.size();
}

int main() {
  while (scanf("%d %d %d", &X, &Y, &Z), X) {
    V.resize(X);
    pl = vector<int>(Y + 1, 0);
    gt = vector<int>(Y + 1, 0);
    dp = vector<vector<double>>(Y + 1, vector<double>(6000, -1.));

    for (int i = 0; i < X; i++) {
      scanf("%d", &V[i]);
    }

    for (int i = 0; i < Z; i++) {
      int N, E, A;
      scanf("%d %d %d", &N, &E, &A);
      if (E == 1)
        pl[N] = A;
      if (E == 2)
        gt[N] = A;
      if (E == 3)
        gt[N] = -A;
    }

    printf("%d\\n", (int)calc(0, 0));
  }
}
Problem p00232 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

double dp[51][5001];
int event[51][2];
int step[4];
int X, Y, Z;

int main(void) {
  while (cin >> X >> Y >> Z, X | Y | Z) {
    // initialize
    for (int i = 0; i < 4; i++)
      step[i] = 0;
    for (int i = 0; i < 51; i++)
      for (int j = 0; j < 5001; j++)
        dp[i][j] = 0.0;
    dp[0][0] = 1.0;
    for (int i = 0; i < 51; i++)
      event[i][0] = event[i][1] = 0;
    //	reading
    for (int i = 0; i < X; i++)
      cin >> step[i];
    for (int i = 0; i < Z; i++) {
      int tmp;
      cin >> tmp;
      cin >> event[tmp][0] >> event[tmp][1];
    }
    for (int i = 0; i < Y; i++) {
      for (int j = 0; j <= 5000; j++) {
        for (int k = 0; k < X; k++) {
          int root = i + step[k];
          if (root >= Y)
            dp[Y][j] += dp[i][j] / X;
          else if (event[root][0] == 1) {
            if (root + event[root][1] > Y)
              dp[Y][j] += dp[i][j] / X;
            else
              dp[root + event[root][1]][j] += dp[i][j] / X;
          } else if (event[root][0] == 2)
            dp[root][j + event[root][1]] += dp[i][j] / X;
          else if (event[root][0] == 3) {
            if (event[root][1] < j)
              dp[root][j - event[root][1]] += dp[i][j] / X;
            else
              dp[root][0] += dp[i][j] / X;
          } else
            dp[root][j] += dp[i][j] / X;
        }
      }
    }
    double exp = 0.0;
    for (int i = 0; i <= 5000; i++)
      exp += dp[Y][i] * i;
    cout << (int)exp << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

double dp[51][5001];
int event[51][2];
int step[4];
int X, Y, Z;

int main(void) {
  while (cin >> X >> Y >> Z, X | Y | Z) {
    // initialize
    for (int i = 0; i < 4; i++)
      step[i] = 0;
    for (int i = 0; i < 51; i++)
      for (int j = 0; j < 5001; j++)
        dp[i][j] = 0.0;
    dp[0][0] = 1.0;
    for (int i = 0; i < 51; i++)
      event[i][0] = event[i][1] = 0;
    //	reading
    for (int i = 0; i < X; i++)
      cin >> step[i];
    for (int i = 0; i < Z; i++) {
      int tmp;
      cin >> tmp;
      cin >> event[tmp][0] >> event[tmp][1];
    }
    for (int i = 0; i < Y; i++) {
      for (int j = 0; j <= 5000; j++) {
        for (int k = 0; k < X; k++) {
          int root = i + step[k];
          if (root > Y)
            dp[Y][j] += dp[i][j] / X;
          else if (event[root][0] == 1)
            dp[root + event[root][1]][j] += dp[i][j] / X;
          else if (event[root][0] == 2)
            dp[root][j + event[root][1]] += dp[i][j] / X;
          else if (event[root][0] == 3) {
            if (event[root][1] < j)
              dp[root][j - event[root][1]] += dp[i][j] / X;
            else
              dp[root][0] += dp[i][j] / X;
          } else
            dp[root][j] += dp[i][j] / X;
        }
      }
    }
    double exp = 0.0;
    for (int i = 0; i <= 5000; i++)
      exp += dp[Y][i] * i;
    cout << (int)exp << endl;
  }
  return 0;
}
Problem p00232 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

struct Cell {
  int e;
  int a;
};

double solve(const vector<Cell> &cel, int pos, const vector<int> &vs, int money,
             vector<vector<double>> &memo) {
  double retval = 0;
  int vl = vs.size();
  int cl = cel.size();
  if (pos >= cl)
    return money;
  if (memo[pos][money] >= 0)
    return memo[pos][money];

  for (int i = 0; i < vl; ++i) {
    int v = vs[i];
    if (pos + v >= cl) {
      retval += money;
      continue;
    }
    Cell cur = cel[pos + v];
    if (cur.e == 0) {
      retval += solve(cel, pos + v, vs, money, memo);
    } else if (cur.e == 1) {
      retval += solve(cel, pos + v + cur.a, vs, money, memo);
    } else if (cur.e == 2) {
      retval += solve(cel, pos + v, vs, money + cur.a, memo);
    } else if (cur.e == 3) {
      retval +=
          solve(cel, pos + v, vs, (money > cur.a) ? money - cur.a : 0, memo);
    }
  }
  return memo[pos][money] = retval / vl;
}

int main() {
  while (1) {
    int x, y, z;
    int i;
    cin >> x >> y >> z;
    if (x == 0 && y == 0 && z == 0)
      break;
    vector<int> vs(x);
    vector<Cell> cells(y);
    for (i = 0; i < x; ++i) {
      cin >> vs[i];
    }
    for (i = 0; i < z; ++i) {
      int n;
      cin >> n;
      cin >> cells[n].e;
      cin >> cells[n].a;
    }
    vector<vector<double>> memo(51, vector<double>(5001, -1));
    cout << (int)solve(cells, 0, vs, 0, memo) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

using namespace std;

struct Cell {
  int e;
  int a;
};

double solve(const vector<Cell> &cel, int pos, const vector<int> &vs, int money,
             vector<vector<double>> &memo) {
  if (memo[pos][money] >= 0)
    return memo[pos][money];
  double retval = 0;
  int vl = vs.size();
  int cl = cel.size();
  if (pos >= cl)
    return money;
  for (int i = 0; i < vl; ++i) {
    int v = vs[i];
    if (pos + v >= cl) {
      retval += money;
      continue;
    }
    Cell cur = cel[pos + v];
    if (cur.e == 0) {
      retval += solve(cel, pos + v, vs, money, memo);
    } else if (cur.e == 1) {
      retval += solve(cel, pos + v + cur.a, vs, money, memo);
    } else if (cur.e == 2) {
      retval += solve(cel, pos + v, vs, money + cur.a, memo);
    } else if (cur.e == 3) {
      retval +=
          solve(cel, pos + v, vs, (money > cur.a) ? money - cur.a : 0, memo);
    }
  }
  return memo[pos][money] = retval / vl;
}

int main() {
  while (1) {
    int x, y, z;
    int i;
    cin >> x >> y >> z;
    if (x == 0 && y == 0 && z == 0)
      break;
    vector<int> vs(x);
    vector<Cell> cells(y);
    for (i = 0; i < x; ++i) {
      cin >> vs[i];
    }
    for (i = 0; i < z; ++i) {
      int n;
      cin >> n;
      cin >> cells[n].e;
      cin >> cells[n].a;
    }
    vector<vector<double>> memo(51, vector<double>(5001, -1));
    cout << (int)solve(cells, 0, vs, 0, memo) << endl;
  }
  return 0;
}