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

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
const double EPS = 1e-8;
typedef vector<double> vec;
typedef vector<vec> mat;
inline double ABS(double a) { return max(a, -a); }
vec gauss_jordan(const mat &A, const vec &b) {
  int n = A.size();
  mat B(n, vec(n + 1));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      B[i][j] = A[i][j];
  for (int i = 0; i < n; i++)
    B[i][n] = b[i];
  for (int i = 0; i < n; i++) {
    int pivot = i;
    for (int j = i; j < n; j++) {
      if (ABS(B[j][i]) > ABS(B[pivot][i]))
        pivot = j;
    }
    swap(B[i], B[pivot]);
    if (ABS(B[i][i]) < EPS)
      return vec();
    for (int j = i + 1; j <= n; j++)
      B[i][j] /= B[i][i];
    for (int j = 0; j < n; j++) {
      if (i != j) {
        for (int k = i + 1; k <= n; k++)
          B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }
  vec x(n);
  for (int i = 0; i < n; i++)
    x[i] = B[i][n];
  return x;
}
double p[12][120];
double dp[110000];
int main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);

  if (a < 0)
    a = -a;
  if (b == 1) {
    if (a % c != 0) {
      printf("-1\\n");
      return 0;
    } else {
      a /= c;
      c = 1;
    }
  }
  p[0][0] = 1;
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < 100; j++) {
      for (int k = 1; k <= b; k++) {
        p[i + 1][j + k] += p[i][j] / b;
      }
    }
  }
  int n = b * c;
  mat A(n, vec(n));
  vec B(n);
  for (int i = 0; i < n; i++) {
    A[i][i] = 1;

    if (i == 0)
      continue;
    B[i] = 1;
    for (int j = c; j <= b * c; j++) {
      int to = i - j;
      if (to < 0)
        to = -to;
      A[i][to] -= p[c][j];
    }
  }
  vec x = gauss_jordan(A, B);
  for (int i = 0; i < n; i++)
    dp[i] = x[i];
  for (int i = n; i <= 100000; i++) {
    dp[i] = 1;
    for (int j = c; j <= b * c; j++) {
      dp[i] += dp[i - j] * p[c][j];
    }
  }
  if (a <= 100000)
    printf("%.12f\\n", dp[a]);
  else {
    double M = (c + b * c) * 0.5;
    printf("%.12f\\n", dp[100000] + (a - 100000) / M);
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
const double EPS = 1e-8;
typedef vector<double> vec;
typedef vector<vec> mat;
inline double ABS(double a) { return max(a, -a); }
vec gauss_jordan(const mat &A, const vec &b) {
  int n = A.size();
  mat B(n, vec(n + 1));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      B[i][j] = A[i][j];
  for (int i = 0; i < n; i++)
    B[i][n] = b[i];
  for (int i = 0; i < n; i++) {
    int pivot = i;
    for (int j = i; j < n; j++) {
      if (ABS(B[j][i]) > ABS(B[pivot][i]))
        pivot = j;
    }
    swap(B[i], B[pivot]);
    if (ABS(B[i][i]) < EPS)
      return vec();
    for (int j = i + 1; j <= n; j++)
      B[i][j] /= B[i][i];
    for (int j = 0; j < n; j++) {
      if (i != j) {
        for (int k = i + 1; k <= n; k++)
          B[j][k] -= B[j][i] * B[i][k];
      }
    }
  }
  vec x(n);
  for (int i = 0; i < n; i++)
    x[i] = B[i][n];
  return x;
}
double p[12][120];
double dp[110000];
int main() {
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);

  if (a < 0)
    a = -a;
  if (b == 1) {
    if (a % c != 0) {
      printf("-1\\n");
    } else {
      a /= c;
      c = 1;
    }
  }
  p[0][0] = 1;
  for (int i = 0; i < c; i++) {
    for (int j = 0; j < 100; j++) {
      for (int k = 1; k <= b; k++) {
        p[i + 1][j + k] += p[i][j] / b;
      }
    }
  }
  int n = b * c;
  mat A(n, vec(n));
  vec B(n);
  for (int i = 0; i < n; i++) {
    A[i][i] = 1;

    if (i == 0)
      continue;
    B[i] = 1;
    for (int j = c; j <= b * c; j++) {
      int to = i - j;
      if (to < 0)
        to = -to;
      A[i][to] -= p[c][j];
    }
  }
  vec x = gauss_jordan(A, B);
  for (int i = 0; i < n; i++)
    dp[i] = x[i];
  for (int i = n; i <= 100000; i++) {
    dp[i] = 1;
    for (int j = c; j <= b * c; j++) {
      dp[i] += dp[i - j] * p[c][j];
    }
  }
  if (a <= 100000)
    printf("%.12f\\n", dp[a]);
  else {
    double M = (c + b * c) * 0.5;
    printf("%.12f\\n", dp[100000] + (a - 100000) / M);
  }
}