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

#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps = 1e-9;
const int inf = 1e9;
typedef pair<int, int> P;
struct Point {
  double x, y;
  Point() {
    x = 0;
    y = 0;
  }
  Point(double d_x, double d_y) { x = d_x, y = d_y; }
  double operator*(Point obj) { return obj.x * x + obj.y * y; }
  double operator%(Point obj) { return obj.y * x - obj.x * y; }
  Point operator*(double b) {
    Point tmp;
    tmp.x = x * b;
    tmp.y = y * b;
    return tmp;
  }
  Point operator/(double b) {
    Point tmp;
    tmp.x = x / b;
    tmp.y = y / b;
    return tmp;
  }
  Point operator+(Point obj) {
    Point tmp;
    tmp.x = x + obj.x;
    tmp.y = y + obj.y;
    return tmp;
  }
  Point operator-() {
    Point tmp;
    tmp.x = -x;
    tmp.y = -y;
    return tmp;
  }
  Point operator-(Point obj) {
    Point tmp;
    tmp.x = x - obj.x;
    tmp.y = y - obj.y;
    return tmp;
  }
  Point operator-=(Point obj) {
    x -= obj.x;
    y -= obj.y;
    return *this;
  }
  Point operator+=(Point obj) {
    x += obj.x;
    y += obj.y;
    return *this;
  }
  Point operator/=(double b) {
    x = x / b;
    y = y / b;
    return *this;
  }
  Point operator*=(double b) {
    x = x * b;
    y = y * b;
    return *this;
  }
  double size() { return hypot(x, y); }
  Point unit() { return Point(x / size(), y / size()); }
  Point normal() { return Point(y, -x); }
  double atan() { return atan2(y, x); }
};

bool operator<(Point a, Point b) { return a.x != b.x ? a.x < b.x : a.y < b.y; }
bool operator>(Point a, Point b) { return b < a; }
bool operator<=(Point a, Point b) { return !(b < a); }
bool operator>=(Point a, Point b) { return !(a < b); }
bool operator==(Point a, Point b) { return (a - b).size() < eps; }
bool operator!=(Point a, Point b) { return !(a == b); }

bool equal(double a, double b) { return abs(a - b) < eps; }
double cross(Point a, Point b) { return a % b; }
double dot(Point a, Point b) { return a * b; }

int ccw(Point a, Point b, Point c) {
  b = b - a;
  c = c - a;
  if (b % c > 0)
    return +1;
  else if (b % c < 0)
    return -1;
  else if (b * c < 0)
    return +2;
  else if (b.size() < c.size())
    return -2;
  else
    return 0;
}

int n, m, k;
vector<int> h1(15);
vector<int> h2(15);

string cmp(string a, string b) {
  if (a.size() > b.size())
    return a;
  if (b.size() > a.size())
    return b;
  return max(a, b);
}

int main(int argc, char const *argv[]) {
  while (1) {
    cin >> n >> m >> k;
    if (!n)
      break;
    double dp[2][100005];
    memset(dp, 0.0, sizeof(dp));
    dp[0][0] = 1.0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < m * n + 1; j++)
        dp[i % 2][j] = 0;
      for (int prev = 0; prev <= n * m; prev++) {
        if (dp[(i + 1) % 2][prev] == 0)
          continue;
        for (int k = 1; k <= m; k++) {
          int nxt = prev + k;
          dp[i % 2][nxt] += dp[(i + 1) % 2][prev] * (double)(1.0 / m + 0.0);
        }
      }
    }
    cout << fixed << setprecision(9);
    double ans = 0.0;

    for (int j = 1; j <= n * m; j++) {
      if (j <= k) {
        dp[n % 2][k + 1] += dp[n % 2][j];
      } else {
        ans += dp[n % 2][j] * (double)(j - k);
      }
    }

    cout << ans << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
const int MOD = 1000000007;
const int INF = 1000000000;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const double eps = 1e-9;
const int inf = 1e9;
typedef pair<int, int> P;
struct Point {
  double x, y;
  Point() {
    x = 0;
    y = 0;
  }
  Point(double d_x, double d_y) { x = d_x, y = d_y; }
  double operator*(Point obj) { return obj.x * x + obj.y * y; }
  double operator%(Point obj) { return obj.y * x - obj.x * y; }
  Point operator*(double b) {
    Point tmp;
    tmp.x = x * b;
    tmp.y = y * b;
    return tmp;
  }
  Point operator/(double b) {
    Point tmp;
    tmp.x = x / b;
    tmp.y = y / b;
    return tmp;
  }
  Point operator+(Point obj) {
    Point tmp;
    tmp.x = x + obj.x;
    tmp.y = y + obj.y;
    return tmp;
  }
  Point operator-() {
    Point tmp;
    tmp.x = -x;
    tmp.y = -y;
    return tmp;
  }
  Point operator-(Point obj) {
    Point tmp;
    tmp.x = x - obj.x;
    tmp.y = y - obj.y;
    return tmp;
  }
  Point operator-=(Point obj) {
    x -= obj.x;
    y -= obj.y;
    return *this;
  }
  Point operator+=(Point obj) {
    x += obj.x;
    y += obj.y;
    return *this;
  }
  Point operator/=(double b) {
    x = x / b;
    y = y / b;
    return *this;
  }
  Point operator*=(double b) {
    x = x * b;
    y = y * b;
    return *this;
  }
  double size() { return hypot(x, y); }
  Point unit() { return Point(x / size(), y / size()); }
  Point normal() { return Point(y, -x); }
  double atan() { return atan2(y, x); }
};

bool operator<(Point a, Point b) { return a.x != b.x ? a.x < b.x : a.y < b.y; }
bool operator>(Point a, Point b) { return b < a; }
bool operator<=(Point a, Point b) { return !(b < a); }
bool operator>=(Point a, Point b) { return !(a < b); }
bool operator==(Point a, Point b) { return (a - b).size() < eps; }
bool operator!=(Point a, Point b) { return !(a == b); }

bool equal(double a, double b) { return abs(a - b) < eps; }
double cross(Point a, Point b) { return a % b; }
double dot(Point a, Point b) { return a * b; }

int ccw(Point a, Point b, Point c) {
  b = b - a;
  c = c - a;
  if (b % c > 0)
    return +1;
  else if (b % c < 0)
    return -1;
  else if (b * c < 0)
    return +2;
  else if (b.size() < c.size())
    return -2;
  else
    return 0;
}

int n, m, k;
vector<int> h1(15);
vector<int> h2(15);

string cmp(string a, string b) {
  if (a.size() > b.size())
    return a;
  if (b.size() > a.size())
    return b;
  return max(a, b);
}

int main(int argc, char const *argv[]) {
  while (1) {
    cin >> n >> m >> k;
    if (!n)
      break;
    double dp[15][10005];
    memset(dp, 0.0, sizeof(dp));
    dp[0][0] = 1.0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= n * m; j++) {
        for (int k = 1; k <= m; k++) {
          int nxt = j + k;
          dp[i][nxt] += dp[i - 1][j] * (double)(1.0 / m + 0.0);
        }
      }
    }
    cout << fixed << setprecision(9);
    double ans = 0.0;

    for (int j = 1; j <= n * m; j++) {
      if (j <= k) {
        dp[n][k + 1] += dp[n][j];
      } else {
        ans += dp[n][j] * (double)(j - k);
      }
    }

    cout << ans << endl;
  }
  return 0;
}