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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define supd 100001
#define SUPR 20
#define ZERO 1e-20

double p[supd]
        [SUPR]; // p[d][r] = d niti me, r ko renzoku site bentou totteru kauritu
double u[supd][SUPR]; // u[d][r] = d niti me, r ko renzoku site bentou totta
                      // toki, imamade no bentou no kitaiti

int solve() {
  FOR(d, 0, supd) FOR(r, 0, SUPR) p[d][r] = u[d][r] = 0;
  p[0][0] = 1;

  FOR(d, 1, supd) {
    FOR(r, 0, SUPR) {
      if (r == 0)
        FOR(k, 0, SUPR)
      p[d][r] += p[d - 1][k] * (1 - pow(0.5, k));
      else p[d][r] = p[d - 1][r - 1] * pow(0.5, r - 1);

      if (r == 0) {
        if (p[d][r] > ZERO)
          FOR(k, 0, SUPR)
        if (p[d - 1][k] > ZERO)
          u[d][r] += u[d - 1][k] * p[d - 1][k] * (1 - pow(0.5, k)) /
                     p[d][r]; // actually p((d,r)->(d-1,k)).   used "p(b->a) =
                              // p(a)*p(a->b)/p(b)."
      } else if (p[d - 1][r - 1] > ZERO)
        u[d][r] = u[d - 1][r - 1] + 1;

      // if(p[d][r]>ZERO) cout << "d:" << d << " r:" << r << " p:" << p[d][r] <<
      // " u:" << u[d][r] << endl;
    }
  }
}

double getres(int d) {
  double res = 0;
  FOR(r, 0, SUPR) if (p[d][r] > ZERO) res += u[d][r] * p[d][r];
  return res;
}

int main() {
  solve();
  while (true) {
    int d;
    scanf("%d", &d);
    if (d == 0)
      return 0;
    printf("%.8f\\n", getres(d));
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define supd 10 // 0001
#define SUPR 20
#define ZERO 1e-10

int main() {
  float p[supd][SUPR]; // p[d][r] = d niti me, r ko renzoku site bentou totteru
                       // kauritu
  float u[supd][SUPR]; // u[d][r] = d niti me, r ko renzoku site bentou totta
                       // toki, imamade no bentou no kitaiti

  FOR(d, 0, supd) FOR(r, 0, SUPR) p[d][r] = u[d][r] = 0;

  p[0][0] = 1;

  FOR(d, 1, supd) {
    FOR(r, 0, SUPR) {
      if (r == 0) {
        FOR(k, 0, SUPR) { p[d][r] += p[d - 1][k] * (1 - pow(0.5, k)); }
      } else {
        p[d][r] = p[d - 1][r - 1] * pow(0.5, r - 1);
      }

      if (r == 0) {
        FOR(k, 0, SUPR) {
          if (p[d - 1][k] > ZERO)
            if (p[d][r] > ZERO)
              u[d][r] += u[d - 1][k] * p[d - 1][k] * (1 - pow(0.5, k)) /
                         p[d][r]; // actually p((d,r)->(d-1,k)).   used "p(b->a)
                                  // = p(a)*p(a->b)/p(b)."
        }
      } else {
        if (p[d - 1][r - 1] > ZERO)
          u[d][r] = u[d - 1][r - 1] + 1;
      }

      // if(p[d][r]>ZERO) cout << "d:" << d << " r:" << r << " p:" << p[d][r] <<
      // " u:" << u[d][r] << endl;
    }
  }

  while (true) {
    int d;
    cin >> d;
    if (d == 0)
      return 0;
    float res = 0;
    FOR(r, 0, SUPR) if (p[d][r] > ZERO) res += u[d][r] * p[d][r];
    cout << res << endl;
  }
}