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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
#define double long double
#define two (double)2.0 / (double)3.0
#define one (double)1.0 / (double)3.0
#define EPS (double)0.000000001
double p[2001][2001] = {};

int n, m;
double point[] = {5.0000000, 3.0000000, 2.000000};
double g[2001][3] = {};
double dfs(int sss, int k, vector<int> &s, vector<vector<int>> &v) {
  if (k == 2) {
    s[k] = sss;
    return g[s[0]][0] + g[s[1]][1] + g[s[2]][2];
  } else {
    double mx = -1000.0;
    for (int i = 0; i <= sss; i++) {
      s[k] = i;
      mx = max(mx, dfs(sss - i, k + 1, s, v));
    }
    return mx;
  }
}

int main() {
  cin >> n >> m;
  vector<vector<int>> v(n, vector<int>(3));
  for (int i = 0; i < n; i++) {
    cin >> v[i][0] >> v[i][1] >> v[i][2];
  }
  p[0][0] = 1.0;
  for (int i = 1; i <= m; i++) {
    p[i][0] = p[i - 1][0] * two;
    p[i][i] = p[i - 1][i - 1] * one;
  }
  for (int i = 2; i <= m; i++) {
    for (int j = 1; j < i; j++) {
      p[i][j] = p[i - 1][j] * two + p[i - 1][j - 1] * one;
    }
  }

  for (int i = m - 1; i >= 0; i--) {
    p[m][i] += p[m][i + 1];
  }
  for (int sq = 0; sq <= m; sq++) {
    for (int q = 0; q < 3; q++) {
      double ans = 0.0;
      int x = v[0][q] * sq;
      vector<double> pr(n);
      for (int i = 1; i < n; i++) {
        int y = (x - 1) / v[i][q] + 1;
        if (x == 0)
          y = 0;
        if (y <= m)
          pr[i] = p[m][y];
        else
          pr[i] = 0.0;
      }
      double p1 = 1.0;
      for (int i = 1; i < n; i++) {
        p1 *= pr[i];
      }
      ans += -1.0 * p1;
      vector<vector<double>> vd(n, vector<double>(3));
      vd[0][0] = 1.0;
      for (int i = 1; i < n; i++) {
        for (int j = 0; j < 3; j++) {
          if (j != 0)
            vd[i][j] =
                vd[i - 1][j] * (1.0 - pr[i]) + vd[i - 1][j - 1] * (pr[i]);
          else
            vd[i][j] = vd[i - 1][j] * (1.0 - pr[i]);
        }
      }
      ans += point[q] * (vd[n - 1][0] + vd[n - 1][1] + vd[n - 1][2]);
      g[sq][q] = ans;
    }
  }

  vector<int> s(3);
  double ans = dfs(m, 0, s, v);
  cout << fixed << setprecision(12) << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
#define double long double
#define two (double)2.0 / (double)3.0
#define one (double)1.0 / (double)3.0
#define EPS (double)0.000000001
double p[2001][2001] = {};
int n, m;
double point[] = {5.0000000, 3.0000000, 2.000000};
double dfs(int sss, int k, vector<int> &s, vector<vector<int>> &v) {
  if (k == 2) {
    s[k] = sss;
    double ans = 0.0;
    for (int q = 0; q < 3; q++) {
      int x = v[0][q] * s[q];
      vector<double> pr(n);
      for (int i = 1; i < n; i++) {
        int y = (x - 1) / v[i][q] + 1;
        if (x == 0)
          y = 0;
        if (y <= m)
          pr[i] = p[m][y];
        else
          pr[i] = 0.0;
      }
      double p1 = 1.0;
      for (int i = 1; i < n; i++) {
        p1 *= pr[i];
      }
      ans += -1.0 * p1;
      double p2 = 1.0;
      int cnt = 0;
      for (int i = 1; i < n; i++) {
        if ((double)1.0 - pr[i] <= EPS) {
          cnt++;
        } else {
          p2 *= (double)1.0 - pr[i];
        }
      }
      if (cnt == 2) {
        ans += point[q] * p2;
      } else if (cnt == 1) {
        ans += point[q] * p2;
        for (int i = 1; i < n; i++) {
          if ((double)1.0 - pr[i] <= EPS)
            continue;
          ans += point[q] * p2 * pr[i] / (1.0 - pr[i]);
        }
      } else if (cnt == 0) {
        ans += point[q] * p2;
        for (int i = 1; i < n; i++) {
          if ((double)1.0 - pr[i] <= EPS)
            continue;
          ans += point[q] * p2 * pr[i] / (1.0 - pr[i]);
        }
        for (int i = 1; i < n; i++) {
          if ((double)1.0 - pr[i] <= EPS)
            continue;
          for (int j = i + 1; j < n; j++) {
            if ((double)1.0 - pr[j] <= EPS)
              continue;
            ans +=
                point[q] * p2 * pr[i] / (1.0 - pr[i]) * pr[j] / (1.0 - pr[j]);
          }
        }
      }
    }
    return ans;
  } else {
    double mx = -1000.0;
    for (int i = 0; i <= sss; i++) {
      s[k] = i;
      mx = max(mx, dfs(sss - i, k + 1, s, v));
    }
    return mx;
  }
}

int main() {
  cin >> n >> m;
  vector<vector<int>> v(n, vector<int>(3));
  for (int i = 0; i < n; i++) {
    cin >> v[i][0] >> v[i][1] >> v[i][2];
  }
  p[0][0] = 1.0;
  for (int i = 1; i <= m; i++) {
    p[i][0] = p[i - 1][0] * two;
    p[i][i] = p[i - 1][i - 1] * one;
  }
  for (int i = 2; i <= m; i++) {
    for (int j = 1; j < i; j++) {
      p[i][j] = p[i - 1][j] * two + p[i - 1][j - 1] * one;
    }
  }

  for (int i = m - 1; i >= 0; i--) {
    p[m][i] += p[m][i + 1];
  }
  vector<int> s(3);
  double ans = dfs(m, 0, s, v);
  cout << fixed << setprecision(12) << ans << endl;
  return 0;
}