Problem p01432 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

const double eps = 1e-8;
typedef vector<double> array;
typedef vector<array> matrix;
int S, N, K;
double prob[12][110];

matrix identity_matrix(int n) {
  matrix A(n, array(n));
  for (int i = 0; i < n; ++i)
    A[i][i] = 1;
  return A;
}

matrix mul(const matrix &A, const matrix &B) {
  matrix C(A.size(), array(B[0].size()));
  for (int i = 0; i < C.size(); ++i)
    for (int j = 0; j < C[i].size(); ++j)
      for (int k = 0; k < A[i].size(); ++k)
        C[i][j] += A[i][k] * B[k][j];
  return C;
}
matrix pow(const matrix &A, int e) {
  return e == 0       ? identity_matrix(A.size())
         : e % 2 == 0 ? pow(mul(A, A), e / 2)
                      : mul(A, pow(A, e - 1));
}

vector<double> gauss_jordan(const matrix &A, const array &b) {
  int n = A.size();
  matrix B(n, array(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) {
      cout << "error\\n";
      return array();
    }

    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];
      }
    }
  }
  array x(n);

  for (int i = 0; i < n; i++)
    x[i] = B[i][n];
  return x;
}

void solve() {

  S = abs(S);
  if (N == 1) {
    if (S % K == 0)
      cout << S / K << endl;
    else
      cout << -1 << endl;
    return;
  }
  memset(prob, 0, sizeof(prob));
  prob[0][0] = 1.0;
  for (int i = 0; i < K; i++)
    for (int j = 0; j + N <= N * K; j++)
      for (int z = 1; z <= N; z++)
        prob[i + 1][j + z] += prob[i][j] / N;

  matrix A(K * N + 1, array(K * N + 1, 0));
  A[0][0] = 1;
  for (int i = 1; i <= K * N; i++) {
    A[i][i] = 1;
    for (int j = 0; j <= K * N; j++) {
      A[i][abs(i - j)] -= prob[K][j];
    }
  }
  array V(K * N + 1, 0);
  V[0] = 0;
  for (int i = 1; i <= K * N; i++)
    V[i] = 1;

  V = gauss_jordan(A, V);

  matrix a(N * K + 1, array(N * K + 1, 0));
  a[N * K][N * K] = a[0][N * K] = 1;
  for (int i = 0; i < N * K; i++)
    a[0][i] = prob[K][i + 1];
  for (int i = 0; i < K * N - 1; i++)
    a[i + 1][i] = 1;

  if (S < N * K) {
    printf("%.6lf\\n", V[S]);
    return;
  }
  a = pow(a, S - N * K + 1);

  double res = a[0][N * K];
  for (int i = 0; i < N * K; i++) {
    res += a[0][i] * V[N * K - i - 1];
  }

  printf("%.6lf\\n", res);
}

int main() {
  while (cin >> S >> N >> K)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

const double eps = 1e-8;
typedef vector<double> array;
typedef vector<array> matrix;
int S, N, K;
double prob[11][101];

matrix identity_matrix(int n) {
  matrix A(n, array(n));
  for (int i = 0; i < n; ++i)
    A[i][i] = 1;
  return A;
}

matrix mul(const matrix &A, const matrix &B) {
  matrix C(A.size(), array(B[0].size()));
  for (int i = 0; i < C.size(); ++i)
    for (int j = 0; j < C[i].size(); ++j)
      for (int k = 0; k < A[i].size(); ++k)
        C[i][j] += A[i][k] * B[k][j];
  return C;
}
matrix pow(const matrix &A, int e) {
  return e == 0       ? identity_matrix(A.size())
         : e % 2 == 0 ? pow(mul(A, A), e / 2)
                      : mul(A, pow(A, e - 1));
}

vector<double> gauss_jordan(const matrix &A, const array &b) {
  int n = A.size();
  matrix B(n, array(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) {
      cout << "error\\n";
      return array();
    }

    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];
      }
    }
  }
  array x(n);

  for (int i = 0; i < n; i++)
    x[i] = B[i][n];
  return x;
}

void solve() {

  S = abs(S);
  if (N == 1) {
    if (S % K == 0)
      cout << S / K << endl;
    else
      cout << -1 << endl;
    return;
  }
  memset(prob, 0, sizeof(prob));
  prob[0][0] = 1.0;
  for (int i = 0; i < K; i++)
    for (int j = 0; j + N <= N * K; j++)
      for (int z = 1; z <= N; z++)
        prob[i + 1][j + z] += prob[i][j] / N;

  matrix A(K * N + 1, array(K * N + 1, 0));
  A[0][0] = 1;
  for (int i = 1; i <= K * N; i++) {
    A[i][i] = 1;
    for (int j = 0; j <= K * N; j++) {
      A[i][abs(i - j)] -= prob[K][j];
    }
  }
  array V(K * N + 1, 0);
  V[0] = 0;
  for (int i = 1; i <= K * N; i++)
    V[i] = 1;

  V = gauss_jordan(A, V);

  matrix a(N * K + 1, array(N * K + 1, 0));
  a[N * K][N * K] = a[0][N * K] = 1;
  for (int i = 0; i < N * K; i++)
    a[0][i] = prob[K][i + 1];
  for (int i = 0; i < K * N - 1; i++)
    a[i + 1][i] = 1;

  a = pow(a, S - N * K + 1);
  matrix ans(1, array(N * K + 1));
  double res = a[0][N * K];

  if (S < N * K) {
    printf("%.6lf\\n", a[S][N * K]);
    return;
  }

  for (int i = 0; i < N * K; i++) {
    res += a[0][i] * V[N * K - i - 1];
  }

  printf("%.6lf\\n", res);
}

int main() {
  while (cin >> S >> N >> K)
    solve();
  return 0;
}