Problem p00865 (C++)

Buggy submission (status: Runtime Error):
// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int hist[2][500000];

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

  int N, M, K;
  while (cin >> N >> M >> K, N) {
    fill((int *)hist, (int *)hist + 2 * 500000, 0);
    int crt = 0, nxt = 1;
    for (int i = 1; i <= M; ++i)
      hist[crt][i] = 1;

    REP(i, N - 1) {
      fill(hist[nxt], hist[nxt] + 500000, 0);
      REP(j, 500000) {
        for (int k = 1; k <= M; ++k)
          hist[nxt][j + k] += hist[crt][j];
      }
      swap(crt, nxt);
    }

    double ans = 0., tot = pow(M, N);
    FOR(i, N, 500000) {
      if (hist[crt][i] == 0)
        break;
      if (i <= K)
        ans += hist[crt][i];
      else
        ans += hist[crt][i] * (i - K);
    }
    cout << fixed << setprecision(8) << ans / tot << endl;
  }

  return 0;
}

Fixed submission:
// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int hist[2][500000];

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

  int N, M, K;
  while (cin >> N >> M >> K, N) {
    fill((int *)hist, (int *)hist + 2 * 500000, 0);
    int crt = 0, nxt = 1;
    for (int i = 1; i <= M; ++i)
      hist[crt][i] = 1;

    REP(i, N - 1) {
      fill(hist[nxt], hist[nxt] + 500000, 0);
      REP(j, 500000) {
        if (hist[crt][j] == 0)
          continue;
        for (int k = 1; k <= M; ++k)
          hist[nxt][j + k] += hist[crt][j];
      }
      swap(crt, nxt);
    }

    double ans = 0., tot = pow(M, N);
    FOR(i, N, 500000) {
      if (hist[crt][i] == 0)
        break;
      if (i <= K)
        ans += hist[crt][i];
      else
        ans += hist[crt][i] * (i - K);
    }
    cout << fixed << setprecision(8) << ans / tot << endl;
  }

  return 0;
}