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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;

int n, m;
double l;
double p[100], t[100], v[100];
double cb[100][51];

void init() {
  rep(u, n) {
    rep(i, m + 1) {
      int j = m - i;
      cb[u][i] = 1.0;
      int cnt1 = i, cnt2 = j;
      for (int k = 1; k <= i; k++) {
        cb[u][i] = cb[u][i] * (m - k + 1) / k;
        while (cb[u][i] > 1.0) {
          if (cnt1 > 0) {
            cb[u][i] *= p[u];
            cnt1--;
          } else if (cnt2 > 0) {
            cb[u][i] *= (1 - p[u]);
            cnt2--;
          }
          if (cnt1 == 0 && cnt2 == 0)
            break;
        }
      }
      rep(k, cnt1) cb[u][i] *= p[u];
      rep(k, cnt2) cb[u][i] *= (1 - p[u]);
    }
  }
}

int main() {
  cin >> n >> m >> l;
  rep(i, n) {
    cin >> p[i] >> t[i] >> v[i];
    p[i] *= 0.01;
  }
  init();
  rep(i, n) {
    double pos;
    // double ti;
    double ans = 0.0;
    if (v[i] == 0.0) {
      puts("0.00000000000");
      continue;
    }
    for (int j = 0; j <= m; j++) {
      pos = cb[i][j];
      rep(k, n) {
        if (k == i)
          continue;
        if (v[k] == 0.0)
          continue;
        if (t[k] == 0.0) {
          if ((int)l * (int)v[k] - (int)l * (int)v[i] +
                  (int)t[i] * j * (int)v[i] * (int)v[k] >=
              0)
            pos *= 0.0;
          else
            pos *= 1.0;
          continue;
        }
        int c;
        if ((int)l * (int)v[k] - (int)l * (int)v[i] +
                (int)t[i] * j * (int)v[i] * (int)v[k] <
            0)
          c = 0;
        else
          c = ((int)l * (int)v[k] - (int)l * (int)v[i] +
               (int)t[i] * j * (int)v[i] * (int)v[k]) /
                  ((int)t[k] * (int)v[i] * (int)v[k]) +
              1;
        double tmp = 0.0;
        for (int l = c; l <= m; l++) {
          tmp += cb[k][l];
        }
        pos *= tmp;
      }
      ans += pos;
    }
    printf("%.9f\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;

int n, m;
double l;
double p[100], t[100], v[100];
double cb[100][51];

void init() {
  rep(u, n) {
    rep(i, m + 1) {
      int j = m - i;
      cb[u][i] = 1.0;
      int cnt1 = i, cnt2 = j;
      for (int k = 1; k <= i; k++) {
        cb[u][i] = cb[u][i] * (m - k + 1) / k;
        while (cb[u][i] > 1.0) {
          if (cnt1 > 0) {
            cb[u][i] *= p[u];
            cnt1--;
          } else if (cnt2 > 0) {
            cb[u][i] *= (1 - p[u]);
            cnt2--;
          }
        }
      }
      rep(k, cnt1) cb[u][i] *= p[u];
      rep(k, cnt2) cb[u][i] *= (1 - p[u]);
    }
  }
}

int main() {
  cin >> n >> m >> l;
  rep(i, n) {
    cin >> p[i] >> t[i] >> v[i];
    p[i] *= 0.01;
  }
  init();
  rep(i, n) {
    double pos;
    // double ti;
    double ans = 0.0;
    if (v[i] == 0.0) {
      puts("0.00000000000");
      continue;
    }
    for (int j = 0; j <= m; j++) {
      pos = cb[i][j];
      rep(k, n) {
        if (k == i)
          continue;
        if (v[k] == 0.0)
          continue;
        if (t[k] == 0.0) {
          if ((int)l * (int)v[k] - (int)l * (int)v[i] +
                  (int)t[i] * j * (int)v[i] * (int)v[k] >
              0)
            pos *= 0.0;
          else
            pos *= 1.0;
          continue;
        }
        int c;
        if ((int)l * (int)v[k] - (int)l * (int)v[i] +
                (int)t[i] * j * (int)v[i] * (int)v[k] <
            0)
          c = 0;
        else
          c = ((int)l * (int)v[k] - (int)l * (int)v[i] +
               (int)t[i] * j * (int)v[i] * (int)v[k]) /
                  ((int)t[k] * (int)v[i] * (int)v[k]) +
              1;
        double tmp = 0.0;
        for (int l = c; l <= m; l++) {
          tmp += cb[k][l];
        }
        pos *= tmp;
      }
      ans += pos;
    }
    printf("%.9f\\n", ans);
  }
}