Problem p00642 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, p, b) for (int i = (p); i < (b); i++)
#define rrep(i, p, b) for (int i = (b)-1; i >= (p); i--)
#define all(p) (p).begin(), (p).end()
#define dump(o)                                                                \\
  { cerr << #o << " " << o << endl; }
#define dumpc(o)                                                               \\
  {                                                                            \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

#define MAX 100010
#define B 15

signed main() {
  cout << fixed << setprecision(8);
  static double dp[MAX][B] = {};
  dp[1][1] = 1;
  static double p[B] = {};
  static double ans[MAX] = {};
  double sum = 0;
  rep(i, 1, B) p[i] = pow(0.5, i - 1);
  rep(i, 1, MAX - 1) {
    rep(j, 1, B) {
      if (dp[i][j] == 0)
        continue;
      sum += dp[i][j] * p[j];
      dp[i + 1][1] += dp[i][j] * (1 - p[j]);
      dp[i + 1][j + 1] += dp[i][j] * p[j];
    }
    ans[i] = sum;
  }
  for (int n; cin >> n && n;) {
    cout << ans[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i, p, b) for (int i = (p); i < (b); i++)
#define rrep(i, p, b) for (int i = (b)-1; i >= (p); i--)
#define all(p) (p).begin(), (p).end()
#define dump(o)                                                                \\
  { cerr << #o << " " << o << endl; }
#define dumpc(o)                                                               \\
  {                                                                            \\
    cerr << #o;                                                                \\
    for (auto &e : (o))                                                        \\
      cerr << " " << e;                                                        \\
    cerr << endl;                                                              \\
  }
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

#define MAX 100010
#define B 15

signed main() {
  cout << fixed << setprecision(8);
  static double dp[MAX][B] = {};
  dp[1][1] = 1;
  static double p[B] = {};
  static double ans[MAX] = {};
  double sum = 0;
  rep(i, 1, B) p[i] = pow(0.5, i - 1);
  rep(i, 1, MAX) {
    rep(j, 1, B) {
      if (dp[i][j] == 0)
        continue;
      sum += dp[i][j] * p[j];
      dump(sum);
      dp[i + 1][1] += dp[i][j] * (1 - p[j]);
      dp[i + 1][j + 1] += dp[i][j] * p[j];
    }
    ans[i] = sum;
  }
  for (int n; cin >> n && n;) {
    cout << ans[n] << endl;
  }
  return 0;
}