Language: C++

#include<iostream>
#include<vector>
#include<cmath>
#include<cstdio>
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];



	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;
}

Compiler output:
a.cc:9:21: error: template argument 1 is invalid
    9 | typedef vector<array> matrix;
      |                     ^
a.cc:9:21: error: template argument 2 is invalid
a.cc: In function 'matrix identity_matrix(int)':
a.cc:15:15: error: reference to 'array' is ambiguous
   15 |   matrix A(n, array(n));
      |               ^~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:8:24: note:                 'typedef class std::vector<double> array'
    8 | typedef vector<double> array;
      |                        ^~~~~
a.cc:15:23: error: expression list treated as compound expression in initializer [-fpermissive]
   15 |   matrix A(n, array(n));
      |                       ^
a.cc:16:32: error: invalid types 'matrix {aka int}[int]' for array subscript
   16 |   for (int i = 0; i < n; ++i) A[i][i] = 1;
      |                                ^
a.cc: In function 'matrix mul(const matrix&, const matrix&)':
a.cc:21:14: error: request for member 'size' in 'A', which is of non-class type 'const matrix' {aka 'const int'}
   21 |   matrix C(A.size(), array(B[0].size()));
      |              ^~~~
a.cc:21:22: error: reference to 'array' is ambiguous
   21 |   matrix C(A.size(), array(B[0].size()));
      |                      ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:8:24: note:                 'typedef class std::vector<double> array'
    8 | typedef vector<double> array;
      |                        ^~~~~
a.cc:21:29: error: invalid types 'const matrix {aka const int}[int]' for array subscript
   21 |   matrix C(A.size(), array(B[0].size()));
      |                             ^
a.cc:21:40: error: expression list treated as compound expression in initializer [-fpermissive]
   21 |   matrix C(A.size(), array(B[0].size()));
      |                                        ^
a.cc:22:25: error: request for member 'size' in 'C', which is of non-class type 'matrix' {aka 'int'}
   22 |   for (int i = 0; i < C.size(); ++i)
      |                         ^~~~
a.cc:23:26: error: invalid types 'matrix {aka int}[int]' for array subscript
   23 |     for (int j = 0; j < C[i].size(); ++j)
      |                          ^
a.cc:24:28: error: invalid types 'const matrix {aka const int}[int]' for array subscript
   24 |       for (int k = 0; k < A[i].size(); ++k)
      |                            ^
a.cc:25:10: error: invalid types 'matrix {aka int}[int]' for array subscript
   25 |         C[i][j] += A[i][k] * B[k][j];
      |          ^
a.cc:25:21: error: invalid types 'const matrix {aka const int}[int]' for array subscript
   25 |         C[i][j] += A[i][k] * B[k][j];
      |                     ^
a.cc:25:31: error: invalid types 'const matrix {aka const int}[int]' for array subscript
   25 |         C[i][j] += A[i][k] * B[k][j];
      |                               ^
a.cc: In function 'matrix pow(const matrix&, int)':
a.cc:29:37: error: request for member 'size' in 'A', which is of non-class type 'const matrix' {aka 'const int'}
   29 |   return e == 0 ? identity_matrix(A.size())  :
      |                                     ^~~~
a.cc: At global scope:
a.cc:34:51: error: reference to 'array' is ambiguous
   34 | vector<double> gauss_jordan(const matrix& A,const array& b){
      |                                                   ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:8:24: note:                 'typedef class std::vector<double> array'
    8 | typedef vector<double> array;
      |                        ^~~~~
a.cc: In function 'std::vector<double> gauss_jordan(const matrix&, const int&)':
a.cc:35:17: error: request for member 'size' in 'A', which is of non-class type 'const matrix' {aka 'const int'}
   35 |         int n=A.size();
      |                 ^~~~
a.cc:36:20: error: reference to 'array' is ambiguous
   36 |         matrix B(n,array(n+1));
      |                    ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:8:24: note:                 'typedef class std::vector<double> array'
    8 | typedef vector<double> array;
      |                        ^~~~~
a.cc:36:30: error: expression list treated as compound expression in initializer [-fpermissive]
   36 |         matrix B(n,array(n+1));
      |                              ^
a.cc:38:39: error: invalid types 'matrix {aka int}[int]' for array subscript
   38 |                 for(int j=0;j<n;j++) B[i][j]=A[i][j];
      |                                       ^
a.cc:38:47: error: invalid types 'const matrix {aka const int}[int]' for array subscript
   38 |                 for(int j=0;j<n;j++) B[i][j]=A[i][j];
      |                                               ^
a.cc:40:31: error: invalid types 'matrix {aka int}[int]' for array subscript
   40 |         for(int i=0;i<n;i++) B[i][n]=b[i];
      |                               ^
a.cc:40:39: error: invalid types 'const int[int]' for array subscript
   40 |         for(int i=0;i<n;i++) B[i][n]=b[i];
      |                                       ^
a.cc:45:33: error: invalid types 'matrix {aka int}[int]' for array subscript
   45 |                         if(abs(B[j][i])>abs(B[pivot][i])) pivot=j;
      |                                 ^
a.cc:45:46: error: invalid types 'matrix {aka int}[int]' for array subscript
   45 |                         if(abs(B[j][i])>abs(B[pivot][i])) pivot=j;
      |                                              ^
a.cc:47:23: error: invalid types 'matrix {aka int}[int]' for array subscript
   47 |                 swap(B[i],B[pivot]);
      |                       ^
a.cc:47:28: error: invalid types 'matrix {aka int}[int]' for array subscript
   47 |                 swap(B[i],B[pivot]);
      |                            ^
a.cc:49:25: error: invalid types 'matrix {aka int}[int]' for array subscript
   49 |                 if(abs(B[i][i])<eps){ cout<<"error\n";return array();}
      |                         ^
a.cc:49:62: error: reference to 'array' is ambiguous
   49 |                 if(abs(B[i][i])<eps){ cout<<"error\n";return array();}
      |                                                              ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:8:24: note:                 'typedef class std::vector<double> array'
    8 | typedef vector<double> array;
      |                        ^~~~~
a.cc:51:42: error: invalid types 'matrix {aka int}[int]' for array subscript
   51 |                 for(int j=i+1;j<=n;j++) B[i][j]/=B[i][i];
      |                                          ^
a.cc:51:51: error: invalid types 'matrix {aka int}[int]' for array subscript
   51 |                 for(int j=i+1;j<=n;j++) B[i][j]/=B[i][i];
      |                                                   ^
a.cc:54:58: error: invalid types 'matrix {aka int}[int]' for array subscript
   54 |                                 for(int k=i+1;k<=n;k++) B[j][k]-=B[j][i]*B[i][k];
      |                                                          ^
a.cc:54:67: error: invalid types 'matrix {aka int}[int]' for array subscript
   54 |                                 for(int k=i+1;k<=n;k++) B[j][k]-=B[j][i]*B[i][k];
      |                                                                   ^
a.cc:54:75: error: invalid types 'matrix {aka int}[int]' for array subscript
   54 |                                 for(int k=i+1;k<=n;k++) B[j][k]-=B[j][i]*B[i][k];
      |                                                                           ^
a.cc:58:9: error: reference to 'array' is ambiguous
   58 |         array x(n);
      |         ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:8:24: note:                 'typedef class std::vector<double> array'
    8 | typedef vector<double> array;
      |                        ^~~~~
a.cc:60:30: error: 'x' was not declared in this scope
   60 |         for(int i=0;i<n;i++) x[i]=B[i][n];
      |                              ^
a.cc:60:36: error: invalid types 'matrix {aka int}[int]' for array subscript
   60 |         for(int i=0;i<n;i++) x[i]=B[i][n];
      |                                    ^
a.cc:61:16: error: 'x' was not declared in this scope
   61 |         return x;
      |                ^
a.cc: In function 'void solve()':
a.cc:74:9: error: 'memset' was not declared in this scope
   74 |         memset(prob,0,sizeof(prob));
      |         ^~~~~~
a.cc:5:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    4 | #include<cstdio>
  +++ |+#include <cstring>
    5 | using namespace std;
a.cc:81:24: error: reference to 'array' is ambiguous
   81 |         matrix A(K*N+1,array(K*N+1,0));
      |                        ^~~~~
/usr/include/c++/14/bits/stl_pair.h:99:12: note: candidates are: 'template<class _Tp, long unsigned int _Nm> struct std::array'
   99 |     struct array;
      |            ^~~~~
a.cc:8:24: note:                 'typedef class std::vector<double> array'
    8 | typedef vector<double> array;
      |                        ^~~~~
a.cc:81:38: error: expression list treated as compound e