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

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int MAX = 100005;
const int LIM = 10;
double dp[MAX][LIM];
double ans[MAX];

void make() {
  memset(dp, 0, sizeof(dp));
  dp[1][1] = 1;

  for (int i = 1; i < MAX - 1; i++) {
    // cout << i << endl;
    double p = 1.0;
    for (int j = 1; j < LIM; j++) {
      p /= 2;
      if (j < LIM - 1)
        dp[i + 1][j + 1] += dp[i][j] * p;
      dp[i + 1][0] += dp[i][j] * (1.0 - p);
    }
    dp[i + 1][1] += dp[i][0];
  }

  double tmp = 0;
  for (int i = 0; i < MAX; i++) {
    for (int j = 1; j < LIM; j++)
      tmp += dp[i][j];
    ans[i] = tmp;
  }
}

int main() {
  make();
  int n;
  while (cin >> n && n)
    printf("%.8f\\n", ans[n]);
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

const int MAX = 100005;
const int LIM = 100;
double dp[MAX][LIM];
double ans[MAX];

void make() {
  memset(dp, 0, sizeof(dp));
  dp[1][1] = 1;

  for (int i = 1; i < MAX - 1; i++) {
    // cout << i << endl;
    double p = 1.0;
    for (int j = 1; j < LIM; j++) {
      p /= 2;
      if (i < LIM - 1)
        dp[i + 1][j + 1] += dp[i][j] * p;
      dp[i + 1][0] += dp[i][j] * p;
    }
    dp[i + 1][1] += dp[i][0];
  }

  double tmp = 0;
  for (int i = 0; i < MAX; i++) {
    for (int j = 1; j < LIM; j++)
      tmp += dp[i][j];
    ans[i] = tmp;
  }
}

int main() {
  make();
  int n;
  while (cin >> n && n)
    printf("%.8f\\n", ans[n]);
  return 0;
}
Problem p00642 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define NMAX 100000
double dp[NMAX + 1];

int main() {
  int n = NMAX;
  for (int i = 0; i < n + 1; i++) {
    dp[i] = 0.0;
  }
  for (int i = 0; i < n; i++) {
    long long int foo = 1;
    int bar = 1;
    for (int j = i + 1; j < n + 1 and foo < 1000000000; j++) {
      dp[j] += (1.0 - dp[i]) / foo;
      bar *= 2;
      foo *= bar;
    }
  }

  while (cin >> n and n > 0) {
    double ans = 0.0;
    for (int i = 1; i < n + 1; i++) {
      ans += dp[i];
    }
    printf("%.8f\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define NMAX 100000
double dp[NMAX + 1];

int main() {
  int n = NMAX;
  for (int i = 0; i < n + 1; i++) {
    dp[i] = 0.0;
  }
  for (int i = 0; i < n; i++) {
    int foo = 1;
    int bar = 1;
    for (int j = i + 1; j < n + 1 and foo < 1000000000; j++) {
      dp[j] += (1.0 - dp[i]) / foo;
      bar *= 2;
      foo *= bar;
    }
  }

  while (cin >> n and n > 0) {
    double ans = 0.0;
    for (int i = 1; i < n + 1; i++) {
      ans += dp[i];
    }
    printf("%.8f\\n", ans);
  }
  return 0;
}
Problem p00642 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int N = 1e5 + 10, N2 = 50;
double dp[N][N2];

const double EPS = 1e-3;

double p[N2];

double rec(int n, int i) {
  if (n == 0)
    return 0;

  double &res = dp[n][i];
  if (res >= 0)
    return res;
  res = 0;
  if (i < N2 - 1)
    res += p[i] * (rec(n - 1, i + 1) + 1);
  res += (1 - p[i]) * (rec(n - 1, 0));

  return res;
}

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

  p[0] = 1;
  rep(i, N2 - 1) { p[i + 1] = p[i] * .5; }
  rep(i, N) rep(j, N2) dp[i][j] = -1;
  rep(i, 10) rec(i * 10000, 0);

  int n;
  while (cin >> n, n) {
    cout << rec(n, 0) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

const int N = 1e5 + 10, N2 = 300;
double dp[N][N2];

const double EPS = 1e-3;

double p[N2];

double rec(int n, int i) {
  if (n == 0)
    return 0;

  double &res = dp[n][i];
  if (res >= 0)
    return res;
  res = 0;
  if (i < N2 - 1)
    res += p[i] * (rec(n - 1, i + 1) + 1);
  res += (1 - p[i]) * (rec(n - 1, 0));

  return res;
}

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

  p[0] = 1;
  rep(i, N2 - 1) { p[i + 1] = p[i] * .5; }
  rep(i, N) rep(j, N2) dp[i][j] = -1;
  rep(i, 10) rec(i * 10000, 0);

  int n;
  while (cin >> n) {
    cout << rec(n, 0) << endl;
  }

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

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

double a[100000];

double rec(int n, double k) {
  double tmp, ans;
  if (k < 10e-6)
    return 0.0;
  if (n < 1)
    return 0.0;
  if (n == 1)
    return 1.0;
  tmp = k / 2;

  ans = 1.0;
  ans += tmp * rec(n - 1, tmp);
  if (!a[n - 2])
    a[n - 2] = rec(n - 2, 1.0);
  ans += (1.0 - tmp) * a[n - 2];

  return ans;
}

int main() {
  int n;
  double d;

  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i <= n; i++)
      a[i] = 0;
    d = rec(n, 1.0);
    printf("%.6lf\\n", d);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

double a[100000];

double rec(int n, double k) {
  double tmp, ans;
  if (n < 1)
    return 0.0;
  if (n == 1)
    return 1.0;
  tmp = k / 2;

  ans = 1.0;
  ans += tmp * rec(n - 1, tmp);
  if (!a[n - 2])
    a[n - 2] = rec(n - 2, 1.0);
  ans += (1.0 - tmp) * a[n - 2];

  return ans;
}

int main() {
  int n;
  double d;

  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i <= n; i++)
      a[i] = 0;
    d = rec(n, 1.0);
    cout << d << endl;
  }
}
Problem p00642 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define MAX_N 120
#define MAX_M 120
#define MAX_P 120
#define C 0.621446216671352

double dp[MAX_N][MAX_M][MAX_P];
double ventoh[120000];

int n;

int main() {
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_M; j++) {
      for (int k = 0; k < MAX_P; k++) {
        dp[i][j][k] = 0.0;
      }
    }
  }
  dp[0][0][0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    for (int j = 0; j < MAX_M; j++) {
      for (int k = 0; k < MAX_P - 1; k++) {
        if (dp[i - 1][j][k] >= 0) {
          dp[i][j + 1][k + 1] += dp[i - 1][j][k] * 1.0 / pow(2.0, k);
          dp[i][j][0] += dp[i - 1][j][k] * (1.0 - 1.0 / pow(2.0, k));
        }
      }
    }
  }

  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_M; j++) {
      for (int k = 0; k < MAX_P; k++) {
        if (dp[i][j][k] >= 0) {
          ventoh[i] += dp[i][j][k] * j;
        }
      }
    }
  }

  for (int i = 110; i < 120000; i++) {
    ventoh[i] = ventoh[i - 1] + C;
  }

  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    cout << fixed << setprecision(15) << ventoh[n] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;

#define MAX_N 500
#define MAX_M 500
#define MAX_P 500

double dp[MAX_N][MAX_M][MAX_P];
double ventoh[MAX_N];

int n;

int main() {
  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_M; j++) {
      for (int k = 0; k < MAX_P; k++) {
        dp[i][j][k] = 0.0;
      }
    }
  }
  dp[0][0][0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    for (int j = 0; j < MAX_M; j++) {
      for (int k = 0; k < MAX_P - 1; k++) {
        if (dp[i - 1][j][k] >= 0) {
          dp[i][j + 1][k + 1] += dp[i - 1][j][k] * 1.0 / pow(2.0, k);
          dp[i][j][0] += dp[i - 1][j][k] * (1.0 - 1.0 / pow(2.0, k));
        }
      }
    }
  }

  for (int i = 0; i < MAX_N; i++) {
    for (int j = 0; j < MAX_M; j++) {
      for (int k = 0; k < MAX_P; k++) {
        if (dp[i][j][k] >= 0) {
          ventoh[i] += dp[i][j][k] * j;
        }
      }
    }
  }

  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    cout << ventoh[n] << endl;
  }
  return 0;
}
Problem p00642 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  double p[12];
  p[0] = 1;
  for (int i = 1; i < 11; i++)
    p[i] = p[i - 1] * 0.5;
  double c[12], d;
  int n;
  while (cin >> n && n) {
    c[0] = 0;
    c[1] = 1;
    double sum = 1;
    if (n == 1) {
      printf("1.00000000\\n");
      continue;
    }
    int i;
    for (int t = 2; t <= n; t++) {
      i = t;
      if (t > 11)
        i = 11;
      d = 0;
      for (int j = i; j > 0; j--) {
        c[j] = c[j - 1] * p[j - 1];
        d += c[j - 1] * (1 - p[j - 1]);
        sum += c[j];
      }
      c[0] = d;
    }
    printf("%.8lf\\n", sum);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
double d[100001];
double p[100001];
double v[100001];
using namespace std;
int main() {
  int n;
  while (cin >> n) {
    for (int f = 0; f < 100001; f++) {
      v[f] = 1.0;
    }
    v[0] = 0.5;
    if (n == 0)
      break;
    d[0] = 1;
    p[0] = 1;
    int i = 1;
    double sum = 1;
    for (; i < n; i++) {
      p[i] = 0;
      for (int j = 0; j < i; j++) {
        p[i] += d[j] * v[j];
        d[j] = d[j] * v[j];
        v[j] = v[j] / 2.0;
      }
      d[i] = 1 - p[i];
      sum += p[i];
    }
    cout << fixed << setprecision(8) << sum << endl;
  }
  return 0;
}
Problem p00642 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

double dp[100000 + 1];
int main(void) {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    double ans = 0;
    // 初期化
    fill(dp, dp + n + 1, 0);
    // 計算
    int i, j;
    for (i = 0; i <= n; i++) {
      double rate = 1, prate = 1, base = 1 - dp[i];
      for (j = i + 1; j <= n; j++) {
        double nrate = rate * prate;
        if (nrate < 0.0000000001)
          break;
        dp[j] += (nrate * base);
        prate = nrate;
        rate *= 0.5;
      }
      ans += dp[i];
    }
    printf("%0.8lf\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
double can[11];
double no[11];
double dp[100001];

int main() {
  int a, b;
  int n;
  can[0] = 1;
  no[0] = 0;
  for (a = 1; a <= 10; a++) {
    double pos = 1.0 / ((double)(1 << a));
    can[a] = can[a - 1] * pos;
    no[a] = (1 - pos) * can[a - 1];
  }
  dp[0] = 0.0;
  dp[1] = 1.0;
  dp[2] = 1.5;
  for (a = 3; a <= 100000; a++) {
    dp[a] = 0.0;
    for (b = 0; b < 10 && a - b - 1 >= 0; b++) {
      dp[a] += no[b] * (b + dp[a - b - 1]);
    }
    if (a - 1 < 10)
      dp[a] += a * can[a - 1];
  }
  while (1) {
    cin >> n;
    cout << dp[n] << endl;
  }
  return 0;
}
Problem p00642 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

const int MAX = 30;

int main() {
  cout << fixed << setprecision(10);
  vector<double> prob(MAX);
  prob[0] = 1;

  vector<double> zyou(MAX);
  zyou[0] = 1;
  for (int i = 1; i < MAX; i++)
    zyou[i] = zyou[i - 1] / 2;

  vector<double> ans;
  double ret = 0;
  for (int i = 1; i <= 100010; i++) {
    for (int j = 0; j < MAX; j++) {
      ret += zyou[j] * prob[j];
    }
    ans.push_back(ret);
    vector<double> next(MAX);
    for (int j = 0; j < MAX; j++) {
      if (j != MAX - 1) {
        next[j + 1] += prob[j] * zyou[j];
      }
      next[0] += prob[j] * (1 - zyou[j]);
    }
    prob = next;
  }

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    cout << ans[n - 1] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define mp(a, b) make_pair((a), (b))
#define eq ==

typedef long long ll;
typedef complex<double> point;
typedef pair<int, int> pii;

// →↑←↓
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};

const double EPS = 1e-9;

const int MAX = 30;

int main() {
  cout << fixed << setprecision(10);
  vector<double> prob(MAX);
  prob[0] = 1;

  vector<double> zyou(MAX);
  zyou[0] = 1;
  for (int i = 1; i < MAX; i++)
    zyou[i] = zyou[i - 1] / 2;

  vector<double> ans;
  double ret = 0;
  for (int i = 1; i <= 100010; i++) {
    for (int j = 0; j < MAX; j++) {
      ret += zyou[j] * prob[j];
    }
    ans.push_back(ret);
    vector<double> next(MAX);
    for (int j = 0; j < MAX; j++) {
      if (j != MAX - 1) {
        next[j + 1] += prob[j] * zyou[j];
      }
      next[0] += prob[j] * (1 - zyou[j]);
    }
    prob = next;
  }

  while (true) {
    int n;
    cin >> n;
    cout << ans[n - 1] << endl;
  }
  //    for(int i=0;i<MAX;i++){
  //        cout << "2^" << i << " " << prob[i] << endl;
  //    }
  cout << ret << endl;
  return 0;
}
Problem p00642 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;

  while (true) {
    cin >> n;

    if (n == 0) {
      break;
    }

    vector<vector<double>> dp(n, vector<double>(69, 0.0));

    dp[0][1] = 1.0;

    for (int i = 1; i < n; i++) {
      double t = 0.5;

      for (int j = 1; j < 69; j++) {
        dp[i][0] += dp[i - 1][j] * (1.0 - t);
        t *= 0.5;
      }

      t = 1.0;

      for (int j = 1; j < 69; j++) {
        dp[i][j] += dp[i - 1][j - 1] * t;
        t *= 0.5;
      }
    }

    double sum = 0;

    for (int i = 0; i < n; i++) {
      sum += (1 - dp[i][0]);
    }

    printf("%.49999f\\n", sum);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;

  while (true) {
    cin >> n;

    if (n == 0) {
      break;
    }

    vector<vector<double>> dp(n, vector<double>(70, 0.0));

    dp[0][1] = 1.0;

    for (int i = 1; i < n; i++) {
      double t = 0.5;

      for (int j = 1; j < 70; j++) {
        dp[i][0] += dp[i - 1][j] * (1.0 - t);
        t *= 0.5;
      }

      t = 1.0;

      for (int j = 1; j < 70; j++) {
        dp[i][j] += dp[i - 1][j - 1] * t;
        t *= 0.5;
      }
    }

    double sum = 0;

    for (int i = 0; i < n; i++) {
      sum += (1 - dp[i][0]);
    }

    printf("%.999999f\\n", sum);
  }

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

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

double mem[N][50];
bool used[N][50];
double dfs(int day, int cnt, double p) {
  if (day == 0)
    return 0;
  if (cnt >= 50)
    return 0;
  if (used[day][cnt])
    return mem[day][cnt];
  used[day][cnt] = 1;
  double res = 0;
  res += p * (1 + dfs(day - 1, cnt + 1, p / 2));
  if (cnt)
    res += (1 - p) * dfs(day - 1, 0, 1);
  return mem[day][cnt] = res;
}

int main() {
  int n;
  while (cin >> n, n)
    printf("%f\\n", dfs(n, 0, 1));
  return 0;
}

Error: MLE

Fixed submission:

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

double mem[N][50];
int used[N][50];
double dfs(int day, int cnt, double p) {

  if (day == 0)
    return 0;
  if (cnt >= 50)
    return 0;
  if (used[day][cnt]++)
    return mem[day][cnt];

  double res = 0;
  res += p * (1 + dfs(day - 1, cnt + 1, p / 2));
  if (cnt)
    res += (1 - p) * dfs(day - 1, 0, 1);
  return mem[day][cnt] = res;
}

int main() {
  int n;
  while (cin >> n, n)
    printf("%Lf\\n", dfs(n, 0, 1));
  return 0;
}
Problem p00642 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  while (cin >> n, n) {
    double memo1[40] = {};
    double memo2[40] = {};
    double ans = 0;

    memo1[0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 35; j++) {
        if (memo1[j] == 0)
          continue;
        double getexp = memo1[j] * pow(2, -j);
        memo2[j + 1] += getexp;
        memo2[0] += memo1[j] - getexp;
        ans += getexp;
        memo1[j] = 0;
      }
      swap(memo1, memo2);
    }

    printf("%f\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  while (cin >> n, n) {
    double memo1[40] = {};
    double memo2[40] = {};
    double ans = 0;

    memo1[0] = 1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 39; j++) {
        if (memo1[j] == 0)
          continue;
        double getexp = memo1[j] * pow(2, -j);
        memo2[j + 1] += getexp;
        memo2[0] += memo1[j] - getexp;
        ans += getexp;
        memo1[j] = 0;
      }
      swap(memo1, memo2);
    }

    printf("%f\\n", ans);
  }

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

#include <cstdio>
using namespace std;

const int MAX_N = 100001;
const int MAX_K = 13;

// 連続で x 回半額弁当をゲットしたときに半額弁当を得る確率
double P(int x) { return 1.0 / (1 << x); }

// dp[i][k] := i 日目で k 回連続ゲットしたときの期待値
double dp[MAX_N + 1][MAX_K + 1] = {0};
// ans[i] := i 日目の半額弁当を得る個数の期待値
double ans[MAX_N + 1] = {0};

int main() {
  int n;
  double sum = 0;

  dp[0][0] = 1.0;
  for (int i = 0; i < MAX_N; i++) {
    for (int k = 0; k < MAX_K; k++) {
      if (dp[i][k] > 0) {
        sum += dp[i][k] * P(k);

        // 半額弁当を得るとき
        dp[i + 1][k + 1] += dp[i][k] * P(k);
        // 半額弁当を得られないとき
        dp[i + 1][0] += dp[i][k] * (1.0 - P(k));
      }
    }
    ans[i] = sum;
  }

  while (scanf("%d", &n), n) {
    printf("%.8f\\n", ans[n - 1]);
  }
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;

const int MAX_N = 100001;
const int MAX_K = 100;

double P(int x) { return 1.0 / (1 << x); }

double dp[MAX_N + 1][MAX_K + 1] = {0};
double ans[MAX_N + 1] = {0};

int main() {
  int n;
  double sum = 0;

  dp[0][0] = 1.0;
  for (int i = 0; i < MAX_N; i++) {
    for (int k = 0; k < MAX_K; k++) {
      if (dp[i][k] > 0) {
        sum += dp[i][k] * P(k);
        dp[i + 1][k + 1] += dp[i][k] * P(k);
        dp[i + 1][0] += dp[i][k] * (1.0 - P(k));
      }
    }
    ans[i] = sum;
  }

  while (cin >> n && n) {
    cout << ans[n - 1] << endl;
  }
}
Problem p00642 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
using namespace std;
double dp[100001][13] = {0};
double rate[12] = {1};
int main() {
  for (int i = 1; i < 12; i++)
    rate[i] = rate[i - 1] * 0.5;
  dp[1][1] = 1;
  for (int i = 1; i < 100000; i++) {
    for (int j = 0; j < 13; j++) {
      if (j == 0)
        dp[i + 1][1] += dp[i][j];
      else if (j < 12) {
        dp[i + 1][0] += dp[i][j] * (1 - rate[j]);
        dp[i + 1][j + 1] += dp[i][j] * rate[j];
      } else {
        dp[i + 1][0] += dp[i][j];
      }
    }
  }
  int n;
  while (cin >> n, n) {
    double ans = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = 1; j < 13; j++) {
        ans += dp[i][j];
      }
    }
    printf("%.8lf\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
double dp[100001][15] = {0};
double rate[12] = {1};
int main() {
  for (int i = 1; i < 12; i++)
    rate[i] = rate[i - 1] * 0.5;
  dp[1][1] = 1;
  for (int i = 1; i < 100000; i++) {
    for (int j = 0; j < 15; j++) {
      if (j == 0)
        dp[i + 1][1] += dp[i][j];
      else if (j < 12) {
        dp[i + 1][0] += dp[i][j] * (1 - rate[j]);
        dp[i + 1][j + 1] += dp[i][j] * rate[j];
      } else {
        dp[i + 1][0] += dp[i][j];
      }
    }
  }
  int n;
  while (cin >> n, n) {
    double ans = 0;
    for (int i = 0; i <= n; i++) {
      for (int j = 1; j <= i; j++) {
        ans += dp[i][j];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00642 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <vector>
using namespace std;

constexpr int MAX_N = 100000;
constexpr int LIMIT = 50;

int main() {
  vector<double> get_p(LIMIT);
  for (int i = 0; i < LIMIT; ++i) {
    get_p[i] = pow(0.5, i);
  }

  vector<vector<double>> dp(MAX_N + 1, vector<double>(LIMIT + 1, 0.0));
  vector<double> ans(MAX_N + 1, 0.0);
  dp[0][0] = 1.0;

  for (int i = 0; i < MAX_N; ++i) {
    double cnt = 0.0;
    for (int j = 0; j < LIMIT; ++j) {
      dp[i + 1][j + 1] += get_p[j] * dp[i][j];
      dp[i + 1][0] += (1.0 - get_p[j]) * dp[i][j];
      cnt += get_p[j] * dp[i][j];
    }
    ans[i + 1] = ans[i] + cnt;
  }

  for (int n; scanf("%d", &n) == 1 && n;)
    printf("%.10lf\\n", ans[n]);

  return EXIT_SUCCESS;
}

Error: WA

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

constexpr int MAX_N = 100000;
constexpr int LIMIT = 50;

int n;
double memo[MAX_N][LIMIT + 1];

double dfs(int day, int get) {
  if (day == n)
    return 0.0;
  if (memo[day][get] >= 0.0)
    return memo[day][get];

  const double get_p = pow(0.5, get);
  double res = (1.0 - get_p) * dfs(day + 1, 0);
  if (get < LIMIT)
    res += get_p * (1.0 + dfs(day + 1, get + 1));

  return memo[day][get] = res;
}

int main() {
  while (scanf("%d", &n) == 1 && n) {
    for (int i = 0; i < n; ++i)
      for (int j = 0; j <= LIMIT; ++j)
        memo[i][j] = -1.0;
    printf("%.10lf\\n", dfs(0, 0));
  }

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

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

#define NMAX 100000
double dp[NMAX + 1];

int main() {
  int n = NMAX;
  for (int i = 0; i < n + 1; i++) {
    dp[i] = 0.0;
  }
  for (int i = 0; i < n; i++) {
    long long int foo = 1;
    long long int bar = 1;
    for (int j = i + 1; j < n + 1 and foo < 1e08; j++) {
      dp[j] += (1.0 - dp[i]) / foo;
      bar *= 2;
      foo *= bar;
    }
  }

  while (cin >> n and n > 0) {
    double ans = 0.0;
    for (int i = 1; i < n + 1; i++) {
      ans += dp[i];
    }
    printf("%.8f\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define NMAX 100000
double dp[NMAX + 1];

int main() {
  int n = NMAX;
  for (int i = 0; i < n + 1; i++) {
    dp[i] = 0.0;
  }
  for (int i = 0; i < n; i++) {
    long long int foo = 1;
    int bar = 1;
    for (int j = i + 1; j < n + 1 and j < n + 30; j++) {
      dp[j] += (1.0 - dp[i]) / foo;
      bar *= 2;
      foo *= bar;
    }
  }

  while (cin >> n and n > 0) {
    double ans = 0.0;
    for (int i = 1; i < n + 1; i++) {
      ans += dp[i];
    }
    printf("%.8f\\n", ans);
  }
  return 0;
}