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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;

double EPS = 1e-10;

bool used[30];
ll d[30], w[30];
int n;

int main() {
  ll sum;
  bool f;

  while (cin >> n && n) {
    rep(i, n) cin >> d[i] >> w[i], used[i] = false;
    sum = 0LL;
    rep(i, n) sum += d[i];
    f = true;
    for (int i = 0; i < n; i++) {
      int idx = -1;
      for (int j = 0; j < n; j++) {
        if (!used[j]) {
          if (w[j] >= sum) {
            if (idx == -1)
              idx = j;
            else if (d[idx] < d[j])
              idx = j;
          }
        }
      }
      if (idx == -1) {
        f = false;
        break;
      }
      sum -= d[idx];
      used[idx] = true;
    }
    cout << (f ? "Yes" : "No") << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;

double EPS = 1e-10;

// bool used[1<<25+10];
int d[30], w[30];
int n;

set<pii> st;

bool calc(int past, int sum) {
  // if(used[past]) return true;
  if (st.count(mp(past, sum)))
    return true;
  st.insert(mp(past, sum));
  if (past == (1 << n) - 1)
    return true;
  bool ret = false;
  for (int i = 0; i < n; i++) {
    if (past & 1 << i)
      continue;
    if (sum + d[i] <= w[i])
      ret |= calc(past | 1 << i, sum + d[i]);
  }
  return ret;
}

int main() {
  while (cin >> n && n) {
    // for(int i = 0; i < 1<<25+10; i++) used[i]=0;
    st.clear();
    rep(i, n) cin >> d[i] >> w[i];
    if (calc(0, 0))
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}
Problem p00638 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 100000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x % (mod))

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool debug = false;

/*---------------------------------------------------*/

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    pi data[30];
    rep(i, n) cin >> data[i].second >> data[i].first;
    sort(data, data + n);
    bool ok = true;
    int sum = 0;
    rep(i, n) {
      if (sum + data[i].second <= data[i].first)
        sum += data[i].second;
      else
        ok = false;
    }
    if (ok)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 100000000
#define LINF 9000000000000000000
#define mod 1000000007

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x % (mod))

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool debug = false;

/*---------------------------------------------------*/

int n;
int weight[30], max_weight[30];

bool DFS(int now, int cnt, bool used[]) {
  // cout<<"DEBUG: "<<now<<" "<<cnt<<endl;
  bool ok = false;
  if (cnt == n)
    return true;
  rep(i, n) {
    if (!used[i] && now + weight[i] <= max_weight[i]) {
      used[i] = true;
      ok = DFS(now + weight[i], cnt + 1, used);
      if (ok)
        return true;
      used[i] = false;
    }
  }
  return false;
}

int main() {
  bool used[30];
  while (cin >> n) {
    if (n == 0)
      break;
    rep(i, n) used[i] = false;
    rep(i, n) cin >> weight[i] >> max_weight[i];
    bool ok = false;
    rep(i, n) {
      used[i] = true;
      if (DFS(weight[i], 1, used))
        ok = true;
      used[i] = false;
    }
    if (ok)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
}
Problem p00638 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
  int n;
  int x[25], y[25];
  while (1) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
    }
    int a[25] = {0};
    int min = 10000;
    int b = -1;
    int count = 0;
    int ans = 1;
    for (int i = 0; i < n; i++) {
      min = 10000;
      b = -1;
      for (int j = 0; j < n; j++) {
        if (y[j] < min && a[j] == 0) {
          b = j;
          min = y[j];
        }
      }
      if (count + x[b] > y[b]) {
        ans = 0;
        break;
      }
      count += x[b];
      a[b] = 1;
    }
    if (ans == 0) {
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int dfs(int n, int m[2][25], int a[25], int N, int count) {
  //  for(int i=0;i<n;i++){
  //    cout << a[i];
  //  }cout << endl;
  if (N == n) {
    return 1;
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == 0) {
      if (count + m[0][i] > m[1][i]) {
        break;
      }
      a[i] = 1;
      if (dfs(n, m, a, N + 1, count + m[0][i]) == 1) {
        return 1;
      } else {
        a[i] = 0;
      }
    }
  }
  return 0;
}

int main() {
  int n;
  int m[2][25];
  while (1) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> m[0][i] >> m[1][i];
    }
    int a[25] = {0};
    if (dfs(n, m, a, 0, 0) == 1) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }
  return 0;
}
Problem p00638 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)

pair<int, int> p[25];
int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    REP(i, n) cin >> p[i].second >> p[i].first;
    sort(p, p + n);

    long long total = 0;
    REP(i, n) {
      total += p[i].second;
      if (total > p[i].first) {
        cout << "No" << endl;
        break;
      }
      if (i == n - 1)
        cout << "Yes" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <array>
#include <bitset>
#include <cstdint>
#include <iostream>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; ++i)

int bridges[25];
int treasures[25];
array<bool, 1 << 25> pattern = {};

int n;

bool recur(uint32_t islands, int w) {
  if (pattern[islands])
    return false;
  pattern[islands] = true;

  if (islands == 0)
    return true;
  REP(i, n) {
    if (!(islands & (1 << i)))
      continue;
    if (w + treasures[i] > bridges[i])
      continue;
    if (recur(islands ^ (1 << i), w + treasures[i]))
      return true;
  }
  return false;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    REP(i, n) cin >> treasures[i] >> bridges[i];

    pattern.fill(false);
    cout << (recur((1 << n) - 1, 0) ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p00638 (C++) — buggy submission, verdict: Runtime Error

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

struct ISLAND {
  int treasure;
  int bridge;
};

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    vector<ISLAND> island;
    ISLAND temp;
    for (int i = 0; i < n; i++) {
      cin >> temp.treasure >> temp.bridge;
      island.push_back(temp);
    }

    // bridge基準でバブルソート、bridge小さいほうが若く
    for (int i = 0; i < n - 1; i++) {
      for (int j = n - 1; j > i; j--) {
        if (island[j].bridge < island[j - 1].bridge) {
          ISLAND temp;
          temp = island[j];
          island[j] = island[j - 1];
          island[j - 1] = temp;
        }
      }
    }

    int burden = 0;
    bool flag = true;
    for (int i = 0; i < n; i++) {
      burden += island[i].treasure;
      if (burden > island[i].bridge) {
        cout << "No" << endl;
        flag = false;
        break;
      }
    }
    if (flag) {
      cout << "Yes" << endl;
    }
  }

  return 0;
}

Error: -11

Fixed submission:

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

struct ISLAND {
  int treasure;
  int bridge;
};

int main() {
  int n;
  while (cin >> n) {
    vector<ISLAND> island;
    ISLAND temp;
    for (int i = 0; i < n; i++) {
      cin >> temp.treasure >> temp.bridge;
      island.push_back(temp);
    }

    // bridge基準でバブルソート、bridge小さいほうが若く
    for (int i = 0; i < n - 1; i++) {
      for (int j = n - 1; j > i; j--) {
        if (island[j].bridge < island[j - 1].bridge) {
          ISLAND temp;
          temp = island[j];
          island[j] = island[j - 1];
          island[j - 1] = temp;
        }
      }
    }

    int burden = 0;
    for (int i = 0; i < n; i++) {
      burden += island[i].treasure;
      if (burden > island[i].bridge) {
        cout << "No" << endl;
        break;
      }
    }
    if (burden <= island[n - 1].bridge) {
      cout << "Yes" << endl;
    }
  }

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

#include <stdio.h>

typedef struct {
  int gold;
  int wight;
} ISLAND;

int island_count;

int main(void) {
  ISLAND island[25];
  int i;
  int j;
  int have;

  while (1) {
    scanf("%d", &island_count);

    if (island_count == 0) {
      break;
    }
    for (i = 0; i < island_count; i++) {
      scanf("%d%d", &island[i].gold, &island[i].wight);
    }

    for (i = 0; i < island_count; i++) {
      for (j = i + 1; j < island_count; j++) {
        if (island[i].wight > island[j].wight) {
          ISLAND temp;
          temp = island[i];
          island[i] = island[j];
          island[j] = temp;
        }
      }
    }

    have = 0;
    for (i = 0; i < island_count; i++) {
      have += island[i].gold;
      if (have > island[i].wight) {
        break;
      }
    }

    if (i == island_count) {
      printf("Yes\\n");
    } else {
      puts("No");
    }
  }

  return (0);
}

Error: TLE

Fixed submission:

#include <stdio.h>

typedef struct {
  int gold;
  int wight;
} ISLAND;

int island_count;
int ans;

int is_pass(int depth, unsigned int pass_flag, int have, int is,
            ISLAND island[]) {
  int i;

  if (is != -1) {
    have += island[is].gold;
  }
  if (have > island[is].wight) {
    return (0);
  }

  if (depth == island_count) {
    ans = 1;
    return (1);
  }

  for (i = 0; i < island_count; i++) {
    if (((pass_flag >> i) & 1) == 0) {
      if (is_pass(depth + 1, (pass_flag | (1 << i)), have, i, island) == 1)
        return (1);
    }
  }

  return (0);
}

int main(void) {
  ISLAND island[25];
  int i;

  while (1) {
    ans = 0;
    scanf("%d", &island_count);
    if (island_count == 0) {
      break;
    }
    for (i = 0; i < island_count; i++) {
      scanf("%d%d", &island[i].gold, &island[i].wight);
    }

    is_pass(0, 0, 0, -1, island);

    if (ans == 1) {
      printf("Yes\\n");
    } else {
      printf("No\\n");
    }
  }

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

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int N, W, H, Q, x, y, c[109], xa[109][109], ya[109][109], xb[109][109],
    yb[109][109];
string s[109], t[109][109], tp;
int main() {
  while (cin >> N, N) {
    cin >> W >> H;
    for (int i = 0; i < N; i++) {
      cin >> s[i] >> c[i];
      for (int j = 0; j < c[i]; j++) {
        cin >> xa[i][j] >> ya[i][j] >> xb[i][j] >> yb[i][j] >> t[i][j];
      }
    }
    cin >> Q;
    vector<int> res;
    res.push_back(0);
    int pos = 0;
    for (int i = 0; i < Q; i++) {
      cin >> tp;
      if (tp == "click") {
        cin >> x >> y;
        for (int j = 0; j < c[res[pos]]; j++) {
          if (xa[res[pos]][j] <= x && x <= xb[res[pos]][j] &&
              ya[res[pos]][j] <= y && y <= yb[res[pos]][j]) {
            int ptr = -1;
            for (int k = 0; k < N; k++) {
              if (s[k] == t[res[pos]][j])
                ptr = k;
            }
            res.resize(pos + 2);
            res[++pos] = ptr;
            break;
          }
        }
      }
      if (tp == "show")
        cout << s[res[pos]] << endl;
      if (tp == "forward" && pos + 1 != res.size())
        pos++;
      if (tp == "back" && pos != 0)
        pos--;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int N, W, H, Q, x, y, c[109], xa[109][109], ya[109][109], xb[109][109],
    yb[109][109];
string s[109], t[109][109], tp;
int main() {
  while (cin >> N, N) {
    cin >> W >> H;
    for (int i = 0; i < N; i++) {
      cin >> s[i] >> c[i];
      for (int j = 0; j < c[i]; j++) {
        cin >> xa[i][j] >> ya[i][j] >> xb[i][j] >> yb[i][j] >> t[i][j];
      }
    }
    cin >> Q;
    vector<int> res;
    res.push_back(0);
    int pos = 0;
    for (int i = 0; i < Q; i++) {
      cin >> tp;
      if (tp == "click") {
        cin >> x >> y;
        for (int j = 0; j < c[res[pos]]; j++) {
          if (xa[res[pos]][j] <= x && x <= xb[res[pos]][j] &&
              ya[res[pos]][j] <= y && y <= yb[res[pos]][j]) {
            int ptr = -1;
            for (int k = 0; k < c[res[pos]]; k++) {
              if (s[k] == t[res[pos]][j])
                ptr = k;
            }
            res.resize(pos + 2);
            res[++pos] = ptr;
            break;
          }
        }
      }
      if (tp == "show")
        cout << s[res[pos]] << endl;
      if (tp == "forward" && pos + 1 != res.size())
        pos++;
      if (tp == "back" && pos != 0)
        pos--;
    }
  }
  return 0;
}
Problem p00640 (C++) — buggy submission, verdict: Runtime Error

// AOJ1054 Distorted Love

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  int buf[1000000];
  string name[100];
  int b[100], x1[100][100], y1[100][100], x2[100][100], y2[100][100],
      ptr[100][100];
  string link[100][100];
  while (cin >> n, n) {
    int x, y;
    cin >> x >> y;
    int cur = 0, size = 0;
    buf[cur] = 0;
    for (int i = 0; i < n; i++) {
      cin >> name[i] >> b[i];
      for (int j = 0; j < b[i]; j++)
        cin >> x1[i][j] >> y1[i][j] >> x2[i][j] >> y2[i][j] >> link[i][j];
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < b[j]; k++)
          if (link[j][k] == name[i])
            ptr[j][k] = i;
    cin >> n;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      if (s == "click") {
        cin >> x >> y;
        for (int j = 0; j < b[buf[cur]]; j++) {
          if (x1[buf[cur]][j] <= x && x <= x2[buf[cur]][j] &&
              y1[buf[cur]][j] <= y && y <= y2[buf[cur]][j]) {
            cur++;
            size = cur;
            buf[cur] = ptr[buf[cur - 1]][j];
            break;
          }
        }
      }
      if (s == "forward" && cur != size)
        cur++;
      if (s == "back" && cur != 0)
        cur--;
      if (s == "show")
        cout << name[buf[cur]] << endl;
    }
  }
}

Error: 0

Fixed submission:

// AOJ1054 Distorted Love

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  int buf[100];
  string name[100];
  int b[100], x1[100][100], y1[100][100], x2[100][100], y2[100][100],
      ptr[100][100];
  string link[100][100];
  while (cin >> n, n) {
    int x, y;
    cin >> x >> y;
    int cur = 0, size = 0;
    buf[cur] = 0;
    for (int i = 0; i < n; i++) {
      cin >> name[i] >> b[i];
      for (int j = 0; j < b[i]; j++)
        cin >> x1[i][j] >> y1[i][j] >> x2[i][j] >> y2[i][j] >> link[i][j];
    }
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        for (int k = 0; k < b[j]; k++)
          if (link[j][k] == name[i])
            ptr[j][k] = i;
    cin >> n;
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      if (s == "click") {
        cin >> x >> y;
        for (int j = 0; j < b[buf[cur]]; j++) {
          if (x1[buf[cur]][j] <= x && x <= x2[buf[cur]][j] &&
              y1[buf[cur]][j] <= y && y <= y2[buf[cur]][j]) {
            cur++;
            size = cur;
            buf[cur] = ptr[buf[cur - 1]][j];
            break;
          }
        }
      }
      if (s == "forward" && cur != size)
        cur++;
      if (s == "back" && cur != 0)
        cur--;
      if (s == "show")
        cout << name[buf[cur]] << endl;
    }
  }
}
Problem p00641 (C++) — buggy submission, verdict: Runtime Error

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

int done[100000];
vector<vector<int>> g;
vector<vector<int>> g_c;
int bfs(int x) {
  queue<int> Q;
  vector<int> vec;
  Q.push(x);
  while (Q.size()) {
    int q = Q.front();
    Q.pop();
    if (done[q])
      continue;
    else
      done[q] = true;
    for (int i = 0; i < g[q].size(); i++) {
      Q.push(g[q][i]);
      vec.push_back(g_c[q][i]);
    }
  }
  // cout << x << " " << vec.size() << endl;
  sort(vec.begin(), vec.end());
  int answer = 0;
  for (int i = 0; i < vec.size(); i++) {
    if (vec[i] == vec.back())
      answer++;
  }
  return answer / 2;
}

int main() {
  int n;
  while (cin >> n && n) {
    for (int i = 0; i < 100000; i++)
      done[i] = 0;
    g.clear();
    g_c.clear();
    g.resize(n);
    g_c.resize(n);
    for (int i = 0; i < n; i++) {

      for (int j = 0; j < 2; j++) {
        int a, b;
        cin >> a >> b;
        g[i].push_back(a);
        g_c[i].push_back(b);
      }
    }
    long long ans = 1;
    for (int i = 0; i < n; i++) {
      if (done[i] == 0) {
        ans *= bfs(i);
        ans %= 10007;
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

int done[100000];
vector<vector<int>> g;
void dfs(int x) {
  if (done[x])
    return;
  else
    done[x] = true;
  for (int i = 0; i < g[x].size(); i++) {
    dfs(g[x][i]);
  }
}

int main() {
  int n;
  while (cin >> n && n) {
    for (int i = 0; i < 100000; i++)
      done[i] = 0;
    g.resize(n);
    for (int i = 0; i < n; i++) {

      for (int j = 0; j < 2; j++) {
        int a, b;
        cin >> a >> b;
        g[i].push_back(a);
      }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (done[i] == 0) {
        dfs(i);
        ans++;
      }
    }
    cout << ans % 10007 << endl;
  }
}
Problem p00642 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

const int M = 20;
double p[M];
double pp[M];

const int N = 100010;
double dp[N];
bool vis[N];

int n;
double dfs(int d) {
  if (vis[d])
    return dp[d];
  if (d == n)
    return 0;

  double ret = 0;
  for (int j = 1; j < M - 1; ++j) {
    if (d + j + 1 <= n)
      ret += p[j] * (1 - pp[j + 1]) * (j + dfs(d + j + 1));
    if (d + j == n)
      ret += p[j] * (j + dfs(d + j));
  }

  vis[d] = true;
  return dp[d] = ret;
}

int main() {
  p[1] = 1;
  pp[1] = 1;
  for (int i = 2; i < M; ++i) {
    pp[i] = pp[i - 1] / 2;
    p[i] = p[i - 1] * pp[i];
  }

  while (cin >> n, n) {
    fill(vis, vis + N, false);
    printf("%.10f\\n", dfs(0));
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

const int M = 40;
double p[M];
double pp[M];

const int N = 100010;
double dp[N];
bool vis[N];

int n;
double dfs(int d) {
  // if(vis[d]) return dp[d];
  if (d == n)
    return 0;

  double ret = 0;
  for (int j = 1; j < M - 1; ++j) {
    if (d + j + 1 <= n)
      ret += p[j] * (1 - pp[j + 1]) * (j + dfs(d + j + 1));
    if (d + j == n)
      ret += p[j] * (j + dfs(d + j));
  }

  vis[d] = true;
  return dp[d] = ret;
}

int main() {
  p[1] = 1;
  pp[1] = 1;
  for (int i = 2; i < M; ++i) {
    pp[i] = pp[i - 1] / 2;
    p[i] = p[i - 1] * pp[i];
  }

  while (cin >> n, n) {
    // fill(vis,vis+N,false);
    printf("%.10f\\n", dfs(0));
  }
  return 0;
}