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

#include <cstdio>
#include <iostream>

using namespace std;

#define MAX_N 100000
#define CUT 1000

double ans[MAX_N + 1] = {0};
double b[MAX_N + 1];
double memo[3][CUT + 1] = {0};

double E(int rest, int con) {
  if (con == 0 && ans[rest] > 0)
    return ans[rest];
  if (rest == 0)
    return 0;
  //	if (con > 1000) return E(rest-1, 0);

  double p = b[con];
  double ret = p * (E(rest - 1, con + 1) + 1) + (1 - p) * E(rest - 1, 0);
  if (con == 0)
    ans[rest] = ret;
  return ret;
}

int main() {
  b[0] = 1.0;
  for (int i = 1; i <= MAX_N; ++i) {
    b[i] = b[i - 1] / 2;
  }
  for (int i = 1; i <= MAX_N; ++i) {
    for (int j = 0; j < min(CUT, MAX_N - i + 1); ++j) {
      if (j == CUT)
        b[j] * ((1 - b[j + 1]) * memo[(i + 1) % 3][0] + 1) +
            (1 - b[j]) * memo[(i - 1) % 3][0];
      else
        memo[i % 3][j] = b[j] * (memo[(i - 1) % 3][j + 1] + 1) +
                         (1 - b[j]) * memo[(i - 1) % 3][0];
    }
    ans[i] = memo[i % 3][0];
  }
  ans[0] = 0;

  //	E(MAX_N, 0);

  int n;
  while (cin >> n, n) {
    //		printf("%.10f\\n", E(MAX_N, 0));
    printf("%.10f\\n", ans[n]);
  }
  //	for (int i = 0; i < 100; ++i) printf("%.10f\\n", ans[i]);
  //	printf("%.10f\\n", E(MAX_N, 0));
  //	printf("%.10f\\n", ans[MAX_N]);
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

using namespace std;

#define MAX_N 100000
#define CUT 10000

double ans[MAX_N + 1] = {0};
double b[MAX_N + 1];
double memo[3][CUT + 1] = {0};

double E(int rest, int con) {
  if (con == 0 && ans[rest] > 0)
    return ans[rest];
  if (rest == 0)
    return 0;
  //	if (con > 1000) return E(rest-1, 0);

  double p = b[con];
  double ret = p * (E(rest - 1, con + 1) + 1) + (1 - p) * E(rest - 1, 0);
  if (con == 0)
    ans[rest] = ret;
  return ret;
}

int main() {
  b[0] = 1.0;
  for (int i = 1; i <= MAX_N; ++i) {
    b[i] = b[i - 1] / 2;
  }
  for (int i = 1; i <= MAX_N; ++i) {
    for (int j = 0; j < min(CUT, MAX_N - i + 1); ++j) {
      if (j == CUT)
        b[j] * ((1 - b[j + 1]) * memo[(i + 1) % 3][0] + 1) +
            (1 - b[j]) * memo[(i - 1) % 3][0];
      else
        memo[i % 3][j] = b[j] * (memo[(i - 1) % 3][j + 1] + 1) +
                         (1 - b[j]) * memo[(i - 1) % 3][0];
    }
    ans[i] = memo[i % 3][0];
  }
  ans[0] = 0;

  //	E(MAX_N, 0);

  int n;
  while (cin >> n, n) {
    //		printf("%.10f\\n", E(MAX_N, 0));
    printf("%.10f\\n", ans[n]);
  }
  for (int i = 0; i < 100; ++i)
    printf("%.10f\\n", ans[i]);
  //	printf("%.10f\\n", E(MAX_N, 0));
  //	printf("%.10f\\n", ans[MAX_N]);
}
Problem p00642 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  cout << setprecision(7) << fixed;
  vector<vector<ld>> dp(100005, vector<ld>(50));
  dp[0][0] = 1;
  for (int n = 0; n < 100004; ++n) {
    for (int i = 0; i < 48; ++i) {
      dp[n + 1][i + 1] += dp[n][i] / pow(2, i);
      dp[n + 1][0] += dp[n][i] * (1 - 1 / pow(2, i));
    }
  }
  vector<ld> anss(100001);
  for (int i = 0; i < 100001; ++i) {
    anss[i + 1] = anss[i];
    for (int j = 1; j < 50; ++j) {
      anss[i + 1] += dp[i + 1][j];
    }
  }
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    cout << anss[N] << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
const ld eps = 1e-9;

//// < "d:\\d_download\\visual studio
///2015\\projects\\programing_contest_c++\\debug\\a.txt" > "d:\\d_download\\visual
///studio 2015\\projects\\programing_contest_c++\\debug\\b.txt"

int main() {
  cout << setprecision(7) << fixed;
  vector<vector<ld>> dp(100005, vector<ld>(50));
  dp[0][0] = 1;
  for (int n = 0; n < 100004; ++n) {
    for (int i = 0; i < 48; ++i) {
      dp[n + 1][i + 1] += dp[n][i] / pow(2, i);
      dp[n + 1][0] += dp[n][i] * (1 - 1 / pow(2, i));
    }
  }
  vector<ld> anss(100001);
  for (int i = 0; i < 100001; ++i) {
    anss[i + 1] = anss[i];
    for (int j = 1; j < 50; ++j) {
      anss[i + 1] += dp[i + 1][j];
    }
  }
  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    cout << anss[N] << endl;
  }

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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define _USE_MATH_DEFINES
using namespace std;

typedef unsigned long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<string, map<string, int>> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;

double getb[200000];
double dp[100001][30];

int main() {
  int n;
  fill(&dp[0][0], &dp[100000][29] + 1, 0.0);
  dp[1][1] = 1.0;
  for (int i = 1; i < 100000; i++) {
    for (int j = 0; j < 29; j++) {
      dp[i + 1][j + 1] += dp[i][j] * pow(0.5, j);
      dp[i + 1][0] += dp[i][j] * (1 - pow(0.5, j));
    }
  }
  getb[0] = 0;
  for (int i = 1; i <= 100000; i++) {
    getb[i] = 0.0;
    for (int j = 1; j < 30; j++) {
      getb[i] += dp[i][j];
    }
    getb[i] += getb[i - 1];
  }
  while (cin >> n && n) {
    cout << fixed << setprecision(9) << getb[n] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
#define _USE_MATH_DEFINES
using namespace std;

typedef unsigned long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<string, map<string, int>> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 30;
const double EPS = 1e-9;

double getb[200000];
double dp[100001][30];

int main() {
  int n;
  fill(&dp[0][0], &dp[199999][49] + 1, 0.0);
  dp[1][1] = 1.0;
  for (int i = 1; i < 100000; i++) {
    for (int j = 0; j < 29; j++) {
      dp[i + 1][j + 1] += dp[i][j] * pow(0.5, j);
      dp[i + 1][0] += dp[i][j] * (1 - pow(0.5, j));
    }
  }
  getb[0] = 0;
  for (int i = 1; i <= 100000; i++) {
    getb[i] = 0.0;
    for (int j = 1; j < 30; j++) {
      getb[i] += dp[i][j];
    }
    getb[i] += getb[i - 1];
  }
  while (cin >> n && n) {
    cout << fixed << setprecision(9) << getb[n] << endl;
  }
  return 0;
}
Problem p00642 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iomanip>
#include <iostream>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

double dp[100005][15];
double two[105];

double solve(int n, int k) {
  if (n == 0)
    return 0;
  double &d = dp[n][k];
  if (d != -1.0)
    return d;
  return d = two[k] * (1.0 + solve(n - 1, min(k + 1, 10))) +
             (1.0 - two[k]) * solve(n - 1, 0);
}

int main() {

  int n;
  cout << fixed << setprecision(10);

  rep(i, 100005) rep(j, 15) dp[i][j] = -1.0;
  double tmp = 1.0;
  rep(i, 15) {
    two[i] = tmp;
    tmp *= 0.5;
  }

  while (cin >> n) {
    if (n == 0)
      break;
    cout << solve(n, 0) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iomanip>
#include <iostream>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

double dp[100005][105];
double two[105];

double solve(int n, int k) {
  if (n == 0)
    return 0;
  double &d = dp[n][k];
  if (d != -1.0)
    return d;
  return d = two[k] * (1.0 + solve(n - 1, min(k + 1, 100))) +
             (1.0 - two[k]) * solve(n - 1, 0);
}

int main() {

  int n;
  cout << fixed << setprecision(10);

  rep(i, 100005) rep(j, 105) dp[i][j] = -1.0;
  double tmp = 1.0;
  rep(i, 105) {
    two[i] = tmp;
    tmp *= 0.5;
  }

  while (cin >> n) {
    if (n == 0)
      break;
    cout << solve(n, 0) << endl;
  }

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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;

int n;
double dp[100001][30], ans[100001];

int main() {
  double pw[30];
  pw[0] = 1;
  rep(i, 29) pw[i + 1] = pw[i] * 0.5;

  fill_n(ans, 100001, 0);
  rep(i, 100001) rep(j, 30) dp[i][j] = 0;
  dp[0][0] = 1;

  for (int i = 1; i < 100001; i++) {
    rep(j, 30) {
      dp[i][0] += dp[i - 1][j] * (1 - pw[j]);
      if (j < 29)
        dp[i][j + 1] += dp[i - 1][j] * pw[j];
    }
    ans[i] = ans[i - 1];
    rep(j, 29) ans[i] += dp[i][j + 1];
  }

  while (cin >> n, n)
    printf("%.3f\\n", ans[n]);

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;

int n;
double dp[100001][50], ans[100001];

int main() {
  double pw[50];
  pw[0] = 1;
  rep(i, 49) pw[i + 1] = pw[i] * 0.5;

  rep(i, 100001) rep(j, 50) dp[i][j] = 0;
  dp[0][0] = 1;

  for (int i = 1; i < 100001; i++) {
    rep(j, 50) {
      dp[i][0] += dp[i - 1][j] * (1 - pw[j]);
      if (j < 49)
        dp[i][j + 1] += dp[i - 1][j] * pw[j];
    }
    ans[i] = ans[i - 1];
    rep(j, 49) ans[i] += dp[i][j + 1];
  }

  while (cin >> n, n)
    cout << ans[n] << endl;

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

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

double dp[2][65];
double ans[100001];

int main() {
  int n;
  double cnt = 0;
  dp[0][0] = 1.0;

  int f = 0;
  for (int i = 0; i <= 100000; i++) {

    fill(dp[f ^ 1], dp[f ^ 1] + 65, 0);
    for (int j = 0; j <= min(j, 64); j++) {
      double r = 1.0 / pow(2.0, j);
      dp[f ^ 1][min(j + 1, 64)] += dp[f][min(j, 64)] * r;
      cnt += dp[f][min(j, 64)] * r;
      dp[f ^ 1][0] += dp[f][min(j, 64)] * (1 - r);
    }

    f ^= 1;
    ans[i + 1] = cnt;
  }
  while (cin >> n, n) {
    printf("%.4f\\n", ans[n]);
  }
}

Error: MLE

Fixed submission:

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

double dp[100002][65];
double ans[100001];

int main() {
  int n;
  double cnt = 0;
  dp[0][0] = 1.0;
  for (int i = 0; i <= 100000; i++) {
    for (int j = 0; j <= min(j, 64); j++) {
      double r = 1.0 / pow(2.0, j);
      dp[i + 1][min(j + 1, 64)] += dp[i][min(j, 64)] * r;
      cnt += dp[i][min(j, 64)] * r;
      dp[i + 1][0] += dp[i][min(j, 64)] * (1 - r);
    }
    ans[i + 1] = cnt;
  }
  while (cin >> n, n) {
    printf("%.4f\\n", ans[n]);
  }
}
Problem p00642 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
const int MAX = 12;

int main() {
  double e[100001];
  for (int i = 0; i <= 100000; i++)
    e[i] = 0.0;

  e[0] = 0.0;
  e[1] = 1.0;
  e[2] = 1.5;
  double pro[MAX + 1], imp[MAX + 1];
  pro[0] = 1.0;
  for (int i = 1; i <= MAX; i++) {
    double p = 1.0 / (1 << (i - 1));
    pro[i] = pro[i - 1] * p;
    imp[i] = pro[i - 1] * (1.0 - p);
    //		cout<<imp[i]<<endl;
  }

  for (int day = 3; day <= 100000; day++) {
    for (int k = 2; k <= min(day, MAX); k++) {
      e[day] += imp[k] * (e[day - k] + k - 1);
      //			cout<<imp[k]<<" "<<(e[day-k]+k-1)<<endl;
    }
    if (day < MAX)
      e[day] += pro[day] * day;
    //		cout<<e[day]<<endl;
  }
  int n;
  while (cin >> n && n != 0)
    printf("%.3lf\\n", e[n]);

  return 0;
}

Error: MLE

Fixed submission:

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

double c[100001][100];

int main() {
  int n;
  double exp[100001];
  double pro[100];
  pro[0] = 1.0;
  for (int i = 1; i < 100; i++) {
    pro[i] = pro[i - 1] / 2.0;
    //		cout<<pro[i]<<" ";
  }
  //	cout<<endl<<endl;

  for (int i = 0; i < 100001; i++) {
    for (int j = 0; j < 100; j++)
      c[i][j] = 0.0;
    exp[i] = 0.0;
  }
  c[0][0] = 1.0;

  for (int day = 1; day < 10001; day++) {
    exp[day] = exp[day - 1];
    c[day][1] = c[day - 1][0];
    for (int k = 1; k < 100; k++) {
      c[day][k + 1] = c[day - 1][k] * pro[k];
      c[day][0] += c[day - 1][k] * (1 - pro[k]);
      exp[day] += k * c[day - 1][k] * (1 - pro[k]);
    }
    //		for(int k=0;k<20;k++)
    //			printf("%.3f ",c[day][k]);
    //		cout<<" exp "<<exp[day]<<endl;
  }

  while (cin >> n && n != 0) {
    double ans = 0.0;
    ans += exp[n];
    for (int i = 0; i < 100; i++) {
      ans += c[n][i] * i;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00642 (C++) — buggy submission, verdict: Runtime Error

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

double ans[100001];
double keisu[100001];

int main() {

  for (int i = 1; i <= 100000; i++) {

    keisu[i] = (pow(2, i) - 1) / pow(2, i * (i + 1) / 2);
  }

  ans[1] = 1.00;
  ans[2] = 1.50;

  for (int i = 3; i <= 100000; i++) {

    ans[i] = 0.00;

    for (int j = 1; j <= 100; j++) {

      if (i - j - 1 == 0)
        break;

      ans[i] += (ans[i - j - 1] + j) * keisu[j];
    }

    if (i > 100)
      continue;

    ans[i] += keisu[i - 1] * (i - 1);

    ans[i] += 1.00 * i / pow(2, i * (i - 1) / 2);
  }

  while (true) {

    long long int n;

    cin >> n;

    if (n == 0)
      break;

    cout << fixed << setprecision(8) << ans[n] << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

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

int main() {

  double keisu[101];

  for (int i = 1; i <= 100000; i++) {

    keisu[i] = (pow(2, i) - 1) / pow(2, i * (i + 1) / 2);
  }

  double ans[100001] = {0};

  ans[1] = 1.00;
  ans[2] = 1.50;

  for (int i = 3; i <= 100000; i++) {

    for (int j = 1; j <= 10; j++) {

      if (i - j - 1 == 0)
        break;

      ans[i] += (ans[i - j - 1] + j) * keisu[j];
    }

    ans[i] += keisu[i - 1] * (i - 1);

    ans[i] += 1.00 * i / pow(2, i * (i - 1) / 2);
  }

  while (true) {

    long long int n;

    cin >> n;

    if (n == 0)
      break;

    cout << ans[n] << endl;
  }

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

#include <stdio.h>
double dp[2][100];
int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      return 0;
    int i, j;
    double ans = 0.0f;
    for (j = 0; j < 100; j++)
      dp[0][j] = 0.0f;
    dp[0][1] = 1.0f;
    for (i = 0; i < n - 1; i++) {
      for (j = 0; j < 100; j++)
        dp[(i + 1) % 2][j] = 0.0f;
      dp[(i + 1) % 2][1] += dp[i % 2][0];
      for (j = 1; j < 99; j++) {
        double p = 1.0f / (1 << j);
        dp[(i + 1) % 2][j + 1] += dp[i % 2][j] * p;
        dp[(i + 1) % 2][0] += dp[i % 2][j] * (1 - p);
        ans += dp[i % 2][j];
      }
    }
    for (j = 1; j < 100; j++)
      ans += dp[(n + 1) % 2][j];
    printf("%lf\\n", ans);
  }
}

Error: MLE

Fixed submission:

#include <stdio.h>
double dp[100000][100];
int main() {
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      return 0;
    int i, j;
    for (i = 0; i < n; i++)
      for (j = 0; j < 100; j++)
        dp[i][j] = 0.0f;
    dp[0][1] = 1.0f;
    for (i = 0; i < n - 1; i++) {
      dp[i + 1][1] += dp[i][0];
      for (j = 1; j < 99; j++) {
        double p = 1.0f / (1 << j);
        dp[i + 1][j + 1] += dp[i][j] * p;
        dp[i + 1][0] += dp[i][j] * (1 - p);
      }
    }
    double ans = 0.0f;
    for (i = 0; i < n; i++)
      for (j = 1; j < 100; j++)
        ans += dp[i][j];
    printf("%lf\\n", ans);
  }
}
Problem p00642 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 100001;

// iツ嘉アツ姪堋づ泳ツ古つ連ツ堕アツつオツづ偲ヲツづェツづゥツ確ツ猟ヲツづーツ格ツ納
double dp[MAX_N][51];
double ret[MAX_N];

int main() {
  fill(ret, ret + MAX_N, 0.0);
  for (int i = 0; i < MAX_N; i++)
    fill(dp[i], dp[i] + 51, 0);

  dp[1][1] = 1.0;
  ret[1] = 1.0;
  for (int i = 2; i < MAX_N; i++) {
    double ss = 0;
    // iツ嘉アツ姪堋づ泳ツ古つ連ツ堕アツつオツづ偲ヲツづェツづゥツ確ツ猟ヲツづーツ計ツ算
    for (int j = 0; j <= min(i, 50); j++) {
      // ツ債。ツ嘉アツミツス
      if (j == 0) {
        for (int k = 1; k <= min(i - 1, 50); k++)
          dp[i][j] += dp[i - 1][k] * (1 - pow(0.5, k));
      }
      // ツ連ツ堕アツ偲ヲツ督セ
      else {
        dp[i][j] = dp[i - 1][j - 1] * (pow(0.5, j - 1));
        ss += dp[i][j];
      }
    }
    ret[i] = ss + ret[i - 1];
  }

  int n;
  while (cin >> n && n != 0) {
    printf("%.3lf\\n", ret[n]);
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 100001;

int main() {
  // iツ嘉アツ姪堋づ泳ツ古つ連ツ堕アツつオツづ偲ヲツづェツづゥツ確ツ猟ヲツづーツ格ツ納
  double dp[MAX_N][51];
  double ret[MAX_N];
  fill(ret, ret + MAX_N, 0.0);
  for (int i = 0; i < MAX_N; i++)
    fill(dp[i], dp[i] + 51, 0);

  dp[1][1] = 1.0;
  ret[1] = 1.0;
  for (int i = 2; i < MAX_N; i++) {
    double ss = 0;
    // iツ嘉アツ姪堋づ泳ツ古つ連ツ堕アツつオツづ偲ヲツづェツづゥツ確ツ猟ヲツづーツ計ツ算
    for (int j = 0; j <= min(i, 50); j++) {
      // ツ債。ツ嘉アツミツス
      if (j == 0) {
        for (int k = 1; k <= min(i - 1, 50); k++)
          dp[i][j] += dp[i - 1][k] * (1 - pow(0.5, k));
      }
      // ツ連ツ堕アツ偲ヲツ督セ
      else {
        dp[i][j] = dp[i - 1][j - 1] * (pow(0.5, j - 1));
        ss += dp[i][j];
      }
    }
    ret[i] = ss + ret[i - 1];
  }

  int n;
  while (cin >> n && n != 0) {
    printf("%.3lf\\n", ret[n]);
  }

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

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;

bool m1[100001] = {0};
ld m2[100001];

#define EPS 1e-12

ld dp(int N) {
  if (N == 0)
    return 0;
  if (m1[N])
    return m2[N];

  ld res = 0, p = 1, np = 1;
  for (int i = 1; i < N; ++i) {
    np /= 2;
    ld dr = p * (1 - np) * (i + dp(N - i - 1));
    res += dr;
    if (dr < EPS)
      break;
    p *= np;
  }
  res += p * N;
  return m1[N] = true, m2[N] = res;
}

ld solve(int N) { return dp(N); }

int main() {
  int N;
  cout.precision(10);
  while (cin >> N, N) {
    cout << solve(N) << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;

bool m1[100001] = {0};
ld m2[100001];

ld dp(int N) {
  if (N == 0)
    return 0;
  if (m1[N])
    return m2[N];

  ld res = 0, p = 1, np = 1;
  for (int i = 1; i < N; ++i) {
    np /= 2;
    res += p * (1 - np) * (i + dp(N - i - 1));
    p *= np;
  }
  res += p * N;
  return m1[N] = true, m2[N] = res;
}

ld solve(int N) { return dp(N); }

int main() {
  int N;
  cout.precision(10);
  while (cin >> N, N) {
    cout << solve(N) << endl;
  }
}
Problem p00642 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 100001;

int main() {

  double ret[MAX_N];
  fill(ret, ret + MAX_N, 0.0);

  // iツ嘉アツ姪堋づ泳ツ古つ連ツ堕アツつオツづ偲ヲツづェツづゥツ確ツ猟ヲツづーツ格ツ納
  vector<vector<double>> dp;
  vector<double> v(51, 0.0);
  for (int i = 0; i < MAX_N; i++) {
    dp.push_back(v);
  }
  // double dp[MAX_N][51];
  // for(int i = 0; i < MAX_N; i++)
  //	fill(dp[i],dp[i]+51,0.0);
  dp[1][1] = 1.0;
  ret[1] = 1.0;
  for (int i = 2; i < MAX_N; i++) {
    double ss = 0;
    // iツ嘉アツ姪堋づ泳ツ古つ連ツ堕アツつオツづ偲ヲツづェツづゥツ確ツ猟ヲツづーツ計ツ算
    for (int j = 0; j <= min(i, 50); j++) {
      // ツ債。ツ嘉アツミツス
      if (j == 0) {
        for (int k = 1; k <= min(i - 1, 50); k++) {
          dp[i][j] += dp[i - 1][k] * (1 - pow(0.5, k));
        }
      }
      // ツ連ツ堕アツ偲ヲツ督セ
      else {
        dp[i][j] = dp[i - 1][j - 1] * (pow(0.5, j - 1));
        ss += dp[i][j];
      }
    }
    ret[i] = ss + ret[i - 1];
  }

  int n;
  while (cin >> n && n != 0) {
    // cout<<ret[n]<<endl;
    printf("%.3lf\\n", ret[n]);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 100001;

int main() {

  double ret[MAX_N];
  fill(ret, ret + MAX_N, 0.0);

  // iツ嘉アツ姪堋づ泳ツ古つ連ツ堕アツつオツづ偲ヲツづェツづゥツ確ツ猟ヲツづーツ格ツ納
  vector<vector<double>> dp;
  vector<double> v(51, 0.0);
  for (int i = 0; i < MAX_N; i++) {
    dp.push_back(v);
  }
  // double dp[MAX_N][51];
  // for(int i = 0; i < MAX_N; i++)
  //	fill(dp[i],dp[i]+51,0.0);
  dp[1][1] = 1.0;
  ret[1] = 1.0;
  for (int i = 2; i < MAX_N; i++) {
    double ss = 0;
    // iツ嘉アツ姪堋づ泳ツ古つ連ツ堕アツつオツづ偲ヲツづェツづゥツ確ツ猟ヲツづーツ計ツ算
    for (int j = 0; j <= min(i, 50); j++) {
      // ツ債。ツ嘉アツミツス
      if (j == 0) {
        for (int k = 1; k <= min(i - 1, 50); k++) {
          dp[i][j] += dp[i - 1][k] * (1 - pow(0.5, k));
        }
      }
      // ツ連ツ堕アツ偲ヲツ督セ
      else {
        dp[i][j] = dp[i - 1][j - 1] * (pow(0.5, j - 1));
      }
      ss += dp[i][j];
    }
    ret[i] = ss + ret[i - 1];
  }

  int n;
  while (cin >> n && n != 0) {
    double sum = 0;
    for (int j = 1; j <= n; j++) {
      for (int i = 1; i <= j; i++) {
        sum += dp[j][i];
      }
    }
    cout << sum << endl;
  }

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

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
const int N = 100000, ONE_KUURU = 12;

vector<double> E;

void init() {
  E.assign(N + 1, 0.0);
  vector<vector<double>> dp(N + 2, vector<double>(ONE_KUURU + 1));
  dp[1][0] = 1.0;
  for (int i = 1; i <= N; ++i) {
    E[i] += E[i - 1];
    rep(j, ONE_KUURU) {
      dp[i + 1][j + 1] += dp[i][j] * (1.0 / (1 << j));
      dp[i + 1][0] += dp[i][j] * (1.0 - 1.0 / (1 << j));
      E[i] += dp[i][j] * (1.0 / (1 << j));
    }
  }
}

int main() {
  init();
  for (int n; cin >> n, n; cout << fixed << setprecision(9) << E[n] << '\\n')
    ;
  return 0;
}

Error: WA

Fixed submission:

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

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
const int N = 10, ONE_KUURU = 12;

vector<double> E;

void init() {
  E.assign(N + 1, 0.0);
  vector<vector<double>> dp(N + 2, vector<double>(ONE_KUURU + 1));
  dp[1][0] = 1.0;
  for (int i = 1; i <= N; ++i) {
    E[i] += E[i - 1];
    rep(j, ONE_KUURU) {
      dp[i + 1][j + 1] += dp[i][j] * (1.0 / (1 << j));
      dp[i + 1][0] += dp[i][j] * (1.0 - 1.0 / (1 << j));
      E[i] += dp[i][j] * (1.0 / (1 << j));
    }
  }
}

int main() {
  init();
  for (int n; cin >> n, n; cout << fixed << setprecision(9) << E[n] << '\\n')
    ;
  return 0;
}