alexjercan/bugnet huggingface dataset:
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
int m, n;
vector<vector<ld>> rate;
ld dp[17][(1 << 16) + 1];

ld solve(int room, int rest) {
  if (room == n)
    return 1.0;
  if (rest == (1 << m) - 1)
    return 0;
  if (dp[room][rest] >= 0)
    return dp[room][rest];
  ld res = 0;
  REP(i, m) {
    if ((rest >> i) & 1)
      continue;
    ld exp = 1.0;
    ld tres = 0;
    FOR(j, room, n) {
      ld win = rate[i][j];
      tres += exp * (1.0 - win) * solve(j, rest | (1 << i));
      exp *= win;
    }
    tres += exp;
    chmax(res, tres);
  }
  return dp[room][rest] = res;
}

int main() {
  cin.sync_with_stdio(false);
  REP(i, 17) REP(j, (1 << 16) + 1) dp[i][j] = -1;
  cin >> m >> n;
  rate.resize(m, vector<ld>(n));
  REP(i, m) REP(j, n) cin >> rate[i][j];
  cout << D10 << solve(0, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
int m, n;
vector<vector<ld>> rate;
ld dp[17][(1 << 16) + 1];

ld solve(int room, int rest) {
  if (room == n)
    return 1.0;
  if (rest == (1 << m) - 1)
    return 0;
  if (dp[room][rest] >= 0)
    return dp[room][rest];
  ld res = 0;
  REP(i, m) {
    if ((rest >> i) & 1)
      continue;
    ld exp = 1.0;
    ld tres = 0;
    FOR(j, room, n) {
      ld win = rate[i][j];
      tres += exp * (1.0 - win) * solve(j, rest | (1 << i));
      exp *= win;
    }
    tres += exp;
    chmax(res, tres);
  }
  return dp[room][rest] = res;
}

int main() {
  cin.sync_with_stdio(false);
  REP(i, 17) REP(j, (1 << 16) + 1) dp[i][j] = -1;
  cin >> m >> n;
  rate.resize(m, vector<ld>(n));
  REP(i, m) REP(j, n) cin >> rate[i][j];
  cout << D10 << solve(0, 0) << endl;
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>

using namespace std;

int m, n;
double prob[16][16];
double dp[16][1 << 16];

double search(int pos, int cat) {
  if (dp[pos][cat] > -0.5)
    return dp[pos][cat];
  double &res = dp[pos][cat];
  res = 0.0;
  for (int i = 0; i < m; i++) {
    if (cat & (1 << i))
      continue;
    double pSum = 0.0;
    double pCur = 1.0;
    for (int j = pos; j < n; j++) {
      pSum += (1 - prob[i][j]) * pCur * search(j, cat | (1 << i));
      pCur *= prob[i][j];
    }
    pSum += pCur;
    res = max(res, pSum);
  }
  return res;
}

int main() {
  while (cin >> m >> n) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++)
        cin >> prob[i][j];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << m); j++)
        dp[i][j] = -1.0;
    }
    printf("%.10lf\\n", search(0, 0));
  }
}

Error: WA

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

int m, n;
double prob[16][16];
double dp[16][1 << 16];

double search(int pos, int cat) {
  if (dp[pos][cat] > -0.5)
    return dp[pos][cat];
  double &res = dp[pos][cat];
  res = 0.0;
  for (int i = 0; i < m; i++) {
    if (cat & (1 << i))
      continue;
    double pSum = 0.0;
    double pCur = 1.0;
    for (int j = pos; j < n; j++) {
      pSum += (1 - prob[i][j]) * pCur * search(j, cat | (1 << i));
      pCur *= prob[i][j];
    }
    pSum += pCur;
    res = max(res, pSum);
  }
  return res;
}

int main() {
  while (cin >> m >> n) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++)
        cin >> prob[i][j];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < (1 << m); j++)
        dp[i][j] = -1.0;
    }
    printf("%.10lf\\n", search(0, 0));
  }
}
Problem p01354 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int m, n;
double t[16][16];
double dp[16][(1 << 16)];
double Q[17][16];

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      cin >> t[i][j];

  for (int S = (1 << m) - 1; S >= 0; S--) {
    for (int i = 0; i < m; i++)
      Q[n][i] = 1.0;
    for (int x = n - 1; x >= 0; x--) {
      for (int i = 0; i < m; i++) {
        if (S >> i & 1)
          continue;
        dp[x][S] = max(dp[x][S], Q[x + 1][i] * t[i][x] +
                                     (1.0 - t[i][x]) * dp[x][S | (1 << i)]);
        Q[x][i] = dp[x][S | (1 << i)] * (1.0 - t[i][x]) + Q[x + 1][i] * t[i][x];
      }
    }
  }
  printf("%.12f\\n", dp[0][0]);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int m, n;
double t[16][16];
double dp[16][(1 << 16)];
double Q[16][16];

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> t[i][j];
    }
  }

  for (int S = (1 << m) - 1; S >= 0; S--) {
    dp[n][S] = 1.0;

    for (int i = 0; i < m; i++)
      Q[n][i] = 1.0;

    for (int x = n - 1; x >= 0; x--) {
      for (int i = 0; i < m; i++) {
        if (S >> i & 1)
          continue;
        dp[x][S] = max(dp[x][S], Q[x + 1][i] * t[i][x] +
                                     (1.0 - t[i][x]) * dp[x][S | (1 << i)]);
        Q[x][i] = dp[x][S | (1 << i)] * (1.0 - t[i][x]) + Q[x + 1][i] * t[i][x];
      }
    }
  }
  printf("%.12f\\n", dp[0][0]);
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <stdio.h>

using namespace std;

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

double p[17][17];
double dp[17][1 << 16];
int n, m;

double DFS(int idx, int state) {
  if (idx == n)
    return 1.0;
  if (dp[idx][state] >= -0.1)
    return dp[idx][state];
  double ret = -0.1;
  REP(i, m) {
    int mask = (1 << i);
    if (mask & state)
      continue;
    double ret1 = 0.0;
    double tmp = 1.0;
    FOR(j, idx, n) {
      ret1 += tmp * (1 - p[i][j]) * DFS(j, (state | mask));
      tmp *= p[i][j];
    }
    ret1 += tmp;
    ret = max(ret, ret1);
  }
  return dp[idx][state] = max(ret, 0.0);
}

int main() {
  scanf("%d%d", &m, &n);
  REP(i, m) REP(j, n) scanf("%lf", &p[i][j]);
  REP(i, n) REP(j, (1 << m)) dp[i][j] = -1.0;
  printf("%.10f\\n", DFS(0, 0));
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <stdio.h>

using namespace std;

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

double p[22][22];
double dp[17][17][1 << 16];
int n, m;

double DFS(int idx, int state, int cidx) {
  if (idx == n)
    return 1.0;
  if (dp[idx][cidx][state] >= -0.1)
    return dp[idx][cidx][state];
  if (cidx != 16)
    return dp[idx][cidx][state] =
               p[cidx][idx] * DFS(idx + 1, state, cidx) +
               (1 - p[cidx][idx]) * DFS(idx, (state | (1 << cidx)), 16);
  double ret = -0.1;
  REP(i, m) {
    int mask = (1 << i);
    if (mask & state)
      continue;
    ret = max(ret, p[i][idx] * DFS(idx + 1, state, i) +
                       (1 - p[i][idx]) * DFS(idx, (state | mask), 16));
  }
  return dp[idx][cidx][state] = max(ret, 0.0);
}

int main() {
  scanf("%d%d", &m, &n);
  REP(i, m) REP(j, n) scanf("%lf", &p[i][j]);
  REP(i, n) REP(j, 17) REP(k, (1 << m)) dp[i][j][k] = -1.0;
  printf("%.10f\\n", DFS(0, 0, 16));
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int m, n;
double dp[17][(1 << 16) + 1];
double prob[17][17];

double calc(int f, int s) {

  if (f == n)
    return 1.0;
  if (dp[f][s] != -1.0)
    return dp[f][s];

  double ret = 0;
  for (int k = 0; k < m; ++k)
    if (!(s & (1 << k))) { // ???????????????????????????
      double cp = 1.0;
      double sum = 0;
      for (int j = f; j < n && cp > 0.0; ++j) { // ?????????????????¢
        if (prob[k][j] < 1.0) {
          sum += calc(j, s | (1 << k)) * cp * (1.0 - prob[k][j]);
        }
        cp *= prob[k][j];
      }
      // ???????????§???????¶????????¢????(????????§?¢????0????????£?????????????????§??§??????
      sum += cp;
      ret = max(ret, sum);
    }
  // printf("f:%d\\ts:%d\\tp:%f\\tret:%f\\n",f,s,p,ret);
  return dp[f][s] = ret;
}

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> prob[i][j];
    }
  }
  // init
  for (int i = 0; i < 17; ++i)
    for (int j = 0; j < (1 << 16) + 1; ++j)
      dp[i][j] = -1.0;

  printf("%.12f\\n", calc(0, 0));

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

int m, n;
double dp[17][(1 << 16) + 1];
double prob[17][17];

double calc(int f, int s) {

  if (f == n)
    return 1.0;
  if (dp[f][s])
    return dp[f][s];

  double ret = 0;
  for (int k = 0; k < m; ++k)
    if (!(s & (1 << k))) { // ???????????????????????????
      double cp = 1.0;
      double sum = 0;
      for (int j = f; j < n && cp > 0.0; ++j) { // ?????????????????¢
        sum += calc(j, s | (1 << k)) * cp * (1.0 - prob[k][j]);
        cp *= prob[k][j];
      }
      // ???????????§???????¶????????¢????(????????§?¢????0????????£?????????????????§??§??????
      sum += cp;
      ret = max(ret, sum);
    }
  // printf("f:%d\\ts:%d\\tp:%f\\tret:%f\\n",f,s,p,ret);
  return dp[f][s] = ret;
}

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> prob[i][j];
    }
  }

  printf("%.12f\\n", calc(0, 0));

  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef double ld;

// ld dp[17][1 << 16] = {0}; // dp[next enemy][used cat]

int N, M;
ld P[16][16];

bool m1[16][1 << 16] = {0};
ld m2[16][1 << 16];

ld dp(int n, int rest) {
  if (n >= N)
    return (ld)1;

  if (m1[n][rest])
    return m2[n][rest];

  ld ret = 0;
  for (int i = 0; i < M; ++i) {
    ld p = 0;
    if (rest & (1 << i)) {
      int nr = rest & ~(1 << i);

      ld per = 1;
      for (int j = n; j < N; ++j) {
        p += per * (1 - P[i][j]) * dp(j, nr);
        per *= P[i][j];
      }
      p += per;
    }
    ret = max(ret, p);
  }
  return m1[n][rest] = true, m2[n][rest] = ret;
}
int main() {
  cin >> M >> N;
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> P[i][j];
    }
  }

  printf("%.12f\\n", (double)dp(0, (1 << M) - 1));
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;
typedef long long ll;

// ld dp[17][1 << 16] = {0}; // dp[next enemy][used cat]

int N, M;
ld P[16][16];

bool m1[16][1 << 16] = {0};
ld m2[16][1 << 16];

ld dp(int n, int rest) {
  if (n >= N)
    return (ld)1;

  if (m1[n][rest])
    return m2[n][rest];

  ld ret = 0;
  for (int i = 0; i < M; ++i) {
    ld p = 0;
    if (rest & (1 << i)) {
      int nr = rest & ~(1 << i);

      ld per = 1;
      for (int j = n; j < N; ++j) {
        p += per * (1 - P[i][j]) * dp(j, nr);
        per *= P[i][j];
      }
      p += per;
    }
    ret = max(ret, p);
  }
  return m1[n][rest] = true, m2[n][rest] = ret;
}
int main() {
  cin >> M >> N;
  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> P[i][j];
    }
  }

  printf("%.12f\\n", (double)dp(0, (1 << M) - 1));
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;
using ld = double;
template <typename T> vector<T> Vec(int n, T v) { return vector<T>(n, v); }
template <class... Args> auto Vec(int n, Args... args) {
  auto val = Vec(args...);
  return vector<decltype(val)>(n, move(val));
}

int main() {
  int M, N;
  cin >> M >> N;
  auto p = Vec(M, N, static_cast<ld>(0.0));
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      cin >> p[i][j];
    }
  }
  const int maximum = 1 << M;
  auto dp = Vec(maximum, M, static_cast<ld>(0.0)); // 残りネコ集合=i,戦闘ネコ=k,敵k-1まで倒した
                                                   // の状態から攻略する確率
  for (int i = 0; i < maximum; i++) {
    for (int j = 0; j < M; j++) {
      dp[i][j] = (i & (1 << j)) ? 1 : 0;
    }
  }
  auto sub = vector<ld>(maximum, static_cast<ld>(0.0));
  for (int k = N - 1; k >= 0; k--) {
    fill(sub.begin(), sub.end(), static_cast<ld>(0.0));
    auto tmp = Vec(maximum, M, static_cast<ld>(0.0));
    for (int i = 0; i < maximum; i++) {
      for (int j = 0; j < M; j++) {
        if (i & (1 << j)) {
          tmp[i][j] = p[j][k] * dp[i][j] + (1 - p[j][k]) * sub[i - (1 << j)];
          sub[i] = max(sub[i], tmp[i][j]);
        }
      }
    }
    dp = tmp;
  }
  cout << fixed << setprecision(15) << sub[maximum - 1] << endl;

  return 0;
}


Error: MLE

Fixed submission:

#include <iomanip>
#include <iostream>
#include <vector>

using namespace std;
using ld = double;
template <typename T> vector<T> Vec(int n, T v) { return vector<T>(n, v); }
template <class... Args> auto Vec(int n, Args... args) {
  auto val = Vec(args...);
  return vector<decltype(val)>(n, move(val));
}

int main() {
  int M, N;
  cin >> M >> N;
  auto p = Vec(M, N, static_cast<ld>(0.0));
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      cin >> p[i][j];
    }
  }
  const int maximum = 1 << M;
  auto dp =
      Vec(maximum, M, N + 1, static_cast<ld>(0.0)); // 残りネコ集合=i,戦闘ネコ=k,敵k-1まで倒した
                                                    // の状態から攻略する確率
  auto sub = Vec(maximum, N + 1,
                 static_cast<ld>(0.0)); // 残りネコ集合=i,敵j-1まで倒した
                                        // の状態から攻略する確率の最大値
  for (int i = 0; i < maximum; i++) {
    for (int j = 0; j < M; j++) {
      dp[i][j][N] = (i & (1 << j)) ? 1 : 0;
    }
  }

  for (int k = N - 1; k >= 0; k--) {
    for (int i = 0; i < maximum; i++) {
      for (int j = 0; j < M; j++) {
        if (i & (1 << j)) {
          dp[i][j][k] =
              p[j][k] * dp[i][j][k + 1] + (1 - p[j][k]) * sub[i - (1 << j)][k];
          sub[i][k] = max(sub[i][k], dp[i][j][k]);
        }
      }
    }
  }
  cout << fixed << setprecision(15) << sub[maximum - 1][0] << endl;

  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define N 16
using namespace std;
int m, n;
double neko[N][N];
double dfs(int pos, int k, int bit);
double calc(int pos, int bit) {
  static int used[N][1 << N] = {};
  static double mem[N][1 << N];
  if (used[pos][bit]++)
    return mem[pos][bit];
  double mx = 0;
  for (int i = 0; i < m; i++)
    if (((bit >> i) & 1) == 0)
      mx = max(mx, dfs(pos, i, bit));
  return mem[pos][bit] = mx;
}

double dfs(int pos, int k, int bit) {
  if (pos == n)
    return 1;
  double p = neko[k][pos];
  return p * dfs(pos + 1, k, bit) + (1 - p) * calc(pos, bit | (1 << k));
}

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      cin >> neko[i][j];

  double ans = 0;
  for (int i = 0; i < m; i++)
    ans = max(ans, dfs(0, i, 0));
  printf("%.10f\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define N 16
using namespace std;
int m, n;
double neko[N][N];
double dfs(int pos, int k, int bit);
double calc(int pos, int bit) {
  static int used[N][1 << N] = {};
  static double mem[N][1 << N];
  if (used[pos][bit]++)
    return mem[pos][bit];
  double mx = 0;
  for (int i = 0; i < m; i++)
    if (((bit >> i) & 1) == 0)
      mx = max(mx, dfs(pos, i, bit));
  return mem[pos][bit] = mx;
}

map<int, double> mem[N][1 << N];
double dfs(int pos, int k, int bit) {
  if (pos == n)
    return 1;
  if (mem[pos][bit].count(k))
    return mem[pos][bit][k];
  double p = neko[k][pos];
  return mem[pos][bit][k] =
             p * dfs(pos + 1, k, bit) + (1 - p) * calc(pos, bit | (1 << k));
}

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++)
      cin >> neko[i][j];

  double ans = 0;
  for (int i = 0; i < m; i++)
    ans = max(ans, dfs(0, i, 0));
  printf("%.10f\\n", ans);
  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define each(i, n) for (auto i : n)
#define all(n) n.begin(), n.end()
#define clr(n) memset(n, 0, sizeof(n))
#define mclr(n) memset(n, -1, sizeof(n))

// dp[bit][i][j]
// ?´???£??????????????????bit???i???????????¨?±????j?????????????????????
//??¨??¨????????????????¢????
double dp[1 << 16][16];

// ap[i][j][k] ???i ?????¨?±?j????????¨?±?k?????????????¢????
double ap[16][16][17];
double a[16][17];

int main() {
  int n, m;
  cin >> m >> n;
  mclr(dp);
  rep(i, m) {
    rep(j, n) { cin >> a[i][j]; }
    a[i][n] = 0.0;
  }
  rep(i, m) {
    rep(j, n) {
      rep(k, n + 1) {
        ap[i][j][k] = 1.0;
        for (int l = j; l < k; l++) {
          ap[i][j][k] *= a[i][l];
        }
        ap[i][j][k] *= (1.0 - a[i][k]);
      }
    }
  }
  function<double(int, int)> solve = [&](int bit, int i) {
    if (i == n)
      return 1.0;
    if (dp[bit][i] >= -0.5) {
      return dp[bit][i];
    }
    double re = 0.0;
    rep(k, m) if (!(bit & (1 << k))) {
      double p = 0.0;
      for (int l = i; l <= n; l++) {
        p += solve(bit | (1 << k), l) * ap[k][i][l];
      }
      re = max(re, p);
    }
    return dp[bit][i] = re;
  };
  cout << fixed << setprecision(30) << solve(0, 0) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define each(i, n) for (auto i : n)
#define all(n) n.begin(), n.end()
#define clr(n) memset(n, 0, sizeof(n))
#define mclr(n) memset(n, -1, sizeof(n))

// dp[bit][i][j]
// ?´???£??????????????????bit???i???????????¨?±????j?????????????????????
//??¨??¨????????????????¢????
double dp[1 << 16][16];

// ap[i][j][k] ???i ?????¨?±?j????????¨?±?k?????????????¢????
double ap[16][16][17];
double a[16][17];

int main() {
  int n, m;
  cin >> m >> n;
  clr(dp);
  rep(i, m) {
    rep(j, n) { cin >> a[i][j]; }
    a[i][n] = 0.0;
  }
  rep(i, m) {
    rep(j, n) {
      rep(k, n + 1) {
        ap[i][j][k] = 1.0;
        for (int l = j; l < k; l++) {
          ap[i][j][k] *= a[i][l];
        }
        ap[i][j][k] *= (1.0 - a[i][k]);
      }
    }
  }
  function<double(int, int)> solve = [&](int bit, int i) {
    if (i == n)
      return 1.0;
    if (dp[bit][i]) {
      return dp[bit][i];
    }
    double re = 0.0;
    rep(k, m) if (!(bit & (1 << k))) {
      double p = 0.0;
      for (int l = i; l <= n; l++) {
        p += solve(bit | (1 << k), l) * ap[k][i][l];
      }
      re = max(re, p);
    }
    return dp[bit][i] = re;
  };
  cout << fixed << setprecision(30) << solve(0, 0) << endl;

  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;

int m, n;
double mat[18][18];
bool visited[(1 << 16)][16];
double memo[(1 << 16)][16];

double dfs(int used, int stage) {
  if (stage == n)
    return 1;
  if (used == (1 << m) - 1)
    return 0;
  if (visited[used][stage])
    return memo[used][stage];
  double ret = 0;
  for (int i = 0; i < m; i++) {
    if ((used >> i) & 1)
      continue;
    double path = 1;
    double suc = 0;
    int nused = used | (1 << i);
    for (int j = stage; j < n; j++) {
      suc += path * (1 - mat[i][j]) * dfs(nused, j);
      path *= mat[i][j];
    }
    suc += path;
    ret = max(ret, suc);
  }
  visited[used][stage] = true;
  memo[used][stage] = ret;
  return ret;
}

int main() {
  cin >> m >> n;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> mat[i][j];
    }
  }

  memset(visited, 0, sizeof(visited));
  printf("%.10f\\n", dfs(0, 0));

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;

int m, n;
double mat[18][18];
bool visited[(1 << 16)][16][16];
double memo[(1 << 16)][16][16];

double dfs(int used, int pos, int stage) {
  if (stage == n)
    return 1;
  if (pos == m)
    return 0;
  if (visited[used][pos][stage])
    return memo[used][pos][stage];
  double ret = 0;
  for (int i = 0; i < m; i++) {
    if ((used >> i) & 1)
      continue;
    double path = 1;
    double suc = 0;
    int nused = used | (1 << i);
    for (int j = stage; j < n; j++) {
      suc += path * (1 - mat[i][j]) * dfs(nused, pos + 1, j);
      path *= mat[i][j];
    }
    suc += path;
    ret = max(ret, suc);
  }
  visited[used][pos][stage] = true;
  memo[used][pos][stage] = ret;
  return ret;
}

int main() {
  cin >> m >> n;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> mat[i][j];
    }
  }

  memset(visited, 0, sizeof(visited));
  printf("%.10f\\n", dfs(0, 0, 0));

  return 0;
}
Problem p01354 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
int n, m;
double dp[1 << 16][16];
double data[17][17];

double memo(int bit, int v) {
  if (v == n)
    return 1.0;
  if (bit == 0)
    return 0.0;
  if (dp[bit][v] >= 0.0)
    return dp[bit][v];
  double res = 0.0;
  for (int i = 0; i < m; i++) {
    if (!(bit >> i & 1))
      continue;
    double sum = 0.0, win = 1.0;
    int lbit = bit - (1 << i);
    for (int j = v; j < n; j++) {
      sum += memo(lbit, j) * win * (1 - data[i][j]);
      win *= data[i][j];
    }
    sum += win;
    res = max(res, sum);
  }
  return dp[bit][v] = res;
}

int main(void) {
  scanf("%d %d", &m, &n);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%lf", &data[i][j]);
    }
  }
  memset(dp, -1, sizeof(dp));
  double res = 0.0;
  printf("%.10f\\n", memo((1 << m) - 1, 0));
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
int n, m;
float dp[1 << 16][16][16];
float dp2[1 << 16][17];
double data[17][17];

double memo(int bit, int v, int c) {
  if (v == n)
    return 1.0;
  if (bit == 0)
    return 0.0;
  if (dp[bit][v][c] >= 0.0)
    return dp[bit][v][c];
  double lost = 0.0;
  int lbit = bit - (1 << c);
  if (dp2[lbit][v] >= 0.0)
    lost = dp2[lbit][v];
  else {
    for (int i = 0; i < m; i++) {
      if (lbit >> i & 1) {
        lost = max(lost, memo(lbit, v, i));
      }
    }
    dp2[lbit][v] = lost;
  }
  double res = memo(bit, v + 1, c) * data[c][v] + lost * (1.0 - data[c][v]);
  return dp[bit][v][c] = res;
}

int main(void) {
  scanf("%d %d", &m, &n);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%lf", &data[i][j]);
    }
  }
  memset(dp, -1, sizeof(dp));
  memset(dp2, -1, sizeof(dp2));
  double res = 0.0;
  for (int i = 0; i < m; i++) {
    res = max(res, memo((1 << m) - 1, 0, i));
  }
  printf("%.10f\\n", res);
  return 0;
}