Language: C++

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



	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:7:21: error: template argument 1 is invalid
    7 | typedef vector<array> matrix;
      |                     ^
a.cc:7:21: error: template argument 2 is invalid
a.cc: In function 'matrix identity_matrix(int)':
a.cc:13:15: error: reference to 'array' is ambiguous
   13 |   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:6:24: note:                 'typedef class std::vector<double> array'
    6 | typedef vector<double> array;
      |                        ^~~~~
a.cc:13:23: error: expression list treated as compound expression in initializer [-fpermissive]
   13 |   matrix A(n, array(n));
      |                       ^
a.cc:14:32: error: invalid types 'matrix {aka int}[int]' for array subscript
   14 |   for (int i = 0; i < n; ++i) A[i][i] = 1;
      |                                ^
a.cc: In function 'matrix mul(const matrix&, const matrix&)':
a.cc:19:14: error: request for member 'size' in 'A', which is of non-class type 'const matrix' {aka 'const int'}
   19 |   matrix C(A.size(), array(B[0].size()));
      |              ^~~~
a.cc:19:22: error: reference to 'array' is ambiguous
   19 |   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:6:24: note:                 'typedef class std::vector<double> array'
    6 | typedef vector<double> array;
      |                        ^~~~~
a.cc:19:29: error: invalid types 'const matrix {aka const int}[int]' for array subscript
   19 |   matrix C(A.size(), array(B[0].size()));
      |                             ^
a.cc:19:40: error: expression list treated as compound expression in initializer [-fpermissive]
   19 |   matrix C(A.size(), array(B[0].size()));
      |                                        ^
a.cc:20:25: error: request for member 'size' in 'C', which is of non-class type 'matrix' {aka 'int'}
   20 |   for (int i = 0; i < C.size(); ++i)
      |                         ^~~~
a.cc:21:26: error: invalid types 'matrix {aka int}[int]' for array subscript
   21 |     for (int j = 0; j < C[i].size(); ++j)
      |                          ^
a.cc:22:28: error: invalid types 'const matrix {aka const int}[int]' for array subscript
   22 |       for (int k = 0; k < A[i].size(); ++k)
      |                            ^
a.cc:23:10: error: invalid types 'matrix {aka int}[int]' for array subscript
   23 |         C[i][j] += A[i][k] * B[k][j];
      |          ^
a.cc:23:21: error: invalid types 'const matrix {aka const int}[int]' for array subscript
   23 |         C[i][j] += A[i][k] * B[k][j];
      |                     ^
a.cc:23:31: error: invalid types 'const matrix {aka const int}[int]' for array subscript
   23 |         C[i][j] += A[i][k] * B[k][j];
      |                               ^
a.cc: In function 'matrix pow(const matrix&, int)':
a.cc:27:37: error: request for member 'size' in 'A', which is of non-class type 'const matrix' {aka 'const int'}
   27 |   return e == 0 ? identity_matrix(A.size())  :
      |                                     ^~~~
a.cc: At global scope:
a.cc:32:51: error: reference to 'array' is ambiguous
   32 | 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:6:24: note:                 'typedef class std::vector<double> array'
    6 | typedef vector<double> array;
      |                        ^~~~~
a.cc: In function 'std::vector<double> gauss_jordan(const matrix&, const int&)':
a.cc:33:17: error: request for member 'size' in 'A', which is of non-class type 'const matrix' {aka 'const int'}
   33 |         int n=A.size();
      |                 ^~~~
a.cc:34:20: error: reference to 'array' is ambiguous
   34 |         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:6:24: note:                 'typedef class std::vector<double> array'
    6 | typedef vector<double> array;
      |                        ^~~~~
a.cc:34:30: error: expression list treated as compound expression in initializer [-fpermissive]
   34 |         matrix B(n,array(n+1));
      |                              ^
a.cc:36:39: error: invalid types 'matrix {aka int}[int]' for array subscript
   36 |                 for(int j=0;j<n;j++) B[i][j]=A[i][j];
      |                                       ^
a.cc:36:47: error: invalid types 'const matrix {aka const int}[int]' for array subscript
   36 |                 for(int j=0;j<n;j++) B[i][j]=A[i][j];
      |                                               ^
a.cc:38:31: error: invalid types 'matrix {aka int}[int]' for array subscript
   38 |         for(int i=0;i<n;i++) B[i][n]=b[i];
      |                               ^
a.cc:38:39: error: invalid types 'const int[int]' for array subscript
   38 |         for(int i=0;i<n;i++) B[i][n]=b[i];
      |                                       ^
a.cc:43:33: error: invalid types 'matrix {aka int}[int]' for array subscript
   43 |                         if(abs(B[j][i])>abs(B[pivot][i])) pivot=j;
      |                                 ^
a.cc:43:46: error: invalid types 'matrix {aka int}[int]' for array subscript
   43 |                         if(abs(B[j][i])>abs(B[pivot][i])) pivot=j;
      |                                              ^
a.cc:45:23: error: invalid types 'matrix {aka int}[int]' for array subscript
   45 |                 swap(B[i],B[pivot]);
      |                       ^
a.cc:45:28: error: invalid types 'matrix {aka int}[int]' for array subscript
   45 |                 swap(B[i],B[pivot]);
      |                            ^
a.cc:47:25: error: invalid types 'matrix {aka int}[int]' for array subscript
   47 |                 if(abs(B[i][i])<eps){ cout<<"error\n";return array();}
      |                         ^
a.cc:47:62: error: reference to 'array' is ambiguous
   47 |                 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:6:24: note:                 'typedef class std::vector<double> array'
    6 | typedef vector<double> array;
      |                        ^~~~~
a.cc:49:42: error: invalid types 'matrix {aka int}[int]' for array subscript
   49 |                 for(int j=i+1;j<=n;j++) B[i][j]/=B[i][i];
      |                                          ^
a.cc:49:51: error: invalid types 'matrix {aka int}[int]' for array subscript
   49 |                 for(int j=i+1;j<=n;j++) B[i][j]/=B[i][i];
      |                                                   ^
a.cc:52:58: error: invalid types 'matrix {aka int}[int]' for array subscript
   52 |                                 for(int k=i+1;k<=n;k++) B[j][k]-=B[j][i]*B[i][k];
      |                                                          ^
a.cc:52:67: error: invalid types 'matrix {aka int}[int]' for array subscript
   52 |                                 for(int k=i+1;k<=n;k++) B[j][k]-=B[j][i]*B[i][k];
      |                                                                   ^
a.cc:52:75: error: invalid types 'matrix {aka int}[int]' for array subscript
   52 |                                 for(int k=i+1;k<=n;k++) B[j][k]-=B[j][i]*B[i][k];
      |                                                                           ^
a.cc:56:9: error: reference to 'array' is ambiguous
   56 |         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:6:24: note:                 'typedef class std::vector<double> array'
    6 | typedef vector<double> array;
      |                        ^~~~~
a.cc:58:30: error: 'x' was not declared in this scope
   58 |         for(int i=0;i<n;i++) x[i]=B[i][n];
      |                              ^
a.cc:58:36: error: invalid types 'matrix {aka int}[int]' for array subscript
   58 |         for(int i=0;i<n;i++) x[i]=B[i][n];
      |                                    ^
a.cc:59:16: error: 'x' was not declared in this scope
   59 |         return x;
      |                ^
a.cc: In function 'void solve()':
a.cc:72:9: error: 'memset' was not declared in this scope
   72 |         memset(prob,0,sizeof(prob));
      |         ^~~~~~
a.cc:3:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    2 | #include<vector>
  +++ |+#include <cstring>
    3 | using namespace std;
a.cc:79:24: error: reference to 'array' is ambiguous
   79 |         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:6:24: note:                 'typedef class std::vector<double> array'
    6 | typedef vector<double> array;
      |                        ^~~~~
a.cc:79:38: error: expression list treated as compound e