Language: C++

#include <bits/stdc++.h>



using namespace std;

using ll=long long;

using ld=__float128;



#define REP(i,n) for(ll i=0;i<(n);++i)

#define FOR(i,n,m) for(ll i=n; i<(m);i++)

#define eps  1e-5

using mat=vector<vector<ld>>;



mat gauss_jordan(mat A) {

    int n = A.size(), m = A[0].size();

    

    REP(i, n) {

        int piv = i;

        FOR(j, i, n) if (abs((long double)A[j][i]) > abs((long double)A[piv][i])) piv = j;

        swap(A[i], A[piv]);

        if (abs((long double)A[i][i]) < eps) return mat();

        FOR(j, i + 1, m) A[i][j] /= A[i][i];

        A[i][i]=1;

        REP(j, n) {

            if (i != j){

                 FOR(k, i + 1, m) A[j][k] -= A[j][i] * A[i][k];

                 A[j][i]=0;

            }

        }

    }

    /*

    mat x(n);

    REP(i,n)x[i].push_back(A[i][m-1]);

    cout<<"done"<<endl;

    return x;

    */

    return A;

}



mat mul(mat a,mat b){

    int n=a.size(),m=a[0].size(),l=b[0].size();

    assert(m==b.size());

    mat ret(n,vector<ld>(l,0));

    REP(i,n){

        REP(j,l){

            REP(k,m){

                ret[i][j]+=a[i][k]*b[k][j];

            }

        }

    }

    return ret;

}

mat id(ll n){

    mat ret(n,vector<ld>(n,0));

    REP(i,n)ret[i][i]=1;

    return ret;

}

mat pow(mat a,ll n){

  

    if(n==0)return id(a.size());

    mat d=pow(a,n/2);

    mat tmp=mul(d,d);

      cout<<"#"<<n<<endl;

    if(n&1)return mul(tmp,a);

    return tmp;

}



int main(){

    ll s,n,k;

    cin>>s>>n>>k;

    s=abs(s);

    cout<<setprecision(10)<<pow(n,k)<<endl;

    if(n==1){

        if(s%k!=0) cout<<-1<<endl;

        else cout<<(long double)(s)/k<<endl;

        return 0;

    }

    if(s==0){

        cout<<0<<endl;

        return 0;

    }



    vector<vector<ll>> dp(k+1,vector<ll>(n*k+1,0));

    dp[0][0]=1;



    REP(i,k){

        REP(j,n*k+1){

            for(int l=1;l<=n;++l){

                if(j>=l)dp[i+1][j]+=dp[i][j-l];

            }

        }

    }

    REP(i,n*k+1)cout<<dp[k][i]<<" ";

    cout<<endl;

    mat eq(n*k,vector<ld>(n*k+1,0));

    REP(i,n*k) {

        eq[i][n*k]=pow(n,k);

        eq[i][i]=pow(n,k);

        FOR(j,k,n*k+1) {

            if(j==i+1) continue;

            eq[i][abs(i-(j-1))-1]-=dp[k][j];

        }

    }



    mat ans=gauss_jordan(eq);

 

    vector<ld> e(n*k);

    for(ll i=n*k-1; i>=0; i--) {

        ld tmp=0;

        for(ll j=i+1; j<=n*k; j++) {

            if(j==n*k) {

                tmp-=ans[i][j];

            } else {

                tmp-=ans[i][j]*e[j];

            }

        }

        e[i]=tmp;

    }

    if(s<=n*k){

        cout<<(long double)e[s-1]<<endl;

        return 0;

    }



    mat c(n*k+1,vector<ld>(n*k+1,0));

    REP(i,n*k-1){

        c[i+1][i]=1;

    }

    c[n*k][n*k]=1;

    c[0][n*k]=1;

    REP(i,n*k){

        c[0][i]=dp[k][i]/pow(n,k);

    }



    mat coe=pow(c,s-n*k);



    mat tmp(n*k+1);



    REP(i,n*k)tmp[i].push_back(e[i]);

    tmp.back().push_back(1);

    mat ret=mul(coe,tmp);

    cout<<setprecision(100)<<(long double)ret[0][0]<<endl;

}

    }

    if(s<=n*k){

        cout<<(long double)e[s-1]<<endl;

        return 0;

    }



    mat c(n*k+1,vector<ld>(n*k+1,0));

    REP(i,n*k-1){

        c[i+1][i]=1;

    }

    c[n*k][n*k]=1;

    c[0][n*k]=1;

    REP(i,n*k){

        c[0][i]=dp[k][i]/pow(n,k);

    }



    mat coe=pow(c,s-n*k);



    mat tmp(n*k+1);



    REP(i,n*k)tmp[i].push_back(e[i]);

    tmp.back().push_back(1);

    mat ret=mul(coe,tmp);

    cout<<setprecision(100)<<(long double)ret[0][0]<<endl;

}


Compiler output:
a.cc:281:5: error: expected declaration before '}' token
  281 |     }
      |     ^
a.cc:283:5: error: expected unqualified-id before 'if'
  283 |     if(s<=n*k){
      |     ^~
a.cc:293:11: error: 'n' was not declared in this scope; did you mean 'yn'?
  293 |     mat c(n*k+1,vector<ld>(n*k+1,0));
      |           ^
      |           yn
a.cc:293:13: error: 'k' was not declared in this scope
  293 |     mat c(n*k+1,vector<ld>(n*k+1,0));
      |             ^
a.cc:293:28: error: 'n' was not declared in this scope; did you mean 'yn'?
  293 |     mat c(n*k+1,vector<ld>(n*k+1,0));
      |                            ^
      |                            yn
a.cc:293:30: error: 'k' was not declared in this scope
  293 |     mat c(n*k+1,vector<ld>(n*k+1,0));
      |                              ^
a.cc:13:18: error: expected unqualified-id before 'for'
   13 | #define REP(i,n) for(ll i=0;i<(n);++i)
      |                  ^~~
a.cc:295:5: note: in expansion of macro 'REP'
  295 |     REP(i,n*k-1){
      |     ^~~
a.cc:295:9: error: 'i' does not name a type
  295 |     REP(i,n*k-1){
      |         ^
a.cc:13:29: note: in definition of macro 'REP'
   13 | #define REP(i,n) for(ll i=0;i<(n);++i)
      |                             ^
a.cc:13:35: error: expected unqualified-id before '++' token
   13 | #define REP(i,n) for(ll i=0;i<(n);++i)
      |                                   ^~
a.cc:295:5: note: in expansion of macro 'REP'
  295 |     REP(i,n*k-1){
      |     ^~~
a.cc:301:5: error: 'c' does not name a type
  301 |     c[n*k][n*k]=1;
      |     ^
a.cc:303:5: error: 'c' does not name a type
  303 |     c[0][n*k]=1;
      |     ^
a.cc:13:18: error: expected unqualified-id before 'for'
   13 | #define REP(i,n) for(ll i=0;i<(n);++i)
      |                  ^~~
a.cc:305:5: note: in expansion of macro 'REP'
  305 |     REP(i,n*k){
      |     ^~~
a.cc:305:9: error: 'i' does not name a type
  305 |     REP(i,n*k){
      |         ^
a.cc:13:29: note: in definition of macro 'REP'
   13 | #define REP(i,n) for(ll i=0;i<(n);++i)
      |                             ^
a.cc:13:35: error: expected unqualified-id before '++' token
   13 | #define REP(i,n) for(ll i=0;i<(n);++i)
      |                                   ^~
a.cc:305:5: note: in expansion of macro 'REP'
  305 |     REP(i,n*k){
      |     ^~~
a.cc:313:19: error: 's' was not declared in this scope
  313 |     mat coe=pow(c,s-n*k);
      |                   ^
a.cc:313:21: error: 'n' was not declared in this scope; did you mean 'yn'?
  313 |     mat coe=pow(c,s-n*k);
      |                     ^
      |                     yn
a.cc:313:23: error: 'k' was not declared in this scope
  313 |     mat coe=pow(c,s-n*k);
      |                       ^
a.cc:317:13: error: 'n' was not declared in this scope; did you mean 'yn'?
  317 |     mat tmp(n*k+1);
      |             ^
      |             yn
a.cc:317:15: error: 'k' was not declared in this scope
  317 |     mat tmp(n*k+1);
      |               ^
a.cc:13:18: error: expected unqualified-id before 'for'
   13 | #define REP(i,n) for(ll i=0;i<(n);++i)
      |                  ^~~
a.cc:321:5: note: in expansion of macro 'REP'
  321 |     REP(i,n*k)tmp[i].push_back(e[i]);
      |     ^~~
a.cc:321:9: error: 'i' does not name a type
  321 |     REP(i,n*k)tmp[i].push_back(e[i]);
      |         ^
a.cc:13:29: note: in definition of macro 'REP'
   13 | #define REP(i,n) for(ll i=0;i<(n);++i)
      |                             ^
a.cc:13:35: error: expected unqualified-id before '++' token
   13 | #define REP(i,n) for(ll i=0;i<(n);++i)
      |                                   ^~
a.cc:321:5: note: in expansion of macro 'REP'
  321 |     REP(i,n*k)tmp[i].push_back(e[i]);
      |     ^~~
a.cc:323:5: error: 'tmp' does not name a type; did you mean 'tm'?
  323 |     tmp.back().push_back(1);
      |     ^~~
      |     tm
a.cc:327:5: error: 'cout' does not name a type
  327 |     cout<<setprecision(100)<<(long double)ret[0][0]<<endl;
      |     ^~~~
a.cc:329:1: error: expected declaration before '}' token
  329 | }
      | ^