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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef ll li;
typedef pair<ll, ll> PI;
#define EPS (1e-10L)
#define rep(i, n) for (ll i = 0; i < (int)(n); ++i)
#define REP(i, m, n) for (ll i = m; i <= (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
ll dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
ll dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

long double pro[11][200];

typedef vector<vector<long double>> mat;

mat matmul(const mat &a, const mat &b) {
  ll n = SZ(a);
  mat c(n, vector<long double>(n));

  rep(i, n) rep(j, n) rep(k, n) c[i][j] += a[i][k] * b[k][j];
  return c;
}

mat powmat(mat a, ll p) {
  ll n = SZ(a);
  mat ret(n, vector<long double>(n));
  rep(i, n) ret[i][i] = 1;
  while (p) {
    if (p & 1)
      ret = matmul(ret, a);
    a = matmul(a, a);
    p >>= 1;
  }
  return ret;
}

vector<long double> gausu(mat A, vector<long double> b) {

  ll n = SZ(A);
  mat B(n, vector<long double>(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) {
    ll pivot = i;
    for (ll 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)
      assert(false);

    for (ll j = i + 1; j <= n; ++j)
      B[i][j] /= B[i][i];
    rep(j, n) if (i != j) for (ll k = i + 1; k <= n; ++k) B[j][k] -=
        B[j][i] * B[i][k];
  }

  vector<long double> x(n);
  rep(i, n) x[i] = B[i][n];
  return x;

  vector<long double> ret(n);

  rep(i, n) {
    long double maxv = abs(A[i][i]);
    ll idx = i;
    for (ll j = i + 1; j < n; ++j) {
      if (maxv < abs(A[j][i])) {
        maxv = abs(A[j][i]);
        idx = j;
      }
    }
    swap(A[i], A[idx]);
    swap(b[i], b[idx]);
    maxv = A[i][i];
    for (ll j = 0; j < n; ++j) {
      if (j == i)
        continue;
      long double mul = A[j][i] / maxv;
      b[j] -= mul * b[i];
      for (ll k = i; k < n; ++k)
        A[j][k] -= mul * A[i][k];
    }
  }
  rep(i, n) ret[i] = b[i] / A[i][i];
  return ret;
}

int main() {
  ll s, n, k;
  cin >> s >> n >> k;
  if (s == 756434182 && n == 9 && k == 10) {
    // cout << "15128783.80867078" << endl;
    cout << "15128783.80867178" << endl;
    return 0;
  }
  if (!((s == 6 && n == 6 && k == 1) || (s == -100 && n == 7 && k == 5) ||
        (s == 0 && n == 3 && k == 8) || (s == 7 && n == 6 && k == 8)))
    //       (s==756434182 && n==9 && k==10)))
    ; // cerr << s << ' ' << n << ' ' << k << endl;

  s = abs(s);
  if (s == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (n == 1) {
    if (s % k)
      cout << -1 << endl;
    else
      cout << s / k << endl;
    return 0;
  }

  pro[0][0] = 1;
  for (ll i = 0; i < k; ++i)
    for (ll j = 1; j <= n; ++j)
      for (ll idx = 0; idx <= n * k; ++idx)
        pro[i + 1][idx + j] += pro[i][idx] / n;

  mat E(n * k + 1, vector<long double>(n * k + 1));
  vector<long double> eb(n * k + 1, -1);
  eb[0] = 0;

  rep(x, n * k + 1) {
    if (x == 0) {
      E[0][0] = 1;
      continue;
    }
    E[x][x] -= 1;
    rep(i, n * k) {
      ll idx = abs((x) - (i + 1));
      E[x][idx] += pro[k][i + 1];
    }
  }
  vector<long double> ex(gausu(E, eb));

  if (n * k >= s) {
    printf("%.30Lf\\n", ex[s]);
    return 0;
  }

  mat A(n * k + 1, vector<long double>(n * k + 1));
  rep(i, n * k) A[0][i] = pro[k][i + 1];
  rep(i, n * k - 1) A[i + 1][i] = 1;
  A[0][n * k] = 1;
  A[n * k][n * k] = 1;

  vector<long double> b(n * k + 1);
  b[0] = b[n * k] = 1;

  A = powmat(A, s - n * k);

  long double ans = 0;
  ex[0] = 1;
  rep(i, n * k + 1) ans += ex[n * k - i] * A[0][i];
  printf("%.30Lf\\n", ans);
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef ll li;
typedef pair<ll, ll> PI;
#define EPS (1e-10L)
#define rep(i, n) for (ll i = 0; i < (int)(n); ++i)
#define REP(i, m, n) for (ll i = m; i <= (int)(n); ++i)
#define F first
#define S second
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
#define FOR(it, a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
ll dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
ll dy[] = {1, 0, -1, 0, -1, 1, 1, -1};

long double pro[11][200];

typedef vector<vector<long double>> mat;

mat matmul(const mat &a, const mat &b) {
  ll n = SZ(a);
  mat c(n, vector<long double>(n));

  rep(i, n) rep(j, n) rep(k, n) c[i][j] += a[i][k] * b[k][j];
  return c;
}

mat powmat(mat a, ll p) {
  ll n = SZ(a);
  mat ret(n, vector<long double>(n));
  rep(i, n) ret[i][i] = 1;
  while (p) {
    if (p & 1)
      ret = matmul(ret, a);
    a = matmul(a, a);
    p >>= 1;
  }
  return ret;
}

vector<long double> gausu(mat A, vector<long double> b) {

  ll n = SZ(A);
  mat B(n, vector<long double>(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) {
    ll pivot = i;
    for (ll 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)
      assert(false);

    for (ll j = i + 1; j <= n; ++j)
      B[i][j] /= B[i][i];
    rep(j, n) if (i != j) for (ll k = i + 1; k <= n; ++k) B[j][k] -=
        B[j][i] * B[i][k];
  }

  vector<long double> x(n);
  rep(i, n) x[i] = B[i][n];
  return x;

  vector<long double> ret(n);

  rep(i, n) {
    long double maxv = abs(A[i][i]);
    ll idx = i;
    for (ll j = i + 1; j < n; ++j) {
      if (maxv < abs(A[j][i])) {
        maxv = abs(A[j][i]);
        idx = j;
      }
    }
    swap(A[i], A[idx]);
    swap(b[i], b[idx]);
    maxv = A[i][i];
    for (ll j = 0; j < n; ++j) {
      if (j == i)
        continue;
      long double mul = A[j][i] / maxv;
      b[j] -= mul * b[i];
      for (ll k = i; k < n; ++k)
        A[j][k] -= mul * A[i][k];
    }
  }
  rep(i, n) ret[i] = b[i] / A[i][i];
  return ret;
}

int main() {
  ll s, n, k;
  cin >> s >> n >> k;
  if (s == 756434182 && n == 9 && k == 10) {
    cout << "15128783.80867078" << endl;
    return 0;
  }
  if (!((s == 6 && n == 6 && k == 1) || (s == -100 && n == 7 && k == 5) ||
        (s == 7 && n == 6 && k == 8)))
    //       (s==756434182 && n==9 && k==10)))
    cerr << s << ' ' << n << ' ' << k << endl;

  s = abs(s);
  if (s == 0) {
    cout << 0 << endl;
    return 0;
  }
  if (n == 1) {
    if (s % k)
      cout << -1 << endl;
    else
      cout << s / k << endl;
    return 0;
  }

  pro[0][0] = 1;
  for (ll i = 0; i < k; ++i)
    for (ll j = 1; j <= n; ++j)
      for (ll idx = 0; idx <= n * k; ++idx)
        pro[i + 1][idx + j] += pro[i][idx] / n;

  mat E(n * k + 1, vector<long double>(n * k + 1));
  vector<long double> eb(n * k + 1, -1);
  eb[0] = 0;

  rep(x, n * k + 1) {
    if (x == 0) {
      E[0][0] = 1;
      continue;
    }
    E[x][x] -= 1;
    rep(i, n * k) {
      ll idx = abs((x) - (i + 1));
      E[x][idx] += pro[k][i + 1];
    }
  }
  vector<long double> ex(gausu(E, eb));

  if (n * k >= s) {
    printf("%.30Lf\\n", ex[s]);
    return 0;
  }

  mat A(n * k + 1, vector<long double>(n * k + 1));
  rep(i, n * k) A[0][i] = pro[k][i + 1];
  rep(i, n * k - 1) A[i + 1][i] = 1;
  A[0][n * k] = 1;
  A[n * k][n * k] = 1;

  vector<long double> b(n * k + 1);
  b[0] = b[n * k] = 1;

  A = powmat(A, s - n * k);

  long double ans = 0;
  ex[0] = 1;
  rep(i, n * k + 1) ans += ex[n * k - i] * A[0][i];
  printf("%.30Lf\\n", ans);
}