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

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

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define for_rev(i, a, b) for (int i = a; i >= b; --i)

int N, M, L, P[110], T[110], V[110];
double p[110][55];

void solve() {
  for_(i, 0, N) {
    double dp[55][55] = {0.0}, pi = (double)P[i] / 100.;
    dp[0][0] = 1.0;

    for_(j, 0, M) for_(k, 0, M) {
      dp[j + 1][k] += dp[j][k] * (1.0 - pi);
      dp[j + 1][k + 1] += dp[j][k] * pi;
    }

    for_(k, 0, M + 1) p[i][k] = dp[M][k];
  }

  for_(i, 0, N) {
    double ans = 0.0;

    if (V[i] > 0) {
      for_(j, 0, M + 1) {
        double t1 = (double)L / (double)V[i] + (double)(j * T[i]);
        double mul = 1.0;

        for_(ii, 0, N) {
          if (i != ii && V[ii] > 0) {
            double mii = 0.0;
            double t2 = (double)L / (double)V[ii] + (double)(M * T[ii]);

            for_rev(k, M, 0) {
              if (t2 - t1 < 1e-9)
                break;
              mii += p[ii][k];
              t2 -= (double)T[ii];
            }

            mul *= mii;
          }
        }

        ans += mul * p[i][j];
      }
    }

    printf("%.9lf\\n", (N == 1) ? 1.0 : ans);
  }
}

int main() {
  cin >> N >> M >> L;
  for_(i, 0, N) cin >> P[i] >> T[i] >> V[i];

  solve();

  return 0;
}

Error: TLE

Fixed submission:

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

#define for_(i, a, b) for (int i = a; i < b; ++i)

struct State {
  string dial;
  int cnt;
  State(string _d, int _c) : dial(_d), cnt(_c) {}
};

int k;
string S, T;

char add(char c, int x) {
  int y = c - '0';
  return char('0' + (y + x) % 10);
}

void solve() {
  set<string> vis;
  vis.insert(S);

  queue<State> q;
  q.push(State(S, 0));

  while (!q.empty()) {
    State s = q.front();
    q.pop();

    if (s.dial == T) {
      cout << s.cnt << endl;
      break;
    }

    for_(i, 0, k) if (s.dial[i] != T[i]) {
      int dif = (T[i] - s.dial[i] + 10) % 10;

      for_(j, i, k) {
        s.dial[j] = add(s.dial[j], dif);
        if (vis.find(s.dial) == vis.end())
          q.push(State(s.dial, s.cnt + 1));
      }

      break;
    }
  }
}

int main() {
  while (cin >> k, k) {
    cin >> S >> T;
    solve();
  }

  return 0;
}
Problem p01420 (C++) — buggy submission, verdict: Runtime Error

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

int N, M, L;
vector<double> P, T, V;
double F[51];
double C[51][51];
double pTable[101][51];

int main() {
  cout.precision(16);
  cout.setf(ios::fixed);

  F[0] = 1;
  for (int i = 1; i < 51; i++)
    F[i] = F[i - 1] * i;
  for (int i = 0; i < 51; i++)
    for (int j = 0; j <= i; j++)
      C[i][j] = F[i] / F[i - j] / F[j];

  cin >> N >> M >> L;
  P.resize(N);
  T.resize(N);
  V.resize(N);
  for (int i = 0; i < N; i++)
    cin >> P[i] >> T[i] >> V[i];

  double tmp1[51], tmp2[51];
  for (int i = 0; i < N; i++) {
    tmp1[0] = tmp2[0] = 1;
    for (int j = 1; j <= M; j++) {
      tmp1[j] = tmp1[j - 1] * P[i] / 100;
      tmp2[j] = tmp2[j - 1] * (1 - P[i] / 100);
    }

    for (int j = 0; j <= M; j++) {
      pTable[i][j] = tmp1[j] * tmp2[M - j] * C[M][j];
    }
  }

  for (int i = 0; i < N; i++) {
    double ans = 0;
    for (int k = 0; k <= M; k++) { //????????????i????????????????????????
      double ti = L / V[i] + T[i] * k;
      double ttans = 1;
      for (int j = 0; j < N; j++)
        if (i != j) {
          double tans = 0;
          for (int l = 0; l <= M; l++) { //????????????j????????????????????????
            double tj = L / V[j] + T[j] * l;
            if (ti < tj) {
              tans += pTable[j][l];
            }
          }
          ttans *= tans;
        }
      ans += ttans * pTable[i][k];
    }
    cout << ans << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

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

int N, M, L;
vector<double> P, T, V;
double F[51];
double C[51][51];
double pTable[50][101];

int main() {
  cout.precision(16);
  cout.setf(ios::fixed);

  F[0] = 1;
  for (int i = 1; i < 51; i++)
    F[i] = F[i - 1] * i;
  for (int i = 0; i < 51; i++)
    for (int j = 0; j <= i; j++)
      C[i][j] = F[i] / F[i - j] / F[j];

  cin >> N >> M >> L;
  P.resize(N);
  T.resize(N);
  V.resize(N);
  for (int i = 0; i < N; i++)
    cin >> P[i] >> T[i] >> V[i];

  double tmp1[101], tmp2[101];
  for (int i = 0; i < N; i++) {
    tmp1[0] = tmp2[0] = 1;
    for (int j = 1; j <= M; j++) {
      tmp1[j] = tmp1[j - 1] * P[i] / 100;
      tmp2[j] = tmp2[j - 1] * (1 - P[i] / 100);
    }

    for (int j = 0; j <= M; j++) {
      pTable[i][j] = tmp1[j] * tmp2[M - j] * C[M][j];
    }
  }

  for (int i = 0; i < N; i++) {
    double ans = 0;
    for (int k = 0; k <= M; k++) { //????????????i????????????????????????
      double ti = L / V[i] + T[i] * k;
      double ttans = 1;
      for (int j = 0; j < N; j++)
        if (i != j) {
          double tans = 0;
          for (int l = 0; l <= M; l++) { //????????????j????????????????????????
            double tj = L / V[j] + T[j] * l;
            if (ti < tj) {
              tans += pTable[j][l];
            }
          }
          ttans *= tans;
        }
      ans += ttans * pTable[i][k];
    }
    cout << ans << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
using namespace std;
#define min(a, b) a < b ? a : b;
#define max(a, b) a > b ? a : b;
#define LL long long
const int N = 210;
const double eps = 1e-8;
double dp[N][N], p[N][N], ans[N]; // 莠コ?御シ第?蜃?惻縲よ慮髣エ?梧ヲら紫
double zu[N][N];
int n;
struct node {
  double p, t, v;
} data[N];
void zuhe() {
  memset(zu, 0, sizeof(zu));
  int i, j;
  for (i = 0; i <= 50; i++) {
    zu[i][0] = 1;
    zu[i][1] = i;
  }
  for (i = 2; i <= 50; i++)
    for (j = 1; j <= i; j++)
      zu[i][j] = zu[i - 1][j] + zu[i - 1][j - 1];
}
int main() {
  int i, j, z, k, m, l;
  double sum, res, time1, time2;
  zuhe();
  while (scanf("%d%d%d", &n, &m, &l) != EOF) {
    for (i = 1; i <= n; i++)
      scanf("%lf%lf%lf", &data[i].p, &data[i].t, &data[i].v);
    for (i = 1; i <= n; i++)
      for (j = 0; j <= m; j++)
        p[i][j] = zu[m][j] * pow(data[i].p / 100.0, j * 1.0) *
                  pow((1 - data[i].p / 100.0), (m - j) * 1.0);
    for (i = 1; i <= n; i++) {
      ans[i] = 0;
      for (j = 0; j <= m; j++) {
        res = p[i][j];
        time1 = j * data[i].t + l / data[i].v;
        for (z = 1; z <= n; z++) {
          sum = 0;
          if (z == i)
            continue;
          for (k = 0; k <= m; k++) {
            time2 = k * data[z].t + l / data[z].v;
            if (time1 < time2 - eps)
              sum += p[z][k];
          }
          res *= sum;
        }
        ans[i] += res;
      }
    }
    for (i = 1; i <= n; i++)
      printf("%.8lf\\n", ans[i]);
  }
  return 0;
}

/*
2 2 50
30 50 1
30 50 2

2 1 100
100 100 10
0 100 1

3 1 100
50 1 1
50 1 1
50 1 1

2 2 50
30 0 1
30 50 2

  */

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;

const int Max = 109;

struct P {
  int t, v;
  double p;
} stu[Max];

long long C(int n, int k) {
  long long y = 1;
  for (int i = n - k + 1, j = 2; i <= n; i++) {
    y *= i;
    while (y % j == 0 && j <= k) {
      y = y / j;
      j++;
    }
  }
  return y;
}

int main() {
  double l;
  int n, st;
  scanf("%d %d %lf", &n, &st, &l);
  for (int i = 0; i < n; i++) {
    scanf("%lf %d %d", &stu[i].p, &stu[i].t, &stu[i].v);
    stu[i].p /= 100;
  }
  double pp, t, t1, p2, ppp;
  for (int i = 0; i < n; i++) {
    ppp = 0;
    for (int j = 0; j <= st; j++) {
      t = j * stu[i].t + l / stu[i].v;
      pp = static_cast<double>(C(st, j)) * pow(stu[i].p, (double)j) *
           pow(1 - stu[i].p, (double)(st - j));
      for (int k = 0; k < n; k++) {
        if (k == i)
          continue;
        p2 = 0;
        for (int u = 0; u <= st; u++) {
          t1 = u * stu[k].t + l / stu[k].v;
          if (t1 > t)
            p2 += C(st, u) * pow(stu[k].p, (double)u) *
                  pow(1 - stu[i].p, (double)(st - u));
        }
        pp *= p2;
      }
      ppp += pp;
    }
    printf("%.8lf\\n", ppp);
  }
  return 0;
}
Problem p01420 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define eps (1e-8)

int N, M;
double L;
double P[100], T[100], V[100];
double a[100][100], b[100][100];

double mem[100][100];
double ncr(int n, int r) {
  if (mem[n][r] != -1.0)
    return mem[n][r];
  r = min(r, n - r);
  double res = 1.0;
  for (int i = 0; i < r; i++) {
    res = res * (double)(n - i);
    res = res / (double)(i + 1);
  }
  return mem[n][r] = res;
}

double solve(int x) {
  if (V[x] < eps)
    return 0;

  double res = 0;
  for (int i = 0; i <= M; i++) {
    double p = a[x][i];
    double d = b[x][i];
    for (int j = 0; j < N; j++) {
      if (x == j)
        continue;
      if (V[j] < eps) {
        continue;
      }
      double sum = 0;
      for (int k = 0; k <= M; k++) {
        if (b[j][k] < d + eps)
          continue;
        sum += a[j][k];
      }
      p *= sum;
    }
    res += p;
  }
  return res;
}

int main() {
  for (int i = 0; i < 100; i++)
    for (int j = 0; j < 100; j++)
      mem[i][j] = -1.0;

  cin >> N >> M >> L;
  for (int i = 0; i < N; i++) {
    cin >> P[i] >> T[i] >> V[i];
    if (V[i] < eps)
      continue;

    double p = P[i] / 100.0;
    double q = 1.0 - p;
    for (int j = 0; j <= M; j++) {
      a[i][j] = ncr(M, j) * pow(p, j) * pow(q, M - j);
      b[i][j] = L / V[i] + T[i] * j;
      // cout<<a[i][j]<<' '<<b[i][j]<<endl;
    }
    // cout<<endl;
  }
  for (int i = 0; i < N; i++) {
    printf("%.10f\\n", solve(i));
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define eps (1e-8)

int N, M;
double L;
double P[100], T[100], V[100];
double a[100][100], b[100][100];

double mem[100][100];
double ncr(int n, int r) {
  if (mem[n][r] != -1.0)
    return mem[n][r];
  r = min(r, n - r);
  double res = 1.0;
  for (int i = 0; i < r; i++) {
    res = res * (double)(n - i);
    res = res / (double)(i + 1);
  }
  return mem[n][r] = res;
}

double solve(int x) {
  double res = 0;
  for (int i = 0; i <= M; i++) {
    double p = a[x][i];
    double d = b[x][i];
    for (int j = 0; j < N; j++) {
      if (x == j)
        continue;
      double sum = 0;
      for (int k = 0; k <= M; k++) {
        if (b[j][k] < d + eps)
          continue;
        sum += a[j][k];
      }
      p *= sum;
    }
    res += p;
  }
  return res;
}

int main() {
  for (int i = 0; i < 100; i++)
    for (int j = 0; j < 100; j++)
      mem[i][j] = -1.0;

  cin >> N >> M >> L;
  for (int i = 0; i < N; i++) {
    cin >> P[i] >> T[i] >> V[i];
    assert(V[i] > eps);

    double p = P[i] / 100.0;
    double q = 1.0 - p;
    for (int j = 0; j <= M; j++) {
      a[i][j] = ncr(M, j) * pow(p, j) * pow(q, M - j);
      b[i][j] = L / V[i] + T[i] * j;
      // cout<<a[i][j]<<' '<<b[i][j]<<endl;
    }
    // cout<<endl;
  }
  for (int i = 0; i < N; i++) {
    printf("%.10f\\n", solve(i));
  }
  return 0;
}
Problem p01421 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 400
#define inf 1000000000

using namespace std;

struct node {
  int st, en;
} old[maxn * maxn];

int t, n, m, source, sink, ans;
int c[maxn][maxn];
int pre[maxn];
bool vs[maxn];

void init() {
  scanf("%d%d", &n, &m);
  memset(c, 0, sizeof(c));
  memset(old, 0, sizeof(old));
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    c[a][b] = c[b][a] = 1;
    old[i].st = a;
    old[i].en = b;
  }
  scanf("%d%d", &source, &sink);
}

int find() {
  memset(vs, 0, sizeof(vs));
  memset(pre, 0, sizeof(pre));
  int zhan[maxn];
  int head = 0, tail = 1;
  zhan[tail] = source;
  vs[zhan[1]] = true;
  while (head != tail) {
    int now = zhan[++head];
    for (int i = 1; i <= n; i++)
      if (!vs[i] && c[now][i] > 0) {
        zhan[++tail] = i;
        vs[i] = true;
        pre[i] = now;
        if (i == sink)
          return 1;
      }
  }
  return 0;
}

void sug() {
  int now = sink, minflow = inf;
  while (now != source) {
    if (c[pre[now]][now] < minflow)
      minflow = c[pre[now]][now];
    now = pre[now];
  }
  ans += minflow;
  now = sink;
  while (now != source) {
    c[pre[now]][now] -= minflow;
    c[now][pre[now]] += minflow;
    now = pre[now];
  }
}

int main() {
  init();
  ans = 0;
  while (find()) {
    sug();
  }
  printf("%d\\n", ans);
  int tot = 0;
  int change[maxn];
  memset(change, 0, sizeof(change));
  for (int i = 1; i <= m; i++) {
    int st = old[i].st, en = old[i].en;
    if (c[en][st] == 0)
      change[++tot] = i;
  }
  printf("%d\\n", tot);
  for (int i = 1; i <= tot; i++)
    printf("%d\\n", change[i]);
  return 0;
}

Error: 0

Fixed submission:

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

int op[310][310];
int vis[90010];
int N, M, R, S, ans = 0;
int visn[400];

bool dfs(int f) {
  visn[f] = 1;
  int i;
  int flag = 0;
  if (op[f][S]) {
    ans++;
    flag = 1;
  }
  if (op[S][f]) {
    ans++;
    vis[op[S][f]] = 1;
    op[f][S] = op[S][f];
    op[S][f] = 0;
    flag = 1;
  }

  for (i = 1; i <= M; i++) {
    if (!op[f][i] || visn[i] == 1)
      continue;

    if (dfs(i)) {
      flag = 1;
    }
  }

  for (i = 1; i <= M; i++) {
    if (!op[i][f] || vis[i] == 1)
      continue;

    if (dfs(i)) {
      vis[op[i][f]] = 1;
      op[f][i] = op[i][f];
      op[i][f] = 0;
      flag = 1;
    }
  }

  visn[f] = 0;
  if (flag)
    return true;
  return false;
}

int main() {
  int x, y, i;
  scanf("%d%d", &N, &M);
  memset(op, 0, sizeof(op));
  memset(vis, 0, sizeof(vis));
  memset(visn, 0, sizeof(visn));
  for (i = 1; i <= M; i++) {
    scanf("%d%d", &x, &y);
    op[x][y] = i;
  }
  scanf("%d%d", &R, &S);

  visn[S] = 1;
  dfs(R);
  printf("%d\\n", ans);

  ans = 0;
  for (i = 1; i <= 90001; i++) {
    if (vis[i])
      ans++;
  }
  printf("%d\\n", ans);

  for (i = 1; i <= 90001; i++) {
    if (vis[i])
      printf("%d\\n", i);
  }

  return 0;
}
Problem p01421 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int inf = 1012345678;
struct edge {
  int to, cap, rev, id;
};
void add_edge(int a, int b, int id, vector<vector<edge>> &G) {
  G[a].push_back(edge{b, 1, int(G[b].size()), id});
  G[b].push_back(edge{a, 0, int(G[a].size()) - 1, id});
}
int find_augment(int pos, int tar, int curmax, vector<vector<edge>> &G,
                 vector<bool> &vis) {
  if (pos == tar)
    return curmax;
  vis[pos] = true;
  for (edge &e : G[pos]) {
    if (vis[e.to] || e.cap == 0)
      continue;
    int res = find_augment(e.to, tar, min(curmax, e.cap), G, vis);
    if (res > 0) {
      e.cap -= res;
      G[e.to][e.rev].cap += res;
      return res;
    }
  }
  return 0;
}
int max_flow(int S, int T, vector<vector<edge>> &G) {
  int V = G.size();
  int ans = 0;
  while (true) {
    vector<bool> vis(V, false);
    int res = find_augment(S, T, inf, G, vis);
    if (res > 0)
      ans += res;
    else
      break;
  }
  return ans;
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<edge>> G(N);
  vector<int> X(M), Y(M);
  for (int i = 0; i < M; ++i) {
    cin >> X[i] >> Y[i];
    --X[i], --Y[i];
    add_edge(X[i], Y[i], i, G);
    add_edge(Y[i], X[i], i, G);
  }
  int S, T;
  cin >> S >> T;
  --S, --T;
  int ret = max_flow(S, T, G);
  cout << ret << endl;
  vector<int> elist;
  for (int i = 0; i < M; ++i) {
    int revused = 0;
    for (edge e : G[X[i]]) {
      if (e.to == Y[i])
        revused += e.cap;
    }
    if (revused == 2) {
      elist.push_back(i);
    }
  }
  cout << elist.size() << endl;
  for (int i : elist)
    cout << i + 1 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct edge {
  int to, cap, rev;
};
int V, E, S, T, c[109][109];
vector<int> used;
vector<vector<edge>> G;
void add_edge(int v1, int v2, int cap) {
  G[v1].push_back(edge{v2, cap, (int)G[v2].size()});
  G[v2].push_back(edge{v1, 0, (int)G[v1].size() - 1});
}
int rec(int p, int t, int f) {
  if (p == t)
    return f;
  used[p] = 1;
  for (auto &e : G[p]) {
    if (used[e.to] || e.cap <= 0)
      continue;
    int res = rec(e.to, t, min(e.cap, f));
    if (res) {
      c[p][e.to]++;
      e.cap -= res, G[e.to][e.rev].cap += res;
      return res;
    }
  }
  return 0;
}
int max_flow(int s, int t) {
  int ret = 0;
  while (true) {
    used = vector<int>(V, 0);
    int res = rec(s, t, 2147483647);
    if (!res)
      break;
    ret += res;
  }
  return ret;
}
int main() {
  cin >> V >> E;
  G.resize(V);
  vector<int> x1(E), x2(E);
  for (int i = 0; i < E; i++) {
    cin >> x1[i] >> x2[i];
    x1[i]--, x2[i]--;
    add_edge(x1[i], x2[i], 1);
    add_edge(x2[i], x1[i], 1);
  }
  cin >> S >> T;
  S--, T--;
  cout << max_flow(S, T) << endl;
  int ret = 0;
  for (int i = 0; i < E; i++) {
    if (!c[x1[i]][x2[i]])
      ret++;
  }
  cout << ret << endl;
  for (int i = 0; i < E; i++) {
    if (!c[x1[i]][x2[i]])
      cout << i + 1 << endl;
  }
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 22
#define MAX_C 222222
#define INF 9999999999.99

vector<int> yakusuu[MAX_C];
double dp[MAX_N][MAX_C];
int n;
int x[MAX_N];

void yaku() {
  for (int i = 1; i < MAX_C; i++) {
    for (int j = i; j < MAX_C; j += i) {
      yakusuu[j].push_back(i);
    }
  }
}

int main() {
  yaku();
  cin >> n;
  x[0] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
  }
  dp[0][0] = INF;
  for (int i = 1; i < MAX_C; i++) {
    dp[1][i] = 1.0 * abs(x[1] - i) / x[1];
  }
  for (int i = 2; i < MAX_N; i++) {
    for (int j = 0; j < MAX_C; j++) {
      dp[i][j] = INF;
    }
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j < MAX_C; j++) {
      for (int k = 0; k < yakusuu[j].size(); k++) {
        double pos1 = dp[i - 1][yakusuu[j][k]];
        double pos2 = 1.0 * abs(x[i] - j) / x[i];
        double pos3 = max(pos1, pos2);
        dp[i][j] = min(dp[i][j], pos3);
      }
    }
  }
  double minx = INF;
  for (int i = 0; i < MAX_C; i++) {
    minx = min(minx, dp[n][i]);
  }
  cout << fixed << setprecision(15) << minx << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;

#define MAX_N 25
#define MAX_C 300000
#define INF 9999999999.99

vector<int> yakusuu[MAX_C];
double dp[MAX_N][MAX_C];
int n;
int x[MAX_N];

void yaku() {
  for (int i = 1; i < MAX_C; i++) {
    for (int j = i; j < MAX_C; j += i) {
      yakusuu[j].push_back(i);
    }
  }
}

int main() {
  yaku();
  cin >> n;
  x[0] = 1;
  for (int i = 1; i <= n; i++) {
    cin >> x[i];
  }
  dp[0][0] = INF;
  for (int i = 1; i < MAX_C; i++) {
    dp[1][i] = 1.0 * abs(x[1] - i) / x[1];
  }
  for (int i = 2; i < MAX_N; i++) {
    for (int j = 0; j < MAX_C; j++) {
      dp[i][j] = INF;
    }
  }
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j < MAX_C; j++) {
      for (int k = 0; k < yakusuu[j].size(); k++) {
        double pos1 = dp[i - 1][yakusuu[j][k]];
        double pos2 = 1.0 * abs(x[i] - j) / x[i];
        double pos3 = max(pos1, pos2);
        dp[i][j] = min(dp[i][j], pos3);
      }
    }
  }
  double minx = INF;
  for (int i = 0; i < MAX_C; i++) {
    minx = min(minx, dp[n][i]);
  }
  cout << fixed << setprecision(15) << minx << endl;
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define N 30
using namespace std;
typedef long long ll;
int n, A[N];

unordered_map<ll, double> mem[N];
double calc(double a, double b) { return abs(a - b) / a; }

double dfs(int idx, ll num) {
  if (idx == n)
    return 0;
  if (mem[idx].count(num))
    return mem[idx][num];

  double res = 1LL << 55;
  for (int i = idx; i < n; i++) {
    ll t = num * ((A[i] - 1) / num + 1);
    res = min(res, max(calc(A[idx], t), dfs(idx + 1, t)));
  }
  return mem[idx][num] = res;
}

double DP() {
  unordered_map<ll, double> dp[2];
  int f = 0;
  dp[0][1] = 0;
  for (int i = 0; i < n; i++, f = !f) {
    dp[!f].clear();
    for (auto it = dp[f].begin(); it != dp[f].end(); it++) {
      ll num = it->first;
      double cost = it->second;
      for (int j = num;; j += num) {
        if (dp[!f].count(j))
          dp[!f][j] = min(dp[!f][j], max(calc(A[i], j), cost));
        else
          dp[!f][j] = max(calc(A[i], j), cost);
        if (j > A[n - 1])
          break;
      }
    }
  }

  double res = 1LL << 55;
  for (auto it = dp[f].begin(); it != dp[f].end(); it++)
    res = min(res, it->second);
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  printf("%.10f\\n", DP());
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define N 30
using namespace std;
typedef long long ll;
int n, A[N];

unordered_map<ll, double> mem[N];
double calc(double a, double b) { return abs(a - b) / a; }

double dfs(int idx, ll num) {
  if (idx == n)
    return 0;
  if (mem[idx].count(num))
    return mem[idx][num];

  double res = 1LL << 55;
  for (int i = num;; i += num) {
    res = min(res, max(calc(A[idx], i), dfs(idx + 1, i)));
    if (i > A[n - 1])
      break;
  }
  return mem[idx][num] = res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  printf("%.10f\\n", dfs(0, 1));
  return 0;
}
Problem p01422 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

const int maxn = 22;
const int maxm = 150000;
double dp[maxn][maxm];
int a[maxn];

double rat(int x, int y) { return 1.0 * abs(x - y) / x; }

int main() {
  int n, i, j, k;
  double ans, Min;
  while (~scanf("%d", &n)) {
    for (i = 1; i <= n; ++i)
      scanf("%d", &a[i]);
    memset(dp, 0x7f, sizeof(dp));
    for (i = 1; i < maxm; ++i) {
      dp[1][i] = rat(a[1], i);
    }
    for (i = 2; i <= n; ++i) {
      // Min = dp[0][0];
      for (j = 1; j < maxm; ++j) {
        for (k = 1; k * j < maxm; ++k) {
          dp[i][k * j] = min(dp[i][k * j], max(dp[i - 1][j], rat(a[i], k * j)));
          // printf ("%lf %lf %lf\\n", dp[i][k * j], dp[i-1][j], rat (a[i], k *
          // j)); Min = min (Min, dp[i][k * j]);
        }
      }
      // printf ("Min: %lf\\n", Min);
    }
    ans = dp[n][1];
    for (j = 2; j < maxm; ++j) {
      ans = min(ans, dp[n][j]);
    }
    printf("%.12lf\\n", ans);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

const int maxn = 22;
const int maxm = 222222;
double dp[maxn][maxm];
int a[maxn];

double rat(int x, int y) { return 1.0 * abs(x - y) / x; }

int main() {
  int n, i, j, k;
  double ans, Min;
  while (~scanf("%d", &n)) {
    for (i = 1; i <= n; ++i)
      scanf("%d", &a[i]);
    memset(dp, 0x7f, sizeof(dp));
    for (i = 1; i < maxm; ++i) {
      dp[1][i] = rat(a[1], i);
    }
    for (i = 2; i <= n; ++i) {
      // Min = dp[0][0];
      for (j = 1; j < maxm; ++j) {
        for (k = 1; k * j < maxm; ++k) {
          dp[i][k * j] = min(dp[i][k * j], max(dp[i - 1][j], rat(a[i], k * j)));
          // printf ("%lf %lf %lf\\n", dp[i][k * j], dp[i-1][j], rat (a[i], k *
          // j)); Min = min (Min, dp[i][k * j]);
        }
      }
      // printf ("Min: %lf\\n", Min);
    }
    ans = dp[n][1];
    for (j = 2; j < maxm; ++j) {
      ans = min(ans, dp[n][j]);
    }
    printf("%.12lf\\n", ans);
  }
  return 0;
}