Problem p01432 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

typedef ld Data;
typedef vector<Data> Array;
typedef vector<Array> Matrix;

bool is_zero(Data dat) { return (abs(dat) < EPS); }

Matrix operator-(Matrix mat) {
  REP(i, mat.size()) REP(j, mat[0].size()) mat[i][j] = -mat[i][j];
  return mat;
}

Matrix operator+(Matrix lhs, const Matrix &rhs) {
  REP(i, lhs.size()) REP(j, lhs[0].size()) lhs[i][j] = lhs[i][j] + rhs[i][j];
  return lhs;
}

Matrix operator-(Matrix lhs, const Matrix &rhs) {
  REP(i, lhs.size()) REP(j, lhs[0].size()) lhs[i][j] = lhs[i][j] - rhs[i][j];
  return lhs;
}

Matrix operator*(const Matrix &lhs, const Matrix &rhs) {
  Matrix res(lhs.size(), Array(rhs[0].size(), 0));
  REP(i, lhs.size())
  REP(j, rhs[0].size()) REP(k, rhs.size()) res[i][j] =
      fma(lhs[i][k], rhs[k][j], res[i][j]);
  return res;
}

Array operator+(const Array &lhs, const Array &rhs) {
  Array res = lhs;
  REP(i, rhs.size()) res[i] = res[i] + rhs[i];
  return res;
}

Array operator*(const Matrix &lhs, const Array &rhs) {
  Array res(lhs.size());
  REP(i, lhs.size())
  res[i] = inner_product(begin(lhs[i]), end(lhs[i]), begin(rhs), Data(0));
  return res;
}

Array operator*(Data scalar, const Array &rhs) {
  Array res(rhs.size());
  REP(i, rhs.size())
  res[i] = scalar * rhs[i];
  return res;
}

Matrix transpose(const Matrix &A) {
  const int n = A[0].size(), m = A.size();
  Matrix res(n, Array(m, 0));
  REP(i, n) REP(j, m) res[i][j] = A[j][i];
  return res;
}

Data inner_product_ex(const Array &a, const Array &b, const Matrix &m) {
  Array b_prime = m * b;
  return inner_product(begin(a), end(a), begin(b_prime), Data(0));
}

Data norm(const Array &a) {
  return inner_product(begin(a), end(a), begin(a), Data(0));
}

Data norm(const Array &a, const Matrix &M) { return inner_product_ex(a, a, M); }

int rankMat(Matrix A) {
  const int n = A.size(), m = A[0].size();
  int r = 0;
  for (int i = 0; r < n && i < m; ++i) {
    int pivot = r;
    for (int j = r + 1; j < n; ++j)
      if (abs(A[j][i]) > abs(A[pivot][i]))
        pivot = j;
    swap(A[pivot], A[r]);
    if (is_zero(A[r][i]))
      continue;
    for (int k = m - 1; k >= i; --k)
      A[r][k] = A[r][k] / A[r][i];
    for (int j = r + 1; j < n; ++j)
      for (int k = m - 1; k >= i; --k)
        A[j][k] = fma(-A[r][k], A[j][i], A[j][k]);
    ++r;
  }
  return r;
}

Data det(Matrix A) {
  const int n = A.size();
  Data D = Data(1);
  for (int i = 0; i < n; ++i) {
    int pivot = i;
    for (int j = i + 1; j < n; ++j)
      if (abs(A[j][i]) > abs(A[pivot][i]))
        pivot = j;
    swap(A[pivot], A[i]);
    D = D * A[i][i] * Data(i != pivot ? -1 : 1);
    if (is_zero(A[i][i]))
      break;
    for (int j = i + 1; j < n; ++j)
      for (int k = n - 1; k >= i; --k)
        A[j][k] = A[j][k] - A[i][k] * A[j][i] / A[i][i];
  }
  return D;
}

Array gauss_jordan(const Matrix &A, const Array &b) {
  int n = A.size();
  Matrix B(n, Array(n + 1));
  REP(i, n) REP(j, n) B[i][j] = A[i][j];
  REP(i, n) B[i][n] = b[i];
  REP(i, n) {
    int pivot = i;
    FOR(j, i, n) {
      if (abs(B[j][i]) > abs(B[pivot][i]))
        pivot = j;
    }
    swap(B[i], B[pivot]);

    if (abs(B[i][i]) < EPS)
      return Array();
    FOR(j, i + 1, n + 1) B[i][j] /= B[i][i];
    REP(j, n) {
      if (i != j) {
        FOR(k, i + 1, n + 1) B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }
  Array x(n);
  REP(i, n) x[i] = B[i][n];
  return x;
}

int mx;
ld dp[111111];
ld solve(int s, vector<ld> &exp) {
  if (s == 0)
    return 0;
  if (dp[s] >= 0)
    return dp[s];
  ld res = 1;
  FOR(i, 1, mx + 1) { res += solve(abs(s - i), exp) * exp[i]; }
  return dp[s] = res;
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  REP(i, 111111) dp[i] = 0;
  int s, n, k;
  cin >> s >> n >> k;
  s = abs(s);
  ld adv[11][111];
  MS(adv, 0);
  adv[0][0] = 1.0;
  REP(i, k) {
    REP(j, 100) {
      FOR(dice, 1, n + 1) { adv[i + 1][j + dice] += adv[i][j] / n; }
    }
  }
  vector<ld> v;
  REP(i, 101) v.push_back(adv[k][i]);
  ld exp = 0;
  REP(i, 111) exp += i * adv[k][i];
  ld ans = 0;
  if (s > 100000) {
    ans += (s - 100000) / exp;
    s = 100000;
  }
  mx = n * k;
  Matrix A(mx, Array(mx));
  Array b(mx);
  A[0][0] = 1;
  FOR(i, 1, mx) {
    FOR(j, 1, mx + 1) { A[i][abs(i - j)] += v[j]; }
  }
  FOR(i, 1, mx) {
    A[i][i] -= 1;
    b[i] = -1;
  }
  Array x = gauss_jordan(A, b);
  REP(i, mx) dp[i] = x[i];
  FOR(i, mx, s + 1) {
    REP(j, 101) {
      if (i - j < 0)
        break;
      dp[i] += dp[i - j] * v[j];
    }
    dp[i] += 1;
  }
  cout << dp[s] + ans << endl;
  // cout << solve(s, v) + ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

typedef ld Data;
typedef vector<Data> Array;
typedef vector<Array> Matrix;

bool is_zero(Data dat) { return (abs(dat) < EPS); }

Matrix operator-(Matrix mat) {
  REP(i, mat.size()) REP(j, mat[0].size()) mat[i][j] = -mat[i][j];
  return mat;
}

Matrix operator+(Matrix lhs, const Matrix &rhs) {
  REP(i, lhs.size()) REP(j, lhs[0].size()) lhs[i][j] = lhs[i][j] + rhs[i][j];
  return lhs;
}

Matrix operator-(Matrix lhs, const Matrix &rhs) {
  REP(i, lhs.size()) REP(j, lhs[0].size()) lhs[i][j] = lhs[i][j] - rhs[i][j];
  return lhs;
}

Matrix operator*(const Matrix &lhs, const Matrix &rhs) {
  Matrix res(lhs.size(), Array(rhs[0].size(), 0));
  REP(i, lhs.size())
  REP(j, rhs[0].size()) REP(k, rhs.size()) res[i][j] =
      fma(lhs[i][k], rhs[k][j], res[i][j]);
  return res;
}

Array operator+(const Array &lhs, const Array &rhs) {
  Array res = lhs;
  REP(i, rhs.size()) res[i] = res[i] + rhs[i];
  return res;
}

Array operator*(const Matrix &lhs, const Array &rhs) {
  Array res(lhs.size());
  REP(i, lhs.size())
  res[i] = inner_product(begin(lhs[i]), end(lhs[i]), begin(rhs), Data(0));
  return res;
}

Array operator*(Data scalar, const Array &rhs) {
  Array res(rhs.size());
  REP(i, rhs.size())
  res[i] = scalar * rhs[i];
  return res;
}

Matrix transpose(const Matrix &A) {
  const int n = A[0].size(), m = A.size();
  Matrix res(n, Array(m, 0));
  REP(i, n) REP(j, m) res[i][j] = A[j][i];
  return res;
}

Data inner_product_ex(const Array &a, const Array &b, const Matrix &m) {
  Array b_prime = m * b;
  return inner_product(begin(a), end(a), begin(b_prime), Data(0));
}

Data norm(const Array &a) {
  return inner_product(begin(a), end(a), begin(a), Data(0));
}

Data norm(const Array &a, const Matrix &M) { return inner_product_ex(a, a, M); }

int rankMat(Matrix A) {
  const int n = A.size(), m = A[0].size();
  int r = 0;
  for (int i = 0; r < n && i < m; ++i) {
    int pivot = r;
    for (int j = r + 1; j < n; ++j)
      if (abs(A[j][i]) > abs(A[pivot][i]))
        pivot = j;
    swap(A[pivot], A[r]);
    if (is_zero(A[r][i]))
      continue;
    for (int k = m - 1; k >= i; --k)
      A[r][k] = A[r][k] / A[r][i];
    for (int j = r + 1; j < n; ++j)
      for (int k = m - 1; k >= i; --k)
        A[j][k] = fma(-A[r][k], A[j][i], A[j][k]);
    ++r;
  }
  return r;
}

Data det(Matrix A) {
  const int n = A.size();
  Data D = Data(1);
  for (int i = 0; i < n; ++i) {
    int pivot = i;
    for (int j = i + 1; j < n; ++j)
      if (abs(A[j][i]) > abs(A[pivot][i]))
        pivot = j;
    swap(A[pivot], A[i]);
    D = D * A[i][i] * Data(i != pivot ? -1 : 1);
    if (is_zero(A[i][i]))
      break;
    for (int j = i + 1; j < n; ++j)
      for (int k = n - 1; k >= i; --k)
        A[j][k] = A[j][k] - A[i][k] * A[j][i] / A[i][i];
  }
  return D;
}

Array gauss_jordan(const Matrix &A, const Array &b) {
  int n = A.size();
  Matrix B(n, Array(n + 1));
  REP(i, n) REP(j, n) B[i][j] = A[i][j];
  REP(i, n) B[i][n] = b[i];
  REP(i, n) {
    int pivot = i;
    FOR(j, i, n) {
      if (abs(B[j][i]) > abs(B[pivot][i]))
        pivot = j;
    }
    swap(B[i], B[pivot]);

    if (abs(B[i][i]) < EPS)
      return Array();
    FOR(j, i + 1, n + 1) B[i][j] /= B[i][i];
    REP(j, n) {
      if (i != j) {
        FOR(k, i + 1, n + 1) B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }
  Array x(n);
  REP(i, n) x[i] = B[i][n];
  return x;
}

int mx;
ld dp[111111];
ld solve(int s, vector<ld> &exp) {
  if (s == 0)
    return 0;
  if (dp[s] >= 0)
    return dp[s];
  ld res = 1;
  FOR(i, 1, mx + 1) { res += solve(abs(s - i), exp) * exp[i]; }
  return dp[s] = res;
}

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  REP(i, 111111) dp[i] = 0;
  int s, n, k;
  cin >> s >> n >> k;
  s = abs(s);
  if (n == 1) {
    cout << s / k << endl;
    return 0;
  }
  ld adv[11][111];
  MS(adv, 0);
  adv[0][0] = 1.0;
  REP(i, k) {
    REP(j, 100) {
      FOR(dice, 1, n + 1) { adv[i + 1][j + dice] += adv[i][j] / n; }
    }
  }
  vector<ld> v;
  REP(i, 101) v.push_back(adv[k][i]);
  ld exp = 0;
  REP(i, 111) exp += i * adv[k][i];
  ld ans = 0;
  if (s > 100000) {
    ans += (s - 100000) / exp;
    s = 100000;
  }
  mx = n * k;
  Matrix A(mx, Array(mx));
  Array b(mx);
  A[0][0] = 1;
  FOR(i, 1, mx) {
    FOR(j, 1, mx + 1) { A[i][abs(i - j)] += v[j]; }
  }
  FOR(i, 1, mx) {
    A[i][i] -= 1;
    b[i] = -1;
  }
  Array x = gauss_jordan(A, b);
  REP(i, mx) dp[i] = x[i];
  FOR(i, mx, s + 1) {
    REP(j, 101) {
      if (i - j < 0)
        break;
      dp[i] += dp[i - j] * v[j];
    }
    dp[i] += 1;
  }
  cout << dp[s] + ans << endl;
  // cout << solve(s, v) + ans << endl;
  return 0;
}