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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#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;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define EACH(v, c) for (auto &v : c)

#define ITER(c) __typeof((c).begin())
#define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<VVD> VVVD;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cout.precision(4);
  cout << fixed;

  while (true) {
    int n;
    cin >> n;

    if (!n) {
      break;
    }

    VVD dp(n + 1, VD(11, 0.));
    dp[0][0] = 1;

    double res = 0;

    REP(i, 0, dp.size() - 1) {
      REP(j, 0, dp[i].size() - 1) {
        double p = 1 / pow(2, j);

        res += dp[i][j] * p;

        dp[i + 1][j + 1] += dp[i][j] * p;
        dp[i + 1][0] += dp[i][j] * (1 - p);
      }
    }

    cout << res << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#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;

typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define EACH(v, c) for (auto &v : c)

#define ITER(c) __typeof((c).begin())
#define IREP(c, it) for (ITER(c) it = c.begin(); it != c.end(); ++it)
#define ALL(c) (c).begin(), (c).end()

#define PB(n) push_back(n)
#define MP(a, b) make_pair((a), (b))
#define EXIST(c, e) ((c).find(e) != (c).end())

#define fst first
#define snd second

#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \\
  cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << (x) << endl

typedef vector<double> VD;
typedef vector<VD> VVD;
typedef vector<VVD> VVVD;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cout.precision(4);
  cout << fixed;

  while (true) {
    int n;
    cin >> n;

    if (!n) {
      break;
    }

    VVVD dp(n + 1, VVD(n + 1, VD(11, 0.)));
    dp[0][0][0] = 1;

    REP(i, 0, dp.size() - 1) {
      REP(j, 0, dp[i].size() - 1) {
        REP(k, 0, dp[i][j].size() - 1) {
          double p = 1 / pow(2, k);

          dp[i + 1][j + 1][k + 1] += dp[i][j][k] * p;
          dp[i + 1][j][0] += dp[i][j][k] * (1 - p);
        }
      }
    }

    double res = 0;

    REP(i, 0, dp[n].size()) {
      REP(j, 0, dp[n][i].size()) { res += dp[n][i][j] * i; }
    }

    cout << res << endl;
  }

  return 0;
}