Problem p00642 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

#define MAX 100001
long double ans[MAX];
long double *memo, *prev;
long double buff[2][110];
long double pp[110];

int main() {
  memo = buff[0];
  prev = buff[1];

  prev[1] = 1.0;
  ans[1] = 1.0;
  REP(i, 110) pp[i] = pow(1.0 / 2.0, i);
  rep(i, 2, MAX) {
    // printf("%d\\n",i);
    if (i > 3000) {
      ans[i] = ans[i - 1] + 0.621446217;
    } else {
      ans[i] = ans[i - 1];
      memo[0] = 0.0;
      rep(j, 1, 110) {
        double tmp = prev[j - 1] * pp[j - 1];
        memo[j] = tmp;
        memo[0] += prev[j - 1] - tmp;
        ans[i] += tmp;
      }
      swap(prev, memo);
    }
    /*
    if(i < 5){
      REP(j,4) printf("%.3f ",(double)memo[j]);
      puts("");
    }
    */
  }

  int n;
  while (n = getInt())
    printf("%.8f\\n", (double)ans[n]);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\\n", x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};

int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

#define MAX 100001
double ans[MAX];
double memo[MAX][110];
double pp[110];

int main() {
  memo[1][1] = 1.0;
  ans[1] = 1.0;
  REP(i, 110) pp[i] = pow(1.0 / 2.0, i);
  rep(i, 2, MAX) {
    // printf("%d\\n",i);
    ans[i] = ans[i - 1];
    rep(j, 1, 40) {
      double tmp = memo[i - 1][j - 1] * pp[j - 1];
      memo[i][j] = tmp;
      memo[i][0] += tmp;
      ans[i] += tmp;
    }
  }
  /*
  REP(i,4) {
    REP(j,4)
      printf("%.3f ",memo[i][j]);
    puts("");
  }
  */
  int n;
  while (n = getInt())
    printf("%.8f\\n", ans[n]);
  return 0;
}