Problem:
問題文
影の魔女 ELSAMARIA はすべてのものに対して祈りを続ける魔女である．影に捕まったあらゆる命はこの魔女の中に引きずり込まれてしまう．
影は触手のように動き
美樹さやか
を引きずり込もうと狙っている．
美樹さやか
はこの魔女に捕まることなく攻撃するために，魔女にとって予測の難しいでたらめな動きをすることを考えた．具体的には，まず頭の中で
1
から
N
までの整数を一様な確率で
K
個作り，その合計値の距離だけ今の位置から魔女の方向へジャンプする，ということを繰り返すのである．そして魔女の位置に到達した時に攻撃するのである．
さやか
と魔女はある一直線上にいるとし，最初魔女は座標
0
，
さやか
は座標
S
の位置にいるものとする．また魔女は動かないものとする．何度ジャンプした後に魔女に最初の攻撃を与えることができるだろうか．期待値を求めよ．
入力形式
入力は以下の形式で与えられる．
S\ N\ K
S
は最初に
さやか
がいる座標である．
N
は
さやか
が頭の中で作る整数の範囲の上限，
K
は
さやか
が
1
回のジャンプに用いる整数の個数である．
出力形式
さやか
が魔女の位置に到達するために必要なジャンプの回数の期待値を
1
行に出力せよ．もし何度ジャンプしても魔女の位置へ到達できない，もしくは期待値が有限値に収まらない場合は
-1
と出力せよ．
小数点以下何桁でも出力して構わないが，相対誤差あるいは絶対誤差が
10
-6
未満になっていなければならない．
制約
-10
9
≤ S ≤ 10
9
1 ≤ N ≤ 10
1 ≤ K ≤ 10
入力値は全て整数である．
入出力例
入力例 1
6 6 1
出力例1
6.00000000
入力例 2
-100 7 5
出力例 2
43.60343043
入力例 3
756434182 9 10
出力例 3
15128783.80867078
Problem Setter: Flat35

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

vector<vector<long double> > matmul(int N, const vector<vector<long double> >& mat1, const vector<vector<long double> >& mat2) {
	vector<vector<long double> > res(N, vector<long double>(N));
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			for (int k = 0; k < N; k++) {
				res[i][j] += mat1[i][k] * mat2[k][j];
			}
		}
	}
	return res;
}

void gaussian_elimination(int N, vector<vector<long double> >& mat) {
	int row = 0;
	for (int i = 0; i < N; i++) {
		int opt = -1;
		long double optmag = 0.0;
		for (int j = row; j < N; j++) {
			if (optmag < abs(mat[j][i])) {
				opt = j;
				optmag = abs(mat[j][i]);
			}
		}
		if (opt == -1) {
			continue;
		}
		swap(mat[row], mat[opt]);
		double submul = 1.0 / mat[row][i];
		for (int j = i + 1; j <= N; j++) {
			mat[row][j] *= submul;
		}
		for (int j = 0; j < N; j++) {
			if (j != row) {
				double mul = mat[j][i];
				for (int k = i; k <= N; k++) {
					mat[j][k] -= mat[row][k] * mul;
				}
			}
		}
		row += 1;
	}
}

long double solve(long long S, int N, int K) {
	// step #1. calculate probability of transition
	vector<long double> prob = { 1.0 };
	for (int i = 1; i <= K; i++) {
		vector<long double> nprob(N * i + 1);
		long double s = 0.0;
		for (int j = 0; j <= N * i; j++) {
			nprob[j] = s / N;
			s += (j <= N * (i - 1) ? prob[j] : 0.0);
			s -= (j >= N ? prob[j - N] : 0.0);
		}
		prob = nprob;
	}

	// step #2. create matrix
	int Z = N * K;
	vector<vector<long double> > mat0(2 * Z, vector<long double>(2 * Z, 0.0L));
	for (int i = 0; i < Z - 1; i++) {
		mat0[i + 1][i] = 1.0L;
		mat0[Z + i + 1][Z + i] = 1.0L;
	}
	for (int i = 0; i < Z; i++) {
		mat0[0][i] = prob[i + 1];
	}
	for (int i = 0; i < Z; i++) {
		mat0[Z][i] = prob[i + 1];
		mat0[Z][Z + i] = prob[i + 1];
	}
	
	// separate by cases
	vector<long double> p(Z), t(Z);
	if (S < Z) {
		p[S] = 1.0L;
		t[S] = 0.0L;
	}
	else {
		// step #3. matrix exponentation
		vector<vector<long double> > mat1(2 * Z, vector<long double>(2 * Z));
		for (int i = 0; i < 2 * Z; i++) {
			mat1[i][i] = 1.0L;
		}
		vector<vector<long double> > mat2 = mat0;
		long long rem = S - Z;
		while (rem >= 1) {
			if (rem % 2 == 1) {
				mat1 = matmul(2 * Z, mat1, mat2);
			}
			mat2 = matmul(2 * Z, mat2, mat2);
			rem /= 2;
		}
		
		// step #4. calculate answer
		vector<long double> x(2 * Z);
		for (int i = 0; i < 2 * Z; i++) {
			x[i] = mat1[i][0];
		}
		for (int i = 0; i < Z; i++) {
			for (int j = Z; j <= Z + i; j++) {
				p[i] += x[j - Z] * prob[j - i];
				t[i] += x[j - Z + Z] * prob[j - i];
			}
			t[i] += p[i];
		}
	}

	// step #5. compute systems of linear equation
	vector<vector<long double> > mat3(Z - 1, vector<long double>(Z));
	for (int i = 1; i < Z; i++) {
		for (int j = 1; j <= Z; j++) {
			int pos = abs(i - j);
			if (pos != 0) {
				mat3[i - 1][pos - 1] -= prob[j];
			}
		}
		mat3[i - 1][i - 1] += 1.0;
		mat3[i - 1][Z - 1] += 1.0;
	}
	gaussian_elimination(Z - 1, mat3);
	vector<long double> e(Z);
	for (int i = 1; i < Z; i++) {
		e[i] = mat3[i - 1][Z - 1];
	}

	// step #6. calculate answer
	long double ans = 0.0;
	for (int i = 0; i < Z; i++) {
		ans += t[i];
	}
	for (int i = 1; i < Z; i++) {
		ans += p[i] * e[i];
	}

	return ans;
}

int main() {
	long long S; int N, K;
	cin >> S >> N >> K;
	S = abs(S);
	if (N == 1) {
		cout << (S % K == 0 ? S / K : -1) << endl;
	}
	else {
		long double ans = solve(S, N, K);
		cout.precision(15);
		cout << fixed << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 660
memory_kb: 5652
final_rank: 11

code_snippet: #include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

int S, N, K;
double D[19][109], E[109];
double Prob1[150009];
double Expc1[150009];
double Prob2[7009][109];
double Expc2[7009][109];
double ProbList[109], ExpcList[109], ExpcSecond[109];

void GetFirstAns(int BASE) {
	for (int i = 0; i <= BASE + 200; i++) Prob1[i] = 0.0;
	for (int i = 0; i <= BASE + 200; i++) Expc1[i] = 0.0;
	Prob1[0] = 1.0;
	for (int i = 1; i <= BASE + 200; i++) {
		for (int j = 1; j <= N * K; j++) {
			int pre = i - j; if (pre < 0 || pre >= BASE) continue;
			Prob1[i] += (Prob1[pre] * E[j]);
			Expc1[i] += (Prob1[pre] * E[j]) * (Expc1[pre] + 1.0);
		}
		if (Prob1[i] != 0.0) Expc1[i] /= Prob1[i];
	}
}

void GetSecondAns() {
	for (int i = 0; i <= 7000; i++) {
		for (int j = 0; j <= N * K; j++) Prob2[i][j] = 0.0;
		for (int j = 0; j <= N * K; j++) Expc2[i][j] = 0.0;
	}
	
	// Forward DP
	for (int i = 1; i <= N * K; i++) Prob2[0][i] = ProbList[i];
	for (int i = 0; i < 7000; i++) {
		for (int j = 1; j <= N * K; j++) {
			for (int k = 1; k <= N * K; k++) {
				int to = abs(j - k);
				Prob2[i + 1][to] += (Prob2[i][j] * E[k]);
			}
		}
	}
	
	// Reverse DP
	for (int i = 6999; i >= 0; i--) {
		for (int j = 1; j <= N * K; j++) {
			for (int k = 1; k <= N * K; k++) {
				int to = abs(j - k);
				Expc2[i][j] += (1.0 * (Expc2[i + 1][to] + 1.0) * E[k]);
			}
		}
	}
}

int main() {
	// Input
	cin >> S >> N >> K; S = abs(S);
	if (N == 1) {
		if (S % K == 0) cout << S/ K << endl;
		else cout << "-1" << endl;
		return 0;
	}
	
	// Prepare
	D[0][0] = 1.0;
	for (int i = 0; i < K; i++) {
		for (int j = 0; j < N * K; j++) {
			for (int k = 1; k <= N; k++) D[i+1][j+k] += 1.0 * D[i][j] / N;
		}
	}
	for (int i = 1; i <= N * K; i++) E[i] = D[K][i];
	
	// Get First Answer
	if (S <= N * K) {
		ProbList[S] = 1.0;
		ExpcList[S] = 0.;
	}
	else {
		double BASE_Expc = 1.0 / (0.5 * (N + 1) * K);
		int OFFSET = min(100000, S - N * K);
		GetFirstAns(OFFSET);
		for (int i = 0; i <= N * K; i++) {
			ProbList[N * K - i] = Prob1[OFFSET + i];
			ExpcList[N * K - i] = Expc1[OFFSET + i] + 1.0 * ((S - N * K) - OFFSET) * BASE_Expc;
		}
	}
	
	// Get Second Answer
	GetSecondAns();
	for (int i = 1; i <= N * K; i++) {
		ExpcSecond[i] = Expc2[0][i];
		//cout << i << " " << ProbList[i] << " " << ExpcSecond[i] << endl;
	}
	
	// Get Final Answer
	double Answer = 0.0;
	for (int i = 1; i <= N * K; i++) Answer += (ExpcList[i] + ExpcSecond[i]) * ProbList[i];
	printf("%.12lf\n", Answer);
	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 17084
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)

#define popcount __builtin_popcount
#define next __next
#define prev __prev

#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...)                                                               \
  cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif

// ---------------- Utility ------------------

template <class T>
bool chmin(T &a, const T &b) {
  if (a <= b) return false;
  a = b;
  return true;
}

template <class T>
bool chmax(T &a, const T &b) {
  if (a >= b) return false;
  a = b;
  return true;
}

template <class T>
using MaxHeap = priority_queue<T>;

template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

// ----------------- Input -------------------

template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}

// ----------------- Output ------------------

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ',' << p.second;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &e : v) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
  for (const T &e : d) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{ ";
  for (const T &e : s) os << e << " ";
  return os << "}";
}

template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << "{ ";
  for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
  return os << "}";
}

template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
  (..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}

template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << "(";
  dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
  return os << ")";
}

void dump_str_rec(ostringstream &) {}

template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << ", " << head;
  dump_str_rec(oss, forward<Tail>(tail)...);
}

template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
  ostringstream oss;
  oss << arg;
  dump_str_rec(oss, forward<U>(args)...);
  return oss.str();
}

// --------------- Fast I/O ------------------

void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

// ------------------ ACL --------------------

// #include <atcoder/modint>
// constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
// using mint = atcoder::static_modint<MOD>;
// ostream &operator<<(ostream &os, const mint &v) {
//   return os << v.val();
// }

// ------------ End of template --------------

#define endl "\n"
using ll = long long;
using pii = pair<int, int>;

const bool multitest = false;

#define double long double

// N変数連立方程式(Ax=bの形)を解く
//
// 引数のAは拡大係数行列. (Ax=bでAとbをそのまま並べてつなげたもの)
// すなわち
// A[i][0] * x_0 + A[i][1] * x_1 + ... A[i][n - 1] * x_{n-1} = A[i][n]
// (0 <= i < n)
//
//
// 関数が終わったときにAの左側は単位行列になる
// Note: 誤差に注意
//
// Verify:
// https://onlinejudge.u-aizu.ac.jp/solutions/problem/1328/review/4927454/okuraofvegetable/C++17
// https://onlinejudge.u-aizu.ac.jp/solutions/problem/2171/review/6045411/okuraofvegetable/C++17

double myabs(double a) {
  if (a < 0.0) return -a;
  return a;
}

vector<double> solve_equation(vector<vector<double>> &A) {
  assert(A[0].size() == A.size() + 1);
  for (int i = 0; i < A.size(); i++) {
    if (myabs(A[i][i]) < eps) {
      for (int j = i + 1; j < A.size(); j++) {
        if (abs(A[j][i]) > eps) {
          swap(A[i], A[j]);
          break;
        }
      }
    }
    double r = A[i][i];
    if (myabs(r) < eps) {
      // dmp(r);
      return vector<double>();
    }
    for (int j = i; j <= A.size(); j++) { A[i][j] /= r; }
    for (int j = i + 1; j < A.size(); j++) {
      double s = A[j][i];
      for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * s; }
    }
  }
  for (int i = A.size() - 1; i > 0; i--) {
    for (int j = 0; j < i; j++) {
      double r = A[j][i];
      for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * r; }
    }
  }
  vector<double> sol;
  for (int i = 0; i < A.size(); i++) sol.push_back(A[i][A.size()]);
  return sol;
}

// Matrix class
//
// constructor of T : T(1),T(0) must be unit of T
//
// Verified: DDCC B
// https://atcoder.jp/contests/ddcc2020-final/submissions/9941022
//
// Verified: determinant
// https://judge.yosupo.jp/submission/45201
//

template <class T>
struct Matrix {
  int H, W;
  vector<vector<T>> elem;
  Matrix(int H, int W, T val = T(0)) : H(H), W(W) {
    elem.resize(H);
    for (int i = 0; i < H; i++) elem[i].assign(W, val);
  }
  Matrix(const vector<vector<T>> &m)
      : H(m.size()), W(m.size() ? m[0].size() : 0) {
    elem.resize(H);
    for (int i = 0; i < H; i++) elem[i].resize(W);
    for (int i = 0; i < H; i++) elem[i] = m[i];
  }
  vector<T> &operator[](int h) { return elem[h]; }
  static Matrix identity(int N) {
    Matrix a(N, N);
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          a.elem[i][i] = T(1);
        else
          a.elem[i][j] = T(0);
      }
    }
    return a;
  }
  Matrix operator+(const Matrix &m) const {
    assert(H == m.H && W == m.W);
    Matrix a(H, W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        a.elem[i][j] = this->elem[i][j] + m.elem[i][j];
      }
    }
    return a;
  }
  Matrix operator-(const Matrix &m) const {
    assert(H == m.H && W == m.W);
    Matrix a(H, W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        a.elem[i][j] = this->elem[i][j] - m.elem[i][j];
      }
    }
    return a;
  }
  Matrix operator*(const Matrix &m) const {
    assert(W == m.H);
    Matrix a(H, m.W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < m.W; j++) {
        for (int k = 0; k < W; k++) {
          a.elem[i][j] = a.elem[i][j] + (this->elem[i][k] * m.elem[k][j]);
        }
      }
    }
    return a;
  }
  Matrix &operator+=(const Matrix &m) { return *this = *this + m; }
  Matrix &operator-=(const Matrix &m) { return *this = *this - m; }
  Matrix &operator*=(const Matrix &m) { return *this = *this * m; }
  Matrix pow(long long x) const {
    Matrix a = identity(H);
    Matrix b = *this;
    while (x) {
      if (x & 1) a *= b;
      b *= b;
      x >>= 1;
    }
    return a;
  }

  T determinant() {
    Matrix m(*this);
    assert(m.W == m.H);
    T ret(1);
    for (int i = 0; i < m.H; i++) {
      int idx = -1;
      for (int j = i; j < m.H; j++) {
        if (m[j][i] != T(0)) idx = j;
      }
      if (idx == -1) return T(0);
      if (i != idx) {
        ret *= T(-1);
        swap(m[i], m[idx]);
      }
      ret *= m[i][i];
      T div = m[i][i];
      for (int j = i; j < m.W; j++) m[i][j] /= div;
      for (int j = i + 1; j < m.H; j++) {
        T a = m[j][i];
        for (int k = i; k < m.W; k++) m[j][k] -= m[i][k] * a;
      }
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const Matrix &x) {
    for (int i = 0; i < x.H; i++) {
      for (int j = 0; j < x.W; j++) {
        os << x.elem[i][j];
        if (j + 1 < x.W) cout << ' ';
      }
      if (i + 1 < x.H) cout << endl;
    }
    return os;
  }
  friend istream &operator>>(istream &is, Matrix &x) {
    for (auto &v : x.elem) is >> v;
    return is;
  }
};

void solve() {
  int S, N, K;
  cin >> S >> N >> K;
  if (S < 0) S *= -1;

  if (N == 1) {
    if (S % K == 0)
      cout << S / K << endl;
    else
      cout << -1 << endl;
    return;
  }

  auto dp = vect(K + 1, vect(N * K + 1, (double)0.0));
  dp[0][0] = 1.0;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j <= N * K; j++) {
      for (int k = 1; k <= N; k++) {
        if (j + k <= N * K) dp[i + 1][j + k] += dp[i][j] * (1.0 / (double)N);
      }
    }
  }

  auto A = vect(N * K, vect(N * K + 1, (double)0.0));

  A[0][0] = 1.0;
  for (int i = 1; i < N * K; i++) {
    for (int j = 1; j <= N * K; j++) { A[i][abs(i - j)] += dp[K][j]; }
    A[i][i] -= 1.0;
    A[i][N * K] = -1.0;
  }

  // for (auto a : A) dmp(a);

  auto sol = solve_equation(A);
  // dmp(sol);
  if (sol.empty()) {
    cout << -1 << endl;
    return;
  }

  Matrix<double> M(N * K + 1, N * K + 1);
  Matrix<double> v(N * K + 1, 1);
  if (S >= N * K) {
    for (int i = 0; i < N * K; i++) { M[0][i] = dp[K][i + 1]; }
    M[0][N * K] = 1.0;
    for (int i = 1; i < N * K; i++) { M[i][i - 1] = 1.0; }
    M[N * K][N * K] = 1.0;
    M = M.pow(S - N * K + 1);

    for (int i = 0; i < N * K; i++) { v[N * K - 1 - i][0] = sol[i]; }
    v[N * K][0] = 1.0;
  }

#undef double
  if (S < N * K) {
    cout << (double)sol[S] << endl;
  } else {
    cout << (double)(M * v)[0][0] << endl;
  }
  return;
}

int main() {
  fastio();
  if (!multitest) {
    solve();
  } else {
    cerr << "[Warning] Multi testcase mode on" << endl;
    int t;
    cin >> t;
    while (t--) solve();
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 4080
final_rank: 8

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)

#define popcount __builtin_popcount
#define next __next
#define prev __prev

#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...)                                                               \
  cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif

// ---------------- Utility ------------------

template <class T>
bool chmin(T &a, const T &b) {
  if (a <= b) return false;
  a = b;
  return true;
}

template <class T>
bool chmax(T &a, const T &b) {
  if (a >= b) return false;
  a = b;
  return true;
}

template <class T>
using MaxHeap = priority_queue<T>;

template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

// ----------------- Input -------------------

template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}

// ----------------- Output ------------------

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ',' << p.second;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &e : v) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
  for (const T &e : d) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{ ";
  for (const T &e : s) os << e << " ";
  return os << "}";
}

template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << "{ ";
  for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
  return os << "}";
}

template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
  (..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}

template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << "(";
  dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
  return os << ")";
}

void dump_str_rec(ostringstream &) {}

template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << ", " << head;
  dump_str_rec(oss, forward<Tail>(tail)...);
}

template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
  ostringstream oss;
  oss << arg;
  dump_str_rec(oss, forward<U>(args)...);
  return oss.str();
}

// --------------- Fast I/O ------------------

void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

// ------------------ ACL --------------------

// #include <atcoder/modint>
// constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
// using mint = atcoder::static_modint<MOD>;
// ostream &operator<<(ostream &os, const mint &v) {
//   return os << v.val();
// }

// ------------ End of template --------------

#define endl "\n"
using ll = long long;
using pii = pair<int, int>;

const bool multitest = false;

#define double __float128

// N変数連立方程式(Ax=bの形)を解く
//
// 引数のAは拡大係数行列. (Ax=bでAとbをそのまま並べてつなげたもの)
// すなわち
// A[i][0] * x_0 + A[i][1] * x_1 + ... A[i][n - 1] * x_{n-1} = A[i][n]
// (0 <= i < n)
//
//
// 関数が終わったときにAの左側は単位行列になる
// Note: 誤差に注意
//
// Verify:
// https://onlinejudge.u-aizu.ac.jp/solutions/problem/1328/review/4927454/okuraofvegetable/C++17
// https://onlinejudge.u-aizu.ac.jp/solutions/problem/2171/review/6045411/okuraofvegetable/C++17

double abs(double a) {
  if (a < 0.0) return -a;
  return a;
}

vector<double> solve_equation(vector<vector<double>> &A) {
  assert(A[0].size() == A.size() + 1);
  for (int i = 0; i < A.size(); i++) {
    if (abs(A[i][i]) < eps) {
      for (int j = i + 1; j < A.size(); j++) {
        if (abs(A[j][i]) > eps) {
          swap(A[i], A[j]);
          break;
        }
      }
    }
    double r = A[i][i];
    if (abs(r) < eps) {
      // dmp(r);
      return vector<double>();
    }
    for (int j = i; j <= A.size(); j++) { A[i][j] /= r; }
    for (int j = i + 1; j < A.size(); j++) {
      double s = A[j][i];
      for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * s; }
    }
  }
  for (int i = A.size() - 1; i > 0; i--) {
    for (int j = 0; j < i; j++) {
      double r = A[j][i];
      for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * r; }
    }
  }
  vector<double> sol;
  for (int i = 0; i < A.size(); i++) sol.push_back(A[i][A.size()]);
  return sol;
}

// Matrix class
//
// constructor of T : T(1),T(0) must be unit of T
//
// Verified: DDCC B
// https://atcoder.jp/contests/ddcc2020-final/submissions/9941022
//
// Verified: determinant
// https://judge.yosupo.jp/submission/45201
//

template <class T>
struct Matrix {
  int H, W;
  vector<vector<T>> elem;
  Matrix(int H, int W, T val = T(0)) : H(H), W(W) {
    elem.resize(H);
    for (int i = 0; i < H; i++) elem[i].assign(W, val);
  }
  Matrix(const vector<vector<T>> &m)
      : H(m.size()), W(m.size() ? m[0].size() : 0) {
    elem.resize(H);
    for (int i = 0; i < H; i++) elem[i].resize(W);
    for (int i = 0; i < H; i++) elem[i] = m[i];
  }
  vector<T> &operator[](int h) { return elem[h]; }
  static Matrix identity(int N) {
    Matrix a(N, N);
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          a.elem[i][i] = T(1);
        else
          a.elem[i][j] = T(0);
      }
    }
    return a;
  }
  Matrix operator+(const Matrix &m) const {
    assert(H == m.H && W == m.W);
    Matrix a(H, W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        a.elem[i][j] = this->elem[i][j] + m.elem[i][j];
      }
    }
    return a;
  }
  Matrix operator-(const Matrix &m) const {
    assert(H == m.H && W == m.W);
    Matrix a(H, W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        a.elem[i][j] = this->elem[i][j] - m.elem[i][j];
      }
    }
    return a;
  }
  Matrix operator*(const Matrix &m) const {
    assert(W == m.H);
    Matrix a(H, m.W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < m.W; j++) {
        for (int k = 0; k < W; k++) {
          a.elem[i][j] = a.elem[i][j] + (this->elem[i][k] * m.elem[k][j]);
        }
      }
    }
    return a;
  }
  Matrix &operator+=(const Matrix &m) { return *this = *this + m; }
  Matrix &operator-=(const Matrix &m) { return *this = *this - m; }
  Matrix &operator*=(const Matrix &m) { return *this = *this * m; }
  Matrix pow(long long x) const {
    Matrix a = identity(H);
    Matrix b = *this;
    while (x) {
      if (x & 1) a *= b;
      b *= b;
      x >>= 1;
    }
    return a;
  }

  T determinant() {
    Matrix m(*this);
    assert(m.W == m.H);
    T ret(1);
    for (int i = 0; i < m.H; i++) {
      int idx = -1;
      for (int j = i; j < m.H; j++) {
        if (m[j][i] != T(0)) idx = j;
      }
      if (idx == -1) return T(0);
      if (i != idx) {
        ret *= T(-1);
        swap(m[i], m[idx]);
      }
      ret *= m[i][i];
      T div = m[i][i];
      for (int j = i; j < m.W; j++) m[i][j] /= div;
      for (int j = i + 1; j < m.H; j++) {
        T a = m[j][i];
        for (int k = i; k < m.W; k++) m[j][k] -= m[i][k] * a;
      }
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const Matrix &x) {
    for (int i = 0; i < x.H; i++) {
      for (int j = 0; j < x.W; j++) {
        os << x.elem[i][j];
        if (j + 1 < x.W) cout << ' ';
      }
      if (i + 1 < x.H) cout << endl;
    }
    return os;
  }
  friend istream &operator>>(istream &is, Matrix &x) {
    for (auto &v : x.elem) is >> v;
    return is;
  }
};

void solve() {
  int S, N, K;
  cin >> S >> N >> K;
  if (S < 0) S *= -1;

  auto dp = vect(K + 1, vect(N * K + 1, (double)0.0));
  dp[0][0] = 1.0;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j <= N * K; j++) {
      for (int k = 1; k <= N; k++) {
        if (j + k <= N * K) dp[i + 1][j + k] += dp[i][j] * (1.0 / (double)N);
      }
    }
  }

  auto A = vect(N * K, vect(N * K + 1, (double)0.0));

  A[0][0] = 1.0;
  for (int i = 1; i < N * K; i++) {
    for (int j = 1; j <= N * K; j++) { A[i][abs(i - j)] += dp[K][j]; }
    A[i][i] -= 1.0;
    A[i][N * K] = -1.0;
  }

  // for (auto a : A) dmp(a);

  auto sol = solve_equation(A);
  // dmp(sol);
  if (sol.empty()) {
    cout << -1 << endl;
    return;
  }

  Matrix<double> M(N * K + 1, N * K + 1);
  Matrix<double> v(N * K + 1, 1);
  if (S >= N * K) {
    for (int i = 0; i < N * K; i++) { M[0][i] = dp[K][i + 1]; }
    M[0][N * K] = 1.0;
    for (int i = 1; i < N * K; i++) { M[i][i - 1] = 1.0; }
    M[N * K][N * K] = 1.0;
    M = M.pow(S - N * K + 1);

    for (int i = 0; i < N * K; i++) { v[N * K - 1 - i][0] = sol[i]; }
    v[N * K][0] = 1.0;
  }

#undef double
  if (S < N * K) {
    cout << (double)sol[S] << endl;
  } else {
    cout << (double)(M * v)[0][0] << endl;
  }
  return;
}

int main() {
  fastio();
  if (!multitest) {
    solve();
  } else {
    cerr << "[Warning] Multi testcase mode on" << endl;
    int t;
    cin >> t;
    while (t--) solve();
  }
  return 0;
}
accuracy: 0.1935483870967742
time_ms: 1440
memory_kb: 4148
final_rank: 18

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)

#define popcount __builtin_popcount
#define next __next
#define prev __prev

#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...)                                                               \
  cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif

// ---------------- Utility ------------------

template <class T>
bool chmin(T &a, const T &b) {
  if (a <= b) return false;
  a = b;
  return true;
}

template <class T>
bool chmax(T &a, const T &b) {
  if (a >= b) return false;
  a = b;
  return true;
}

template <class T>
using MaxHeap = priority_queue<T>;

template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

// ----------------- Input -------------------

template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}

// ----------------- Output ------------------

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ',' << p.second;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &e : v) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
  for (const T &e : d) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{ ";
  for (const T &e : s) os << e << " ";
  return os << "}";
}

template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << "{ ";
  for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
  return os << "}";
}

template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
  (..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}

template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << "(";
  dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
  return os << ")";
}

void dump_str_rec(ostringstream &) {}

template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << ", " << head;
  dump_str_rec(oss, forward<Tail>(tail)...);
}

template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
  ostringstream oss;
  oss << arg;
  dump_str_rec(oss, forward<U>(args)...);
  return oss.str();
}

// --------------- Fast I/O ------------------

void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

// ------------------ ACL --------------------

// #include <atcoder/modint>
// constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
// using mint = atcoder::static_modint<MOD>;
// ostream &operator<<(ostream &os, const mint &v) {
//   return os << v.val();
// }

// ------------ End of template --------------

#define endl "\n"
using ll = long long;
using pii = pair<int, int>;

const bool multitest = false;

#define double long double

// N変数連立方程式(Ax=bの形)を解く
//
// 引数のAは拡大係数行列. (Ax=bでAとbをそのまま並べてつなげたもの)
// すなわち
// A[i][0] * x_0 + A[i][1] * x_1 + ... A[i][n - 1] * x_{n-1} = A[i][n]
// (0 <= i < n)
//
//
// 関数が終わったときにAの左側は単位行列になる
// Note: 誤差に注意
//
// Verify:
// https://onlinejudge.u-aizu.ac.jp/solutions/problem/1328/review/4927454/okuraofvegetable/C++17
// https://onlinejudge.u-aizu.ac.jp/solutions/problem/2171/review/6045411/okuraofvegetable/C++17

vector<double> solve_equation(vector<vector<double>> &A) {
  assert(A[0].size() == A.size() + 1);
  for (int i = 0; i < A.size(); i++) {
    if (abs(A[i][i]) < eps) {
      for (int j = i + 1; j < A.size(); j++) {
        if (abs(A[j][i]) > eps) {
          swap(A[i], A[j]);
          break;
        }
      }
    }
    double r = A[i][i];
    if (abs(r) < eps) {
      dmp(r);
      return vector<double>();
    }
    for (int j = i; j <= A.size(); j++) { A[i][j] /= r; }
    for (int j = i + 1; j < A.size(); j++) {
      double s = A[j][i];
      for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * s; }
    }
  }
  for (int i = A.size() - 1; i > 0; i--) {
    for (int j = 0; j < i; j++) {
      double r = A[j][i];
      for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * r; }
    }
  }
  vector<double> sol;
  for (int i = 0; i < A.size(); i++) sol.push_back(A[i][A.size()]);
  return sol;
}

// Matrix class
//
// constructor of T : T(1),T(0) must be unit of T
//
// Verified: DDCC B
// https://atcoder.jp/contests/ddcc2020-final/submissions/9941022
//
// Verified: determinant
// https://judge.yosupo.jp/submission/45201
//

template <class T>
struct Matrix {
  int H, W;
  vector<vector<T>> elem;
  Matrix(int H, int W, T val = T(0)) : H(H), W(W) {
    elem.resize(H);
    for (int i = 0; i < H; i++) elem[i].assign(W, val);
  }
  Matrix(const vector<vector<T>> &m)
      : H(m.size()), W(m.size() ? m[0].size() : 0) {
    elem.resize(H);
    for (int i = 0; i < H; i++) elem[i].resize(W);
    for (int i = 0; i < H; i++) elem[i] = m[i];
  }
  vector<T> &operator[](int h) { return elem[h]; }
  static Matrix identity(int N) {
    Matrix a(N, N);
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          a.elem[i][i] = T(1);
        else
          a.elem[i][j] = T(0);
      }
    }
    return a;
  }
  Matrix operator+(const Matrix &m) const {
    assert(H == m.H && W == m.W);
    Matrix a(H, W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        a.elem[i][j] = this->elem[i][j] + m.elem[i][j];
      }
    }
    return a;
  }
  Matrix operator-(const Matrix &m) const {
    assert(H == m.H && W == m.W);
    Matrix a(H, W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        a.elem[i][j] = this->elem[i][j] - m.elem[i][j];
      }
    }
    return a;
  }
  Matrix operator*(const Matrix &m) const {
    assert(W == m.H);
    Matrix a(H, m.W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < m.W; j++) {
        for (int k = 0; k < W; k++) {
          a.elem[i][j] = a.elem[i][j] + (this->elem[i][k] * m.elem[k][j]);
        }
      }
    }
    return a;
  }
  Matrix &operator+=(const Matrix &m) { return *this = *this + m; }
  Matrix &operator-=(const Matrix &m) { return *this = *this - m; }
  Matrix &operator*=(const Matrix &m) { return *this = *this * m; }
  Matrix pow(long long x) const {
    Matrix a = identity(H);
    Matrix b = *this;
    while (x) {
      if (x & 1) a *= b;
      b *= b;
      x >>= 1;
    }
    return a;
  }

  T determinant() {
    Matrix m(*this);
    assert(m.W == m.H);
    T ret(1);
    for (int i = 0; i < m.H; i++) {
      int idx = -1;
      for (int j = i; j < m.H; j++) {
        if (m[j][i] != T(0)) idx = j;
      }
      if (idx == -1) return T(0);
      if (i != idx) {
        ret *= T(-1);
        swap(m[i], m[idx]);
      }
      ret *= m[i][i];
      T div = m[i][i];
      for (int j = i; j < m.W; j++) m[i][j] /= div;
      for (int j = i + 1; j < m.H; j++) {
        T a = m[j][i];
        for (int k = i; k < m.W; k++) m[j][k] -= m[i][k] * a;
      }
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const Matrix &x) {
    for (int i = 0; i < x.H; i++) {
      for (int j = 0; j < x.W; j++) {
        os << x.elem[i][j];
        if (j + 1 < x.W) cout << ' ';
      }
      if (i + 1 < x.H) cout << endl;
    }
    return os;
  }
  friend istream &operator>>(istream &is, Matrix &x) {
    for (auto &v : x.elem) is >> v;
    return is;
  }
};

void solve() {
  int S, N, K;
  cin >> S >> N >> K;
  if (S < 0) S *= -1;

  auto dp = vect(K + 1, vect(N * K + 1, 0.0));
  dp[0][0] = 1.0;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j <= N * K; j++) {
      for (int k = 1; k <= N; k++) {
        if (j + k <= N * K) dp[i + 1][j + k] += dp[i][j] * (1.0 / N);
      }
    }
  }

  auto A = vect(N * K, vect(N * K + 1, (double)0.0));

  A[0][0] = 1.0;
  for (int i = 1; i < N * K; i++) {
    for (int j = 1; j <= N * K; j++) { A[i][abs(i - j)] += dp[K][j]; }
    A[i][i] -= 1.0;
    A[i][N * K] = -1.0;
  }

  // for (auto a : A) dmp(a);

  auto sol = solve_equation(A);
  // dmp(sol);
  if (sol.empty()) {
    cout << -1 << endl;
    return;
  }

  if (S < N * K) {
    cout << sol[S] << endl;
    return;
  }

  Matrix<double> M(N * K + 1, N * K + 1);
  for (int i = 0; i < N * K; i++) { M[0][i] = dp[K][i + 1]; }
  M[0][N * K] = 1.0;
  for (int i = 1; i < N * K; i++) { M[i][i - 1] = 1.0; }
  M[N * K][N * K] = 1.0;
  M = M.pow(S - N * K + 1);

  Matrix<double> v(N * K + 1, 1);
  for (int i = 0; i < N * K; i++) { v[N * K - 1 - i][0] = sol[i]; }
  v[N * K][0] = 1.0;
  cout << (M * v)[0][0] << endl;

  return;
}

int main() {
  fastio();
  if (!multitest) {
    solve();
  } else {
    cerr << "[Warning] Multi testcase mode on" << endl;
    int t;
    cin >> t;
    while (t--) solve();
  }
  return 0;
}
accuracy: 0.1935483870967742
time_ms: 100
memory_kb: 4204
final_rank: 17

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define eps 1e-9
#define INF 2000000000              // 2e9
#define LLINF 2000000000000000000ll // 2e18 (llmax:9e18)
#define all(x) (x).begin(), (x).end()
#define sq(x) ((x) * (x))

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define drep(i, x) for (int i = ((int)(x)-1); i >= 0; i--)

#define popcount __builtin_popcount
#define next __next
#define prev __prev

#ifndef LOCAL
#define dmp(...) ;
#else
#define dmp(...)                                                               \
  cerr << "[ " << #__VA_ARGS__ << " ] : " << dump_str(__VA_ARGS__) << endl
#endif

// ---------------- Utility ------------------

template <class T>
bool chmin(T &a, const T &b) {
  if (a <= b) return false;
  a = b;
  return true;
}

template <class T>
bool chmax(T &a, const T &b) {
  if (a >= b) return false;
  a = b;
  return true;
}

template <class T>
using MaxHeap = priority_queue<T>;

template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

template <class T>
vector<T> vect(int len, T elem) {
  return vector<T>(len, elem);
}

// ----------------- Input -------------------

template <class T, class U>
istream &operator>>(istream &is, pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (int i = 0; i < vec.size(); i++) is >> vec[i];
  return is;
}

// ----------------- Output ------------------

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << p.first << ',' << p.second;
}

template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &e : v) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const deque<T> &d) {
  for (const T &e : d) os << e << " ";
  return os;
}

template <class T>
ostream &operator<<(ostream &os, const set<T> &s) {
  os << "{ ";
  for (const T &e : s) os << e << " ";
  return os << "}";
}

template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
  os << "{ ";
  for (const auto &[key, val] : m) os << "( " << key << " -> " << val << " ) ";
  return os << "}";
}

template <class TupleTy, size_t... I>
void dump_tuple(ostream &os, const TupleTy t, std::index_sequence<I...>) {
  (..., (os << (I == 0 ? "" : ",") << std::get<I>(t)));
}

template <class... Args>
ostream &operator<<(ostream &os, const tuple<Args...> &t) {
  os << "(";
  dump_tuple(os, t, std::make_index_sequence<sizeof...(Args)>());
  return os << ")";
}

void dump_str_rec(ostringstream &) {}

template <class Head, class... Tail>
void dump_str_rec(ostringstream &oss, Head &&head, Tail &&... tail) {
  oss << ", " << head;
  dump_str_rec(oss, forward<Tail>(tail)...);
}

template <class T, class... U>
string dump_str(T &&arg, U &&... args) {
  ostringstream oss;
  oss << arg;
  dump_str_rec(oss, forward<U>(args)...);
  return oss.str();
}

// --------------- Fast I/O ------------------

void fastio() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout << fixed << setprecision(20);
}

// ------------------ ACL --------------------

// #include <atcoder/modint>
// constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
// using mint = atcoder::static_modint<MOD>;
// ostream &operator<<(ostream &os, const mint &v) {
//   return os << v.val();
// }

// ------------ End of template --------------

#define endl "\n"
using ll = long long;
using pii = pair<int, int>;

const bool multitest = false;

// N変数連立方程式(Ax=bの形)を解く
//
// 引数のAは拡大係数行列. (Ax=bでAとbをそのまま並べてつなげたもの)
// すなわち
// A[i][0] * x_0 + A[i][1] * x_1 + ... A[i][n - 1] * x_{n-1} = A[i][n]
// (0 <= i < n)
//
//
// 関数が終わったときにAの左側は単位行列になる
// Note: 誤差に注意
//
// Verify:
// https://onlinejudge.u-aizu.ac.jp/solutions/problem/1328/review/4927454/okuraofvegetable/C++17
// https://onlinejudge.u-aizu.ac.jp/solutions/problem/2171/review/6045411/okuraofvegetable/C++17

vector<double> solve_equation(vector<vector<double>> &A) {
  assert(A[0].size() == A.size() + 1);
  for (int i = 0; i < A.size(); i++) {
    if (abs(A[i][i]) < eps) {
      for (int j = i + 1; j < A.size(); j++) {
        if (abs(A[j][i]) > eps) {
          swap(A[i], A[j]);
          break;
        }
      }
    }
    double r = A[i][i];
    if (abs(r) < eps) {
      dmp(r);
      return vector<double>();
    }
    for (int j = i; j <= A.size(); j++) { A[i][j] /= r; }
    for (int j = i + 1; j < A.size(); j++) {
      double s = A[j][i];
      for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * s; }
    }
  }
  for (int i = A.size() - 1; i > 0; i--) {
    for (int j = 0; j < i; j++) {
      double r = A[j][i];
      for (int k = i; k <= A.size(); k++) { A[j][k] -= A[i][k] * r; }
    }
  }
  vector<double> sol;
  for (int i = 0; i < A.size(); i++) sol.push_back(A[i][A.size()]);
  return sol;
}

// Matrix class
//
// constructor of T : T(1),T(0) must be unit of T
//
// Verified: DDCC B
// https://atcoder.jp/contests/ddcc2020-final/submissions/9941022
//
// Verified: determinant
// https://judge.yosupo.jp/submission/45201
//

template <class T>
struct Matrix {
  int H, W;
  vector<vector<T>> elem;
  Matrix(int H, int W, T val = T(0)) : H(H), W(W) {
    elem.resize(H);
    for (int i = 0; i < H; i++) elem[i].assign(W, val);
  }
  Matrix(const vector<vector<T>> &m)
      : H(m.size()), W(m.size() ? m[0].size() : 0) {
    elem.resize(H);
    for (int i = 0; i < H; i++) elem[i].resize(W);
    for (int i = 0; i < H; i++) elem[i] = m[i];
  }
  vector<T> &operator[](int h) { return elem[h]; }
  static Matrix identity(int N) {
    Matrix a(N, N);
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j)
          a.elem[i][i] = T(1);
        else
          a.elem[i][j] = T(0);
      }
    }
    return a;
  }
  Matrix operator+(const Matrix &m) const {
    assert(H == m.H && W == m.W);
    Matrix a(H, W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        a.elem[i][j] = this->elem[i][j] + m.elem[i][j];
      }
    }
    return a;
  }
  Matrix operator-(const Matrix &m) const {
    assert(H == m.H && W == m.W);
    Matrix a(H, W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        a.elem[i][j] = this->elem[i][j] - m.elem[i][j];
      }
    }
    return a;
  }
  Matrix operator*(const Matrix &m) const {
    assert(W == m.H);
    Matrix a(H, m.W);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < m.W; j++) {
        for (int k = 0; k < W; k++) {
          a.elem[i][j] = a.elem[i][j] + (this->elem[i][k] * m.elem[k][j]);
        }
      }
    }
    return a;
  }
  Matrix &operator+=(const Matrix &m) { return *this = *this + m; }
  Matrix &operator-=(const Matrix &m) { return *this = *this - m; }
  Matrix &operator*=(const Matrix &m) { return *this = *this * m; }
  Matrix pow(long long x) const {
    Matrix a = identity(H);
    Matrix b = *this;
    while (x) {
      if (x & 1) a *= b;
      b *= b;
      x >>= 1;
    }
    return a;
  }

  T determinant() {
    Matrix m(*this);
    assert(m.W == m.H);
    T ret(1);
    for (int i = 0; i < m.H; i++) {
      int idx = -1;
      for (int j = i; j < m.H; j++) {
        if (m[j][i] != T(0)) idx = j;
      }
      if (idx == -1) return T(0);
      if (i != idx) {
        ret *= T(-1);
        swap(m[i], m[idx]);
      }
      ret *= m[i][i];
      T div = m[i][i];
      for (int j = i; j < m.W; j++) m[i][j] /= div;
      for (int j = i + 1; j < m.H; j++) {
        T a = m[j][i];
        for (int k = i; k < m.W; k++) m[j][k] -= m[i][k] * a;
      }
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const Matrix &x) {
    for (int i = 0; i < x.H; i++) {
      for (int j = 0; j < x.W; j++) {
        os << x.elem[i][j];
        if (j + 1 < x.W) cout << ' ';
      }
      if (i + 1 < x.H) cout << endl;
    }
    return os;
  }
  friend istream &operator>>(istream &is, Matrix &x) {
    for (auto &v : x.elem) is >> v;
    return is;
  }
};

void solve() {
  int S, N, K;
  cin >> S >> N >> K;
  if (S < 0) S *= -1;

  auto dp = vect(K + 1, vect(N * K + 1, 0.0));
  dp[0][0] = 1.0;
  for (int i = 0; i < K; i++) {
    for (int j = 0; j <= N * K; j++) {
      for (int k = 1; k <= N; k++) {
        if (j + k <= N * K) dp[i + 1][j + k] += dp[i][j] * (1.0 / N);
      }
    }
  }

  auto A = vect(N * K, vect(N * K + 1, 0.0));

  A[0][0] = 1.0;
  for (int i = 1; i < N * K; i++) {
    for (int j = 1; j <= N * K; j++) { A[i][abs(i - j)] += dp[K][j]; }
    A[i][i] -= 1.0;
    A[i][N * K] = -1.0;
  }

  // for (auto a : A) dmp(a);

  auto sol = solve_equation(A);
  // dmp(sol);
  if (sol.empty()) {
    cout << -1 << endl;
    return;
  }

  if (S < N * K) {
    cout << sol[S] << endl;
    return;
  }

  Matrix<double> M(N * K + 1, N * K + 1);
  for (int i = 0; i < N * K; i++) { M[0][i] = dp[K][i + 1]; }
  M[0][N * K] = 1.0;
  for (int i = 1; i < N * K; i++) { M[i][i - 1] = 1.0; }
  M[N * K][N * K] = 1.0;
  M = M.pow(S - N * K + 1);

  Matrix<double> v(N * K + 1, 1);
  for (int i = 0; i < N * K; i++) { v[N * K - 1 - i][0] = sol[i]; }
  v[N * K][0] = 1.0;
  cout << (M * v)[0][0] << endl;

  return;
}

int main() {
  fastio();
  if (!multitest) {
    solve();
  } else {
    cerr << "[Warning] Multi testcase mode on" << endl;
    int t;
    cin >> t;
    while (t--) solve();
  }
  return 0;
}
accuracy: 0.1935483870967742
time_ms: 30
memory_kb: 3836
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=105,INF=1<<29;

const double eps=1e-8;
typedef vector<double> vec;
typedef vector<vec> mat;

vec gauss_jordan(const mat& A,const vec &b){
    int n=A.size();
    mat B(n,vec(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) return vec();
        
        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];
            }
        }
    }
    
    vec x(n);
    
    for(int i=0;i<n;i++) x[i]=B[i][n];
    return x;
}

mat mul(mat &A, mat&B){
    mat C(A.size(),vec(B[0].size()));
    for(int i=0;i<A.size();i++){
        for(int k=0;k<B.size();k++){
            for(int j=0;j<B[0].size();j++){
                C[i][j]+=A[i][k]*B[k][j];
            }
        }
    }
    return C;
}

mat pow(mat A,ll n){
    mat B(A.size(),vec(A.size()));
    for(int i=0;i<A.size();i++){
        B[i][i]=1;
    }
    
    while(n>0){
        if(n&1) B=mul(B,A);
        A=mul(A,A);
        n/=2;
    }
    return B;
}


double dp[MAX][MAX];

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int S,N,K;cin>>S>>N>>K;
    S=abs(S);
    if(S==0){
        cout<<0<<endl;
        return 0;
    }
    if(N==1){
        if(S%K==0) cout<<S/K<<endl;
        else cout<<-1<<endl;
        return 0;
    }
    
    dp[0][0]=1;
    for(int i=0;i<K;i++){
        for(int j=0;j<=i*N;j++){
            for(int k=1;k<=N;k++){
                dp[i+1][j+k]+=dp[i][j]/N;
            }
        }
    }
    
    mat A(N*K+1,vec(N*K+1));
    vec B(N*K+1,1);
    A[0][0]=1;
    B[0]=0;
    
    for(int i=1;i<=N*K;i++){
        A[i][i]=1;
        for(int j=1;j<=N*K;j++){
            A[i][abs(i-j)]-=dp[K][j];
        }
    }
    
    auto C=gauss_jordan(A,B);
    
    if(S<=N*K){
        cout<<fixed<<setprecision(25)<<C[S]<<endl;
        return 0;
    }
    
    mat D(N*K+1,vec(N*K+1));
    for(int j=0;j<N*K;j++) D[0][j]=dp[K][j+1];
    D[0][N*K]=1;
    for(int i=1;i<N*K;i++){
        D[i][i-1]=1;
    }
    D[N*K][N*K]=1;
    
    D=pow(D,S-1);
    
    mat E(N*K+1,vec(1));
    for(int i=0;i<N*K;i++) E[i][0]=C[N*K-i];
    E[N*K][0]=1;
    
    auto F=mul(D,E);
    
    cout<<fixed<<setprecision(25)<<F[N*K-1][0]<<endl;
    
    
}
accuracy: 1
time_ms: 20
memory_kb: 3936
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
vector<vector<double>> matrix_multiplication(vector<vector<double>> &A, vector<vector<double>> &B){
  int N = A.size();
  vector<vector<double>> C(N, vector<double>(N, 0));
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      for (int k = 0; k < N; k++){
        C[i][k] += A[i][j] * B[j][k];
      }
    }
  }
  return C;
}
vector<vector<double>> matrix_exponentiation(vector<vector<double>> &A, int x){
  int N = A.size();
  vector<vector<double>> B(N, vector<double>(N, 0));
  for (int i = 0; i < N; i++){
    B[i][i] = 1;
  }
  while (x > 0){
    if (x % 2 == 1){
      B = matrix_multiplication(B, A);
    }
    A = matrix_multiplication(A, A);
    x /= 2;
  }
  return B;
}
int main(){
  cout << fixed << setprecision(20);
  int S, N, K;
  cin >> S >> N >> K;
  if (N == 1){
    if (S % K != 0){
      cout << -1 << endl;
    } else {
      cout << abs(S) / K << endl;
    }
  } else {
    vector<vector<double>> dp(K + 1, vector<double>(N * K + 1, 0));
    dp[0][0] = 1;
    for (int i = 0; i < K; i++){
      for (int j = i; j <= i * N; j++){
        for (int k = 1; k <= N; k++){
          dp[i + 1][j + k] += dp[i][j] / N;
        }
      }
    }
    vector<vector<double>> A(N * K, vector<double>(N * K + 1, 0));
    for (int i = 0; i < N * K; i++){
      A[i][i] = 1;
      if (i > 0){
        A[i][N * K] = 1;
        for (int j = K; j <= N * K; j++){
          int i2 = abs(i - j);
          A[i][i2] -= dp[K][j];
        }
      }
    }
    for (int i = 0; i < N * K; i++){
      int p = i;
      for (int j = i + 1; j < N * K; j++){
        if (abs(A[j][i]) > abs(A[p][i])){
          p = j;
        }
      }
      swap(A[i], A[p]);
      for (int j = N * K; j >= i; j--){
        A[i][j] /= A[i][i];
      }
      for (int j = 0; j < N * K; j++){
        if (j != i){
          for (int k = N * K; k >= i; k--){
            A[j][k] -= A[j][i] * A[i][k];
          }
        }
      }
    }
    vector<double> B(N * K);
    for (int i = 0; i < N * K; i++){
      B[i] = A[i][N * K];
    }
    S = abs(S);
    if (S < N * K){
      cout << B[S] << endl;
    } else {
      vector<vector<double>> M(N * K + 1, vector<double>(N * K + 1, 0));
      for (int i = K; i <= N * K; i++){
        M[i - 1][0] = dp[K][i];
      }
      M[N * K][0] = 1;
      for (int i = 1; i < N * K; i++){
        M[i - 1][i] = 1;
      }
      M[N * K][N * K] = 1;
      M = matrix_exponentiation(M, S - N * K + 1);
      double ans = 0;
      for (int i = 0; i < N * K; i++){
        ans += M[i][0] * B[N * K - 1 - i];
      }
      ans += M[N * K][0];
      cout << ans << endl;
    }
  }
}
accuracy: 1
time_ms: 20
memory_kb: 3772
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
vector<vector<double>> matrix_multiplication(vector<vector<double>> &A, vector<vector<double>> &B){
  int N = A.size();
  vector<vector<double>> C(N, vector<double>(N, 0));
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      for (int k = 0; k < N; k++){
        C[i][k] += A[i][j] * B[j][k];
      }
    }
  }
  return C;
}
vector<vector<double>> matrix_exponentiation(vector<vector<double>> &A, int x){
  int N = A.size();
  vector<vector<double>> B(N, vector<double>(N, 0));
  for (int i = 0; i < N; i++){
    B[i][i] = 1;
  }
  while (x > 0){
    if (x % 2 == 1){
      B = matrix_multiplication(B, A);
    }
    A = matrix_multiplication(A, A);
    x /= 2;
  }
  return B;
}
int main(){
  cout << fixed << setprecision(5);
  int S, N, K;
  cin >> S >> N >> K;
  vector<vector<double>> dp(K + 1, vector<double>(N * K + 1, 0));
  dp[0][0] = 1;
  for (int i = 0; i < K; i++){
    for (int j = i; j <= i * N; j++){
      for (int k = 1; k <= N; k++){
        dp[i + 1][j + k] += dp[i][j] / N;
      }
    }
  }
  vector<vector<double>> A(N * K, vector<double>(N * K + 1, 0));
  for (int i = 0; i < N * K; i++){
    A[i][i] = 1;
    if (i > 0){
      A[i][N * K] = 1;
      for (int j = K; j <= N * K; j++){
        int i2 = abs(i - j);
        A[i][i2] -= dp[K][j];
      }
    }
  }
 for (int i = 0; i < N * K; i++){
    int p = i;
    for (int j = i + 1; j < N * K; j++){
      if (abs(A[j][i]) > abs(A[p][i])){
        p = j;
      }
    }
    swap(A[i], A[p]);
    for (int j = N * K; j >= i; j--){
      A[i][j] /= A[i][i];
    }
    for (int j = 0; j < N * K; j++){
      if (j != i){
        for (int k = N * K; k >= i; k--){
          A[j][k] -= A[j][i] * A[i][k];
        }
      }
    }
  }
  vector<double> B(N * K);
  for (int i = 0; i < N * K; i++){
    B[i] = A[i][N * K];
  }
  S = abs(S);
  if (S < N * K){
    cout << B[S] << endl;
  } else {
    vector<vector<double>> M(N * K + 1, vector<double>(N * K + 1, 0));
    for (int i = K; i <= N * K; i++){
      M[i - 1][0] = dp[K][i];
    }
    M[N * K][0] = 1;
    for (int i = 1; i < N * K; i++){
      M[i - 1][i] = 1;
    }
    M[N * K][N * K] = 1;
    M = matrix_exponentiation(M, S - N * K + 1);
    double ans = 0;
    for (int i = 0; i < N * K; i++){
      ans += M[i][0] * B[N * K - 1 - i];
    }
    ans += M[N * K][0];
    cout << ans << endl;
  }
}
accuracy: 0.1935483870967742
time_ms: 20
memory_kb: 3864
final_rank: 15

code_snippet: /*	author:  Kite_kuma
	created: 2021.09.10 16:11:11 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

#pragma region macros

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))

#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

#pragma endregion

#pragma region aliases

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;

#pragma endregion

#pragma region constants

const int inf = 1e9;
const long long INF = 1e18;
const long double pi = acos(-1);
const char dl = '\n';
const char sp = ' ';
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int mod_1000000007 = 1000000007;
const int mod_998244353 = 998244353;

#pragma endregion

#pragma region basic_operation

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool f = 1) { std::cout << (f ? "Yes" : "No") << '\n'; }
void No() { std::cout << "No\n"; }
void YES(bool f = 1) { std::cout << (f ? "YES" : "NO") << '\n'; }
void NO() { std::cout << "NO\n"; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

void err(bool flag = true) {
	if(!flag) return;
	std::cout << -1 << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(T x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T val) {
	for(auto &a : v) a += val;
	return;
}

template <class T>
T dup(T a, T b) {
	assert(b != 0);
	return (a + b - 1) / b;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(std::vector<T> &a) {
	auto vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}

#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

namespace viewer {
void view(const long long e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ")";
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ")";
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		std::cerr << ", ";
		view(c.second);
		std::cerr << ")\n";
	}
	std::cerr << "}";
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}
}  // namespace viewer

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                      \
	do {                                             \
		std::cerr << __LINE__ << " " << #x << " : "; \
		viewer::view(x);                             \
		std::cerr << '\n';                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

#pragma region geometry_light

using Real = long double;
using R = Real;
using Point = std::complex<Real>;
using Vec = Point;
const Real EPS = 1e-10, PI = acos(-1);

inline bool eq(const Real &a, const Real &b) { return fabs(b - a) < EPS; }
inline bool eq(const Point &a, const Point &b) { return fabs(b - a) < EPS; }
/*
	-1: a < b
	0 : a == b
	1 : a > b
*/
inline int compare(const Real &a, const Real &b) { return eq(a, b) ? 0 : a < b ? -1 : 1; }
inline int sign(const Real &a) { return fabs(a) < EPS ? 0 : a < 0 ? -1 : 1; }

namespace std {
const Point operator*(const Point &p, const Real &d) { return Point(real(p) * d, imag(p) * d); }
}  // namespace std

std::istream &operator>>(std::istream &is, Point &p) {
	Real a, b;
	is >> a >> b;
	p = Point(a, b);
	return is;
}
std::ostream &operator<<(std::ostream &os, Point &p) { return os << std::fixed << std::setprecision(10) << p.real() << " " << p.imag(); }

// rotate point 'p' for counter clockwise direction
const Point rotate(const Real &theta, const Point &p) {
	return Point(cos(theta) * p.real() - sin(theta) * p.imag(), sin(theta) * p.real() + cos(theta) * p.imag());
}

const Real radian_to_degree(const Real &r) { return (r * 180.0 / PI); }

const Real degree_to_radian(const Real &d) { return (d * PI / 180.0); }

// get angle a-b-c (<pi)
const Real get_angle(const Point &a, const Point &b, const Point &c) {
	const Point v(a - b), w(c - b);
	Real theta = fabs(atan2(w.imag(), w.real()) - atan2(v.imag(), v.real()));
	return std::min(theta, 2 * PI - theta);
}

namespace std {
bool operator<(const Point &a, const Point &b) { return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag(); }
}  // namespace std

struct Line {
	Point a, b;

	Line() = default;

	Line(Point a, Point b) : a(a), b(b) {}

	Line(Real A, Real B, Real C)  // Ax + By = C
	{
		if(eq(A, 0))
			a = Point(0, C / B), b = Point(1, C / B);
		else if(eq(B, 0))
			b = Point(C / A, 0), b = Point(C / A, 1);
		else
			a = Point(0, C / B), b = Point(C / A, 0);
	}

	friend std::ostream &operator<<(std::ostream &os, Line &p) { return os << p.a << " -- " << p.b; }

	friend std::istream &operator>>(std::istream &is, Line &a) { return is >> a.a >> a.b; }
};

struct Segment : Line {
	Segment() = default;

	Segment(Point a, Point b) : Line(a, b) {}
};

struct Circle {
	Point p;
	Real r;

	Circle() = default;

	Circle(Point p, Real r) : p(p), r(r) {}
};

using Points = std::vector<Point>;
using Polygon = std::vector<Point>;
using Segments = std::vector<Segment>;
using Lines = std::vector<Line>;
using Circles = std::vector<Circle>;

const Real cross(const Point &a, const Point &b) { return real(a) * imag(b) - imag(a) * real(b); }
const Real dot(const Point &a, const Point &b) { return real(a) * real(b) + imag(a) * imag(b); }

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_C
const int ccw(const Point &a, Point b, Point c) {
	b = b - a, c = c - a;
	if(cross(b, c) > EPS) return +1;		// "COUNTER_CLOCKWISE"
	if(cross(b, c) < -EPS) return -1;		// "CLOCKWISE"
	if(dot(b, c) < -EPS) return +2;			// "ONLINE_BACK"
	if(norm(b) + EPS < norm(c)) return -2;	// "ONLINE_FRONT"
	return 0;								// "ON_SEGMENT"
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A
bool parallel(const Line &a, const Line &b) { return eq(cross(a.b - a.a, b.b - b.a), 0.0); }

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A
bool orthogonal(const Line &a, const Line &b) { return eq(dot(a.a - a.b, b.a - b.b), 0.0); }

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_A
Point projection(const Line &l, const Point &p) {
	double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
	return l.a + (l.a - l.b) * t;
}

Point projection(const Segment &l, const Point &p) {
	double t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b);
	return l.a + (l.a - l.b) * t;
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_1_B
Point reflection(const Line &l, const Point &p) { return projection(l, p) * 2.0 - p; }

int intersect(const Line &l, const Point &p) { return int(abs(ccw(l.a, l.b, p)) != 1); }

int intersect(const Line &l, const Line &m) {
	if(intersect(l, m.a) && intersect(l, m.b)) return -1;
	return int(abs(cross(l.b - l.a, m.b - m.a)) > EPS);
}

int intersect(const Segment &s, const Point &p) { return int(ccw(s.a, s.b, p) == 0); }

int intersect(const Line &l, const Segment &s) {
	if(intersect(l, s.a) && intersect(l, s.b)) return -1;
	return cross(l.b - l.a, s.a - l.a) * cross(l.b - l.a, s.b - l.a) < EPS;
}

Real distance(const Line &l, const Point &p);

int intersect(const Circle &c, const Line &l) {
	Real d = c.r - distance(l, c.p);
	return fabs(d) < EPS ? 1 : d > 0. ? 2 : 0;
}

int intersect(const Circle &c, const Point &p) { return int(abs(abs(p - c.p) - c.r) < EPS); }

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_B
int intersect(const Segment &s, const Segment &t) {
	if(eq(s.a, s.b)) return intersect(t, s.a);
	if(intersect(Line(s), t.a) && intersect(Line(s), t.b) &&
	   std::max(std::min(s.a, s.b), std::min(t.a, t.b)) < std::min(std::max(s.a, s.b), std::max(t.a, t.b)))
		return -1;
	return int(ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0);
}

int intersect(const Circle &c, const Segment &l) {
	const Point h = projection(l, c.p);
	if(norm(h - c.p) - c.r * c.r > EPS) return 0;
	auto d1 = abs(c.p - l.a), d2 = abs(c.p - l.b);
	if(compare(d1, c.r) == -1 && compare(d2, c.r) == -1) return 0;
	if(d1 < c.r - EPS && d2 > c.r - EPS || d1 > c.r - EPS && d2 < c.r - EPS) return 1;
	return dot(l.a - h, l.b - h) < 0 ? 2 : 0;
}

Real distance(const Point &a, const Point &b);

int number_of_common_tangents(const Circle &c1, const Circle &c2) {
	Real r1 = std::min(c1.r, c2.r), r2 = std::max(c1.r, c2.r), d = distance(c1.p, c2.p);
	int com = compare(r1 + r2, d);
	return com == 1 ? compare(d + r1, r2) + 1 : 3 - com;
	// if(c1.r < c2.r) swap(c1, c2);
	// Real d = abs(c1.p - c2.p);
	// if(compare(c1.r + c2.r, d) == -1) return 4;
	// if(eq(c1.r + c2.r, d)) return 3;
	// if(compare(c1.r - c2.r, d) == -1) return 2;
	// return int(eq(c1.r - c2.r, d));
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_A&lang=jp
int intersect(const Circle &c1, const Circle &c2) { return 2 - abs(2 - number_of_common_tangents(c1, c2)); }

Real distance(const Point &a, const Point &b) { return abs(a - b); }

Real distance(const Line &l, const Point &p) { return abs(p - projection(l, p)); }

Real distance(const Line &l, const Line &m) { return intersect(l, m) ? 0 : distance(l, m.a); }

Real distance(const Segment &s, const Point &p) {
	Point r = projection(s, p);
	return intersect(s, r) ? distance(r, p) : std::min(abs(s.a - p), abs(s.b - p));
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_D
Real distance(const Segment &a, const Segment &b) {
	if(intersect(a, b)) return 0;
	return std::min({distance(a, b.a), distance(a, b.b), distance(b, a.a), distance(b, a.b)});
}

Real distance(const Line &l, const Segment &s) {
	if(intersect(l, s)) return 0;
	return std::min(distance(l, s.a), distance(l, s.b));
}

Point crosspoint(const Line &l, const Line &m) {
	Real A = cross(l.b - l.a, m.b - m.a);
	Real B = cross(l.b - l.a, l.b - m.a);
	if(eq(abs(A), 0.0) && eq(abs(B), 0.0)) return m.a;
	return m.a + (m.b - m.a) * B / A;
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_C
Point crosspoint(const Segment &l, const Segment &m) { return crosspoint(Line(l), Line(m)); }

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_D
std::pair<Point, Point> crosspoint(const Circle &c, const Line l) {
	Point pr = projection(l, c.p);
	if(eq(distance(l, c.p), c.r)) return {pr, pr};
	Vec v = (l.b - l.a) / abs(l.b - l.a) * sqrt(c.r * c.r - norm(pr - c.p));
	return make_pair(pr - v, pr + v);
	// Vec e = (l.b - l.a) / abs(l.b - l.a);
	// double base = sqrt(c.r * c.r - norm(pr - c.p));
	// return {pr - e * base, pr + e * base};
}

std::pair<Point, Point> crosspoint(const Circle &c, const Segment &l) {
	if(intersect(c, l) == 2) return crosspoint(c, Line(l.a, l.b));
	auto ret = crosspoint(c, Line(l.a, l.b));
	if(dot(l.a - ret.first, l.b - ret.first) < EPS)
		ret.second = ret.first;
	else
		ret.first = ret.second;
	return ret;
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_E
std::pair<Point, Point> crosspoint(const Circle &c1, const Circle &c2) {
	Real d = abs(c1.p - c2.p);
	Real a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));  // cosine theorem
	Real t = atan2(c2.p.imag() - c1.p.imag(), c2.p.real() - c1.p.real());
	Point p1 = c1.p + Point(cos(t + a) * c1.r, sin(t + a) * c1.r);
	Point p2 = c1.p + Point(cos(t - a) * c1.r, sin(t - a) * c1.r);
	return make_pair(p1, p2);
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_F
// "点 p を通る円 c の接線"の接点2つ
std::pair<Point, Point> tangent(const Circle &c1, const Point &p2) {
	return crosspoint(c1, Circle(p2, sqrt(norm(c1.p - p2) - c1.r * c1.r)));
}

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_7_G
// 円 c1, c2 の共通接線
Lines tangent(Circle c1, Circle c2) {
	Lines ret;
	if(c1.r < c2.r) std::swap(c1, c2);
	Real g = norm(c1.p - c2.p);
	if(eq(g, 0)) return ret;
	Vec u = (c2.p - c1.p) / Real(sqrt(g));
	Vec v = rotate(PI * 0.5, u);
	for(int s : {-1, 1}) {
		Real h = (c1.r + s * c2.r) / sqrt(g);
		if(eq(1 - h * h, 0)) {
			ret.emplace_back(c1.p + u * c1.r, c1.p + (u + v) * c1.r);
		} else if(1 - h * h > 0) {
			Point uu = u * h, vv = v * sqrt(1 - h * h);
			ret.emplace_back(c1.p + (uu + vv) * c1.r, c2.p - (uu + vv) * c2.r * s);
			ret.emplace_back(c1.p + (uu - vv) * c1.r, c2.p - (uu - vv) * c2.r * s);
		}
	}
	return ret;
}

#pragma endregion

#pragma region matrix

template <typename T>
struct Matrix {
	std::vector<std::vector<T>> A;

   private:
	static Matrix I(size_t n) {
		Matrix mat(n);
		for(int i = 0; i < n; i++) mat[i][i] = 1;
		return mat;
	}

   public:
	Matrix() = default;

	Matrix(std::vector<std::vector<T>> &vvec) { A = vvec; }

	Matrix(size_t n, size_t m) : A(n, std::vector<T>(m, 0)) {}

	Matrix(size_t n, size_t m, T init) : A(n, std::vector<T>(m, init)) {}

	Matrix(size_t n, std::vector<T> &vec) : A(n, vec) {}

	Matrix(size_t n) : A(n, std::vector<T>(n, 0)) {}

	int height() const { return A.size(); }

	int width() const { return A[0].size(); }

	inline const std::vector<T> &operator[](int k) const { return A[k]; }

	inline std::vector<T> &operator[](int k) { return A[k]; }

	Matrix &operator+=(const Matrix &B) {
		size_t n = height(), m = width();
		assert(n == B.height() && m == B.width());
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++) (*this)[i][j] += B[i][j];
		return *this;
	}

	Matrix &operator-=(const Matrix &B) {
		size_t n = height(), m = width();
		assert(n == B.height() && m == B.width());
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++) (*this)[i][j] -= B[i][j];
		return *this;
	}

	Matrix &operator*=(const Matrix &B) {
		size_t n = height(), m = B.width(), p = width();
		assert(p == B.height());
		std::vector<std::vector<T>> C(n, std::vector<T>(m, 0));
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++)
				for(int k = 0; k < p; k++) C[i][j] += (*this)[i][k] * B[k][j];
		A.swap(C);
		return (*this);
	}

	Matrix &operator^=(long long k) {
		Matrix B = Matrix::I(height());
		while(k > 0) {
			if(k & 1) B *= (*this);
			*this *= *this;
			k >>= 1;
		}
		A.swap(B.A);
		return *this;
	}

	bool operator==(const Matrix &B) {
		size_t n = height(), m = width();
		if(n != B.height() or m != B.width()) return false;
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++)
				if((*this)[i][j] != B[i][j]) return false;
		return true;
	}

	Matrix operator+(const Matrix &B) const { return (Matrix(*this) += B); }

	Matrix operator-(const Matrix &B) const { return (Matrix(*this) -= B); }

	Matrix operator*(const Matrix &B) const { return (Matrix(*this) *= B); }

	Matrix operator^(const long long k) const { return (Matrix(*this) ^= k); }

	Matrix &operator+=(const T &t) {
		int n = height(), m = width();
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++) (*this)[i][j] += t;
		return *this;
	}

	Matrix &operator-=(const T &t) {
		int n = height(), m = width();
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++) (*this)[i][j] -= t;
		return *this;
	}

	Matrix &operator*=(const T &t) {
		int n = height(), m = width();
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++) (*this)[i][j] *= t;
		return *this;
	}

	Matrix &operator/=(const T &t) {
		int n = height(), m = width();
		for(int i = 0; i < n; i++)
			for(int j = 0; j < m; j++) (*this)[i][j] /= t;
		return *this;
	}

	Matrix operator+(const T &t) const { return (Matrix(*this) += t); }

	Matrix operator-(const T &t) const { return (Matrix(*this) -= t); }

	Matrix operator*(const T &t) const { return (Matrix(*this) *= t); }

	Matrix operator/(const T &t) const { return (Matrix(*this) /= t); }

	friend std::ostream &operator<<(std::ostream &os, Matrix &p) {
		size_t n = p.height(), m = p.width();
		for(int i = 0; i < n; i++) {
			os << '[';
			for(int j = 0; j < m; j++) os << p[i][j] << (j == m - 1 ? "]\n" : ",");
		}
		return (os);
	}

	T determinant() {
		Matrix B(*this);
		size_t n = height(), m = width();
		assert(n == m);
		T ret = 1;
		for(int i = 0; i < n; i++) {
			int idx = -1;
			for(int j = i; j < n; j++)
				if(B[j][i] != 0) idx = j;
			if(idx == -1) return 0;
			if(i != idx) {
				ret *= -1;
				swap(B[i], B[idx]);
			}
			ret *= B[i][i];
			T vv = B[i][i];
			for(int j = 0; j < n; j++) B[i][j] /= vv;
			for(int j = i + 1; j < n; j++) {
				T a = B[j][i];
				for(int k = 0; k < n; k++) {
					B[j][k] -= B[i][k] * a;
				}
			}
		}
		return ret;
	}
};

#pragma endregion

ld solve(ll s) {
	s = abs(s);
	int n, k;
	cin >> n >> k;
	if(!s) return 0;
	if(n == 1) {
		if(s % k != 0) return -1;
		return s / k;
	}

	int jump_max = n * k;

	V<ld> jump_prob(jump_max + 1);
	jump_prob[0] = 1.;

	rep(k) {
		auto tmp = jump_prob;
		tmp.assign(jump_max + 1, 0);
		rep(from, jump_max + 1) {
			rep(leap_len, 1, n + 1) {
				if(from + leap_len <= jump_max) {
					tmp[from + leap_len] += jump_prob[from] / n;
				}
			}
		}

		swap(tmp, jump_prob);
	}

	VV<ld> mat(jump_max, V<ld>(jump_max));

	rep(from, 1, jump_max) {
		rep(leap_len, 1, jump_max + 1) {
			int to = abs(from - leap_len);
			if(to) {
				auto &tmp = mat[from][to];
				tmp += jump_prob[leap_len];
			}
		}
	}

	rep(i, 1, jump_max) rep(j, 1, jump_max) { mat[i][j] = bool(i == j) - mat[i][j]; }

	mat.erase(mat.begin());
	foa(t, mat) t.erase(t.begin());

	V<ld> res(jump_max - 1, 1.);

	int len = jump_max - 1;

	ld EPS = 1e-5;
	auto nonzero = [&](ld a) { return abs(a) > EPS; };

	auto mult_row = [&](int row_idx, ld mult) {
		rep(j, len) { mat[row_idx][j] *= mult; }
		res[row_idx] *= mult;
		return;
	};

	auto make_one = [&](int row_idx, int col_idx) {
		assert(mat[row_idx][col_idx] != 0);
		mult_row(row_idx, 1. / mat[row_idx][col_idx]);
		mat[row_idx][col_idx] = 1;
		return;
	};

	auto add_mult = [&](int row_from, int row_to, ld mult) {
		rep(j, len) { mat[row_to][j] += mat[row_from][j] * mult; }
		res[row_to] += res[row_from] * mult;
		return;
	};

	auto swap_row = [&](int i, int j) {
		swap(mat[i], mat[j]);
		swap(res[i], res[j]);
		return;
	};

	auto find_nonzero = [&](int col_idx, int row_start) {
		rep(i, row_start, len) {
			if(nonzero(mat[i][col_idx])) return i;
		}
		return -1;
	};

	debug(len);
	rep(col, len) {
		auto idx = find_nonzero(col, col);
		if(idx < 0) {
			continue;
		}
		if(idx != col) swap_row(idx, col);
		make_one(col, col);
		rep(row, len) {
			if(col != row) {
				add_mult(col, row, -mat[row][col]);
			}
		}
	}

	debug(res);

	res.insert(res.begin(), 0.);
	res.insert(res.begin(), 1.);

	if(s < n * k) return res[s + 1];
	Matrix<ld> a(n * k + 1);

	rep(i, 1, n * k) { a[i][i + 1] = 1; }
	a[0][0] = 1;
	a[n * k][0] = 1;
	rep(i, 1, n * k + 1) { a[n * k][i] = jump_prob[n * k - i + 1]; }

	debug(jump_prob);
	Matrix<ld> b(n * k + 1, 1);
	rep(i, n * k + 1) b[i][0] = res[i];

	debug(a.A, b.A, s);

	a ^= s;

	debug(a.A);
	a *= b;

	debug(a.A);

	return a[1][0];

	// return;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout << std::fixed << std::setprecision(15);
	srand((unsigned)time(NULL));

	int n;
	while(cin >> n) {
		ld res = solve((ll)n);
		if(res < -0.5)
			cout << "-1" << dl;
		else
			cout << res << dl;
	}

	return 0;
}
accuracy: 1
time_ms: 90
memory_kb: 3760
final_rank: 6

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 1;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

typedef long double Data;
typedef vector<Data> Array;
typedef vector<Array> mat;

bool is_zero(Data dat) { return abs(dat) < eps; }
mat operator-(mat a) {
	rep(i, a.size())rep(j, a[0].size())a[i][j] = -a[i][j];
	return a;
}
mat operator+(mat lhs, mat& rhs) {
	rep(i, lhs.size())rep(j, lhs[0].size())lhs[i][j] += rhs[i][j];
	return lhs;
}
mat operator-(mat lhs, mat& rhs) {
	rep(i, lhs.size())rep(j, lhs[0].size())lhs[i][j] -= rhs[i][j];
	return lhs;
}
mat operator*(const mat& lhs, const mat& rhs) {
	mat ret(lhs.size(), Array(rhs[0].size(), 0));
	rep(i, lhs.size())rep(j, rhs[0].size())rep(k, rhs.size()) {
		ret[i][j] += lhs[i][k] * rhs[k][j];
	}
	return ret;
}
//no verify
int rankMat(mat a) {
	const int n = a.size(), m = a[0].size();
	int r = 0;
	for (int i = 0; r < n && i < m; ++i) {
		int pivot = r;
		for (int j = r + 1; j < n; ++j)
			if (abs(a[j][i]) > abs(a[pivot][i]))pivot = j;
		swap(a[pivot], a[r]);
		if (is_zero(a[r][i]))continue;
		for (int k = m - 1; k >= i; --k)
			a[r][k] = a[r][k] / a[r][i];
		for (int j = r + 1; j < n; ++j) {
			for (int k = m - 1; k >= i; --k) {
				a[j][k] = fma(-a[r][k], a[j][i], a[j][k]);
			}
		}
		++r;
	}
	return r;
}
mat scalar(int sz, Data k) {
	mat ret(sz, Array(sz, 0));
	rep(i, sz)ret[i][i] = k;
	return ret;
}
//行列累乗
mat operator^(const mat& lhs, const ll n) {
	if (n == 0)return scalar(lhs.size(), 1);
	mat ret = (lhs * lhs) ^ (n / 2);
	if (n % 2) {
		ret = ret * lhs;
	}
	return ret;
}

Data det(mat a) {
	const int n = a.size();
	Data D = Data(1);
	for (int i = 0; i < n; ++i) {
		int pivot = i;
		for (int j = i + 1; j < n; ++j) {
			if (abs(a[j][i]) > abs(a[pivot][i]))pivot = j;
		}
		swap(a[pivot], a[i]);
		D = D * a[i][i] * Data(i != pivot ? -1 : 1);
		if (is_zero(a[i][i]))break;
		for (int j = i + 1; j < n; ++j) {
			for (int k = n - 1; k >= i; --k) {
				a[j][k] = a[j][k] - a[i][k] * a[j][i] / a[i][i];
			}
		}
	}
	return D;
}

pair<mat, vector<int>> LUPDecomposition(mat a) {
	int n = a.size();
	vector<int> perm(n);
	iota(begin(perm), end(perm), 0);
	rep(i, n) {
		int pivot = i;
		for (int j = i + 1; j < n; ++j)
			if (abs(a[j][i]) > abs(a[pivot][i]))pivot = j;
		swap(a[pivot], a[i]);
		swap(perm[pivot], perm[i]);
		for (int j = i + 1; j < n; ++j) {
			a[j][i] /= a[i][i];
			for (int k = i + 1; k < n; ++k) {
				a[j][k] -= a[i][k] * a[j][i];
			}
		}
	}
	return make_pair(a, perm);
}
Array LUPBackSubstitution(mat& LU, vector<int>& perm, Array a) {
	int n = LU.size();
	Array tmp(n);
	rep(i, n)tmp[i] = a[perm[i]];
	swap(tmp, a);
	rep(i, n) {
		rep(j, i) {
			a[i] -= a[j] * LU[i][j];
		}
	}
	for (int i = n - 1; i >= 0; --i) {
		for (int j = i + 1; j < n; ++j) {
			a[i] -= a[j] * LU[i][j];
		}
		a[i] /= LU[i][i];
	}
	return a;
}

//Ax=bのxを返す
Array calc(mat A, Array b) {
	pair<mat, vector<int>> p = LUPDecomposition(A);
	return LUPBackSubstitution(p.first, p.second, b);
}

ld dp[11][101];
void solve() {
	int x; cin >> x;
	int n, k; cin >> k >> n;
	if (k == 1) {
		x = abs(x);
		if (x % n == 0) {
			cout << x / n << "\n";
		}
		else {
			cout << -1 << "\n";
		}
		return;
	}
	dp[0][0] = 1;
	rep(i, n) {
		rep(j, 10 * i + 1) {
			rep1(t, k) {
				dp[i + 1][j + t] += dp[i][j];
			}
		}
	}
	ld b = 1; rep(i, n)b *= k;
	rep(i, 101)dp[n][i] /= b;
	//for (int c = n; c <= n * k; c++)cout << dp[n][c] << "\n";
	mat A(n * k, vector<ld>(n * k));
	vector<ld> ori(n * k, 1);
	rep1(i, n * k) {
		A[i - 1][i - 1] += 1;
		for (int c = n; c <= n * k; c++) {
			int t = i - c;
			t = abs(t);
			if (t > 0) {
				A[i - 1][t - 1] -= dp[n][c];
			}
		}
	}
	vector<ld> z = calc(A, ori);
	z.insert(z.begin(), 0);
	x = abs(x);
	if (x < z.size()) {
		cout << z[x] << "\n"; return;
	}
	mat B(n * k + 1, vector<ld>(n * k + 1));
	rep1(i, n * k - 1) {
		B[i][i - 1] = 1;
	}
	for (int c = n; c <= n * k; c++) {
		B[0][c - 1] = dp[n][c];
	}
	B[0][n * k] = 1;
	B[n * k][n * k] = 1;
	B = B ^ (int)(x - z.size() + 1);
	reverse(all(z));
	z[n * k] = 1;
	ld ans = 0;
	rep(i, n * k + 1) {
		ans += B[0][i] * z[i];
	}

	cout << ans << "\n";
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(12);
	//init_f();
	//init();
	//expr();
	//int t; cin >> t;rep(i,t)
	//while(cin>>n,n)
	solve();
	return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 8644
final_rank: 10

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;

struct Matrix{
	int h, w;
	vector<double> mat;
	Matrix(){
		h = w = 0;
	}
	Matrix(int h, int w){
		this->h = h, this->w = w;
		mat.resize(h*w);
	}
	double& at(int i, int j){
		return mat[w*(i-1)+(j-1)];
	}
	static Matrix ident(int size){
		Matrix ret(size, size);
		for(int i = 1; i <= size; i++) ret.at(i, i) = 1;
		return ret;
	}
	Matrix operator*(Matrix& ope){
		Matrix ret(h, ope.w);
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= ope.w; j++){
				for(int k = 1; k <= w; k++){
					ret.at(i, j) += at(i, k) * ope.at(k, j);
				}
			}
		}
		return ret;
	}
	Matrix pow(llint n){
		if(n == 0) return ident(h);
		if(n % 2){
			return pow(n-1) * (*this);
		}
		else{
			Matrix tmp = pow(n/2);
			return tmp * tmp;
		}
	}
	void print(){
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
				cout << at(i, j) << " ";
			}
			cout << endl;
		}
	}
};

ll s, n, k;
double p[105];
ll dp[15][105];
double a[105][105];
Matrix mat;


void swap(double mat[105][105], int i, int j, int n)
{
	for(int k = 0; k <= n+1; k++){
		double t = mat[i][k];
		mat[i][k] = mat[j][k];
		mat[j][k] = t;
	}
}

void GaussianElimination(double mat[105][105], int n)
{
	for(int i = 0; i <= n; i++){
		double max_val = 0; int max_j;
		for(int j = i; j <= n; j++){
			if(fabs(mat[j][i]) > max_val){
				max_val = fabs(mat[j][i]);
				max_j = j;
			}
		}
		assert(fabs(max_val) > 1e-9);
		swap(mat, i, max_j, n);
		
		double div = mat[i][i];
		for(int j = 0; j <= n+1; j++){
			mat[i][j] /= div;
		}
		for(int j = 0; j <= n; j++){
			double coe = mat[j][i];
			if(i == j) continue;
			for(int k = 0; k <= n+1; k++){
				mat[j][k] -= coe * mat[i][k];
			}
		}
	}
}

int main(void)
{
	cin >> s >> n >> k;
	s = abs(s); ll m = n*k;
	
	if(n == 1){
		if(s % k){
			cout << -1 << endl;
			return 0;
		}
		s /= k, k = 1;
	}
	
	dp[0][0] = 1;
	rep(i, 0, k-1){
		rep(j, 0, m){
			rep(l, 1, n){
				if(j+l <= m) dp[i+1][j+l] += dp[i][j];
			}
		}
	}
	ll mul = 1;
	rep(i, 1, k) mul *= n;
	rep(j, 1, m) p[j] = (double)dp[k][j] / (double)mul;
	
	rep(i, 1, m){
		a[i][i] += 1;
		rep(j, 1, m) a[i][abs(i-j)] -= p[j];
		a[i][m+1] = 1;
	}
	a[0][0] = 1;
	
	GaussianElimination(a, m);
	
	mat = Matrix(m+2, m+2);
	rep(i, 1, m) mat.at(1, i) = p[i];
	mat.at(1, m+2) = 1;
	rep(i, 1, m) mat.at(i+1, i) = 1;
	mat.at(m+2, m+2) = 1;
	
	if(s <= m){
		printf("%.11f\n", a[s][m+1]);
		return 0;
	}
		
	Matrix pmat = mat.pow(s-m), vec(m+2, 1);
	rep(i, 0, m) vec.at(i+1, 1) = a[m-i][m+1];
	vec.at(m+2, 1) = 1;
	
	vec = pmat * vec;
	printf("%.11f\n", vec.at(1, 1));
	
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3492
final_rank: 3

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;

struct Matrix{
	int h, w;
	vector<double> mat;
	Matrix(){
		h = w = 0;
	}
	Matrix(int h, int w){
		this->h = h, this->w = w;
		mat.resize(h*w);
	}
	double& at(int i, int j){
		return mat[w*(i-1)+(j-1)];
	}
	static Matrix ident(int size){
		Matrix ret(size, size);
		for(int i = 1; i <= size; i++) ret.at(i, i) = 1;
		return ret;
	}
	Matrix operator*(Matrix& ope){
		Matrix ret(h, ope.w);
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= ope.w; j++){
				for(int k = 1; k <= w; k++){
					ret.at(i, j) += at(i, k) * ope.at(k, j);
				}
			}
		}
		return ret;
	}
	Matrix pow(llint n){
		if(n == 0) return ident(h);
		if(n % 2){
			return pow(n-1) * (*this);
		}
		else{
			Matrix tmp = pow(n/2);
			return tmp * tmp;
		}
	}
	void print(){
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
				cout << at(i, j) << " ";
			}
			cout << endl;
		}
	}
};

ll s, n, k;
double p[105];
ll dp[15][105];
double a[105][105];
Matrix mat;


void swap(double mat[105][105], int i, int j, int n)
{
	for(int k = 0; k <= n+1; k++){
		double t = mat[i][k];
		mat[i][k] = mat[j][k];
		mat[j][k] = t;
	}
}

void GaussianElimination(double mat[105][105], int n)
{
	for(int i = 0; i <= n; i++){
		double max_val = 0; int max_j;
		for(int j = i; j <= n; j++){
			if(fabs(mat[j][i]) > max_val){
				max_val = fabs(mat[j][i]);
				max_j = j;
			}
		}
		assert(fabs(max_val) > 1e-9);
		swap(mat, i, max_j, n);
		
		double div = mat[i][i];
		for(int j = 0; j <= n+1; j++){
			mat[i][j] /= div;
		}
		for(int j = 0; j <= n; j++){
			double coe = mat[j][i];
			if(i == j) continue;
			for(int k = 0; k <= n+1; k++){
				mat[j][k] -= coe * mat[i][k];
			}
		}
	}
}

int main(void)
{
	cin >> s >> n >> k;
	s = abs(s); ll m = n*k;
	
	if(n == 1){
		if(s % k){
			cout << -1 << endl;
			return 0;
		}
		s /= k, k = 1;
	}
	
	dp[0][0] = 1;
	rep(i, 0, k-1){
		rep(j, 0, m){
			rep(l, 1, n){
				if(j+l <= m) dp[i+1][j+l] += dp[i][j];
			}
		}
	}
	ll mul = 1;
	rep(i, 1, k) mul *= n;
	rep(j, 1, m) p[j] = (double)dp[k][j] / (double)mul;
	
	rep(i, 1, m){
		a[i][i] += 1;
		rep(j, 1, m) a[i][abs(i-j)] -= p[j];
		a[i][m+1] = 1;
	}
	a[0][0] = 1;
	
	/*rep(i, 0, m){
		rep(j, 0, m+1){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}*/
	
	GaussianElimination(a, m);
	
	mat = Matrix(m+2, m+2);
	rep(i, 1, m) mat.at(1, i) = p[i];
	mat.at(1, m+2) = 1;
	rep(i, 1, m) mat.at(i+1, i) = 1;
	mat.at(m+2, m+2) = 1;
	
	if(s <= m){
		printf("%.11f\n", a[s][m+1]);
		return 0;
	}
		
	Matrix pmat = mat.pow(s-m), vec(m+2, 1);
	rep(i, 0, m) vec.at(i+1, 1) = a[m-i][m+1];
	vec.at(m+2, 1) = 1;
	
	vec = pmat * vec;
	printf("%.11f\n", vec.at(1, 1));
	
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3432
final_rank: 1

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18
#define double long double

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;

struct Matrix{
	int h, w;
	vector<double> mat;
	Matrix(){
		h = w = 0;
	}
	Matrix(int h, int w){
		this->h = h, this->w = w;
		mat.resize(h*w);
	}
	double& at(int i, int j){
		return mat[w*(i-1)+(j-1)];
	}
	static Matrix ident(int size){
		Matrix ret(size, size);
		for(int i = 1; i <= size; i++) ret.at(i, i) = 1;
		return ret;
	}
	Matrix operator*(Matrix& ope){
		Matrix ret(h, ope.w);
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= ope.w; j++){
				for(int k = 1; k <= w; k++){
					ret.at(i, j) += at(i, k) * ope.at(k, j);
				}
			}
		}
		return ret;
	}
	Matrix pow(llint n){
		if(n == 0) return ident(h);
		if(n % 2){
			return pow(n-1) * (*this);
		}
		else{
			Matrix tmp = pow(n/2);
			return tmp * tmp;
		}
	}
	void print(){
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
				cout << at(i, j) << " ";
			}
			cout << endl;
		}
	}
};

ll s, n, k;
double p[105];
ll dp[15][105];
double a[105][105];
Matrix mat;


void swap(double mat[105][105], int i, int j, int n)
{
	for(int k = 0; k <= n+1; k++){
		double t = mat[i][k];
		mat[i][k] = mat[j][k];
		mat[j][k] = t;
	}
}

void GaussianElimination(double mat[105][105], int n)
{
	for(int i = 0; i <= n; i++){
		double max_val = 0; int max_j;
		for(int j = i; j <= n; j++){
			if(fabs(mat[j][i]) > max_val){
				max_val = fabs(mat[j][i]);
				max_j = j;
			}
		}
		assert(fabs(max_val) > 1e-9);
		swap(mat, i, max_j, n);
		
		double div = mat[i][i];
		for(int j = 0; j <= n+1; j++){
			mat[i][j] /= div;
		}
		for(int j = 0; j <= n; j++){
			double coe = mat[j][i];
			if(i == j) continue;
			for(int k = 0; k <= n+1; k++){
				mat[j][k] -= coe * mat[i][k];
			}
		}
	}
}

int main(void)
{
	cin >> s >> n >> k;
	s = abs(s);
	if((n == 1 || k == 1) && s % (n*k)){
		cout << -1 << endl;
		return 0;
	}
	ll m = n*k;
	
	dp[0][0] = 1;
	rep(i, 0, k-1){
		rep(j, 0, m){
			rep(l, 1, n){
				if(j+l <= m) dp[i+1][j+l] += dp[i][j];
			}
		}
	}
	ll mul = 1;
	rep(i, 1, k) mul *= n;
	rep(j, 1, m) p[j] = (double)dp[k][j] / (double)mul;
	
	rep(i, 1, m){
		a[i][i] += 1;
		rep(j, 1, m) a[i][abs(i-j)] -= p[j];
		a[i][m+1] = 1;
	}
	a[0][0] = 1;
	
	GaussianElimination(a, m);
	
	mat = Matrix(m+2, m+2);
	rep(i, 1, m) mat.at(1, i) = p[i];
	mat.at(1, m+2) = 1;
	rep(i, 1, m) mat.at(i+1, i) = 1;
	mat.at(m+2, m+2) = 1;
	
	if(s <= m){
		printf("%.11Lf\n", a[s][m+1]);
		return 0;
	}
		
	Matrix pmat = mat.pow(s-m), vec(m+2, 1);
	rep(i, 0, m) vec.at(i+1, 1) = a[m-i][m+1];
	vec.at(m+2, 1) = 1;
	
	vec = pmat * vec;
	printf("%.11Lf\n", vec.at(1, 1));
	
	return 0;
}
accuracy: 0.0967741935483871
time_ms: 80
memory_kb: 3400
final_rank: 20

code_snippet: #include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18

using namespace std;

typedef long long llint;
typedef long long ll;
typedef pair<llint, llint> P;

struct Matrix{
	int h, w;
	vector<double> mat;
	Matrix(){
		h = w = 0;
	}
	Matrix(int h, int w){
		this->h = h, this->w = w;
		mat.resize(h*w);
	}
	double& at(int i, int j){
		return mat[w*(i-1)+(j-1)];
	}
	static Matrix ident(int size){
		Matrix ret(size, size);
		for(int i = 1; i <= size; i++) ret.at(i, i) = 1;
		return ret;
	}
	Matrix operator*(Matrix& ope){
		Matrix ret(h, ope.w);
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= ope.w; j++){
				for(int k = 1; k <= w; k++){
					ret.at(i, j) += at(i, k) * ope.at(k, j);
				}
			}
		}
		return ret;
	}
	Matrix pow(llint n){
		if(n == 0) return ident(h);
		if(n % 2){
			return pow(n-1) * (*this);
		}
		else{
			Matrix tmp = pow(n/2);
			return tmp * tmp;
		}
	}
	void print(){
		for(int i = 1; i <= h; i++){
			for(int j = 1; j <= w; j++){
				cout << at(i, j) << " ";
			}
			cout << endl;
		}
	}
};

ll s, n, k;
double p[105];
ll dp[15][105];
double a[105][105];
Matrix mat;


void swap(double mat[105][105], int i, int j, int n)
{
	for(int k = 0; k <= n+1; k++){
		double t = mat[i][k];
		mat[i][k] = mat[j][k];
		mat[j][k] = t;
	}
}

void GaussianElimination(double mat[105][105], int n)
{
	for(int i = 0; i <= n; i++){
		double max_val = 0; int max_j;
		for(int j = i; j <= n; j++){
			if(fabs(mat[j][i]) > max_val){
				max_val = fabs(mat[j][i]);
				max_j = j;
			}
		}
		assert(fabs(max_val) > 1e-9);
		swap(mat, i, max_j, n);
		
		double div = mat[i][i];
		for(int j = 0; j <= n+1; j++){
			mat[i][j] /= div;
		}
		for(int j = 0; j <= n; j++){
			double coe = mat[j][i];
			if(i == j) continue;
			for(int k = 0; k <= n+1; k++){
				mat[j][k] -= coe * mat[i][k];
			}
		}
	}
}

int main(void)
{
	cin >> s >> n >> k;
	s = abs(s);
	if((n == 1 || k == 1) && s % (n*k)){
		cout << -1 << endl;
		return 0;
	}
	ll m = n*k;
	
	dp[0][0] = 1;
	rep(i, 0, k-1){
		rep(j, 0, m){
			rep(l, 1, n){
				if(j+l <= m) dp[i+1][j+l] += dp[i][j];
			}
		}
	}
	ll mul = 1;
	rep(i, 1, k) mul *= n;
	rep(j, 1, m) p[j] = (double)dp[k][j] / (double)mul;
	
	rep(i, 1, m){
		a[i][i] += 1;
		rep(j, 1, m) a[i][abs(i-j)] -= p[j];
		a[i][m+1] = 1;
	}
	a[0][0] = 1;
	
	GaussianElimination(a, m);
	
	mat = Matrix(m+2, m+2);
	rep(i, 1, m) mat.at(1, i) = p[i];
	mat.at(1, m+2) = 1;
	rep(i, 1, m) mat.at(i+1, i) = 1;
	mat.at(m+2, m+2) = 1;
	
	if(s <= m){
		printf("%.11f\n", a[s][m+1]);
		return 0;
	}
		
	Matrix pmat = mat.pow(s-m), vec(m+2, 1);
	rep(i, 0, m) vec.at(i+1, 1) = a[m-i][m+1];
	vec.at(m+2, 1) = 1;
	
	vec = pmat * vec;
	printf("%.11f\n", vec.at(1, 1));
	
	return 0;
}
accuracy: 0.0967741935483871
time_ms: 20
memory_kb: 3416
final_rank: 19

code_snippet: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

// 行列ライブラリ
// Verified: TopCoder SRM 704 Div.2 (ModEquationEasy)
// 行列の積 と 累乗(繰り返し二乗法)

// Matrix Library Begin (C++11)

template <typename T>
using Matrix = vector< vector<T> >;

template <typename T>
void init_mat(Matrix<T> &A, int h, int w) {
    A.resize(h, vector<T>(w, 0));
}

template <typename T>
Matrix<T> calc_mat(Matrix<T> A, Matrix<T> B) {
    Matrix<T> C(A.size(), vector<T>(B[0].size()));
    for(int i=0; i<A.size(); i++) {
        for(int k=0; k<B.size(); k++) {
            for(int j=0; j<B[0].size(); j++) {
                C[i][j] += A[i][k] * B[k][j]; // modなし
                // C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD; // modあり
            }
        }
    }
    return C;
}

template <typename T>
Matrix<T> mat_pow(Matrix<T> A, ll n) {
    Matrix<T> B(A.size(), vector<T>(A.size()));
    for(int i=0; i<A.size(); i++) B[i][i] = 1;
    while(n > 0) {
        if(n & 1) B = calc_mat(B, A);
        A = calc_mat(A, A);
        n >>= 1;
    }
    return B;
}

// calculate vector x which satisfies Ax = b
// (A is N*N Matrix, b, x is N-dim vector)

template <typename T>
using Matrix = vector< vector<T> >;
constexpr long double EPS = 1e-8;

template <typename T>
vector<long double> gauss_jordan(const Matrix<T> &A, const vector<T> &b) {
    int N = A.size();
    // B = [A b]
    Matrix<long double> B(N, vector<long double>(N+1));
    for(int i=0; i<N; i++) {
        for(int j=0; j<=N; j++) {
            if(j < N) B[i][j] = A[i][j];
            else B[i][j] = b[i];
        }
    }

    for(int i=0; i<N; i++) {
        int pivot = i;
        for(int j=i+1; j<N; j++) {
            if(abs(B[j][i]) > abs(B[pivot][i])) pivot = j;
        }
        if(abs(B[pivot][i]) < EPS) continue;
        swap(B[i], B[pivot]);

        // pivot is zero -> No solution or Not unique
        // assert(abs(B[i][i]) >= EPS);
        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];
            }
        }
    }
    vector<long double> x(N);
    for(int i=0; i<N; i++) x[i] = B[i][N];
    return x;
}

long double d[11][110], dice[110], dp[110];
signed main() {
    int S, N, K; cin >> S >> N >> K;
    S = abs(S);

    if(N == 1 and S % K != 0) {
        cout << -1 << endl;
        return 0;
    }

    d[0][0] = 1.0;
    for(int i=0; i<K; i++) {
        for(int j=i*N; j>=0; j--) {
            for(int k=N; k>=1; k--) {
                d[i+1][j+k] += d[i][j] / N;
            }
        }
    }

    int M = N*K;
    long double sum = 0.0;
    for(int i=0; i<=M; i++) {
        dice[i] = d[K][i];
        sum += d[K][i];
    }
    
    Matrix<long double> mat;
    vector<long double> b(M+1);
    init_mat(mat, M+1, M+1);
    for(int i=0; i<=M; i++) {
        mat[i][i] = 1.0;
        if(i > 0) {
            for(int j=0; j<=M; j++) {
                long double p = dice[j];
                int idx = abs(i - j);
                mat[i][idx] -= p;
            }
            b[i] = 1.0;
        }
    }
    auto res = gauss_jordan(mat, b);

    if(S <= M) {
        printf("%.12Lf\n", (long double)res[S]);
    }
    else {
        long double ten = 1.0;
        
        int rem = S - M;
        Matrix<long double> tab;
        init_mat(tab, M+1, M+1);
        for(int i=0; i<M; i++) {
            tab[0][i] = dice[i+1];
            if(i+1 < M) tab[i+1][i] = 1.0;
        }
        tab[0][M] = 1.0;
        tab[M][M] = 1.0;

        Matrix<long double> vec;
        init_mat(vec, M+1, 1);
        for(int i=0; i<M; i++) {
            vec[i][0] = res[M-i] * ten;
        }
        vec[M][0] = ten;

        tab = mat_pow(tab, rem);
        vec = calc_mat(tab, vec);
        printf("%.12Lf\n", (long double)vec[0][0]);
    }
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 4104
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;


namespace ProconLib{
    
    template<typename T>
    struct Detail{
        static constexpr T EPS = T(1e-15);
        static bool isZero(T v){
            return abs(v)<=EPS;
        }
    };

    template<typename T>
    class Vector{
        int N;
        std::vector<T> dat;
        public:
        Vector(int n):N(n),dat(n){}
        Vector(int n,T x):N(n),dat(n,x){}
        Vector(std::vector<T> vec):N(vec.size()),dat(vec){}
        Vector(const std::vector<T>& vec):N(vec.size()),dat(vec){}
        Vector(const Vector& vec){dat=vec.dat;}
        int size(){return N;}
        T& operator[](int pos){return dat[pos];};
        const T& operator[](int pos) const{return dat[pos];};
        T& at(int pos){return dat.at(pos);}
        const T& at(int pos) const {return dat.at(pos);}
    };
    
    template<typename T>
    class Matrix{
        int r,c;
        std::vector<Vector<T>> dat;
        public:
        Matrix(int r,int c):r(r),c(c),dat(r,Vector<T>(c)){}
        Matrix(int r,int c,T x):r(r),c(c),dat(r,Vector<T>(c,x)){}

        Vector<T>& operator[](int pos){return dat[pos];};
        const Vector<T>& operator[](int pos) const{return dat[pos];};
        Vector<T>& at(int pos){return dat.at(pos);}
        const Vector<T>& at(int pos) const {return dat.at(pos);}
        int rowSize() const {return r;}
        int colSize() const {return c;}
    };
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs);
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs);
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& mat);
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& mat);
    template<typename T>
    Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs);
    template<typename T>
    Matrix<T> Identity(int n);
    template<typename T>
    Matrix<T> pow(const Matrix<T>& mat,int k);
    template<typename T,typename Detail=Detail<T>>
    Matrix<T> gaussianElimination(Matrix<T> mat);
    template<typename T>
    int rank(const Matrix<T>& mat);
    template<typename T>
    Matrix<T> inv(const Matrix<T>& mat);
    
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs){
        assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());
        int r=lhs.rowSize(),c=lhs.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                res[i][j]=lhs[i]+rhs[i];
            }
        }
        return res;
    }
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs){
        assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());
        int r=lhs.rowSize(),c=lhs.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                res[i][j]=lhs[i]-rhs[i];
            }
        }
        return res;
    }
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& mat){
        int r=mat.rowSize(),c=mat.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];
        return res;
    }
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& mat){
        int r=mat.rowSize(),c=mat.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];
        return res;
    }
    
    template<typename T>
    Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs){
        assert(lhs.colSize()==rhs.rowSize());
        int r=lhs.rowSize(),c=rhs.colSize(),l=lhs.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++){
            for(int k=0;k<l;k++){
                for(int j=0;j<c;j++){
                    res[i][j]+=lhs[i][k]*rhs[k][j];
                }
            }
        }
        return res;
    }

    template<typename T>
    Matrix<T> Identity(int n){
        assert(n>=0);
        Matrix<T> res(n,n);
        for(int i=0;i<n;i++){
            res[i][i]=1;
        }
        return res;
    }

    template<typename T>
    Matrix<T> pow(const Matrix<T>& mat,int k){
        assert(mat.rowSize()==mat.colSize());
        Matrix<T> x=mat;
        Matrix<T> res=Identity<T>(mat.rowSize());
        while(k){
            if(k&1) res=res*x;
            x=x*x;
            k>>=1;
        }
        return res;
    }
    
    template<typename T,typename Detail=Detail<T>>
    Matrix<T> gaussianElimination(Matrix<T> mat){
        int m=mat.rowSize(),n=mat.colSize();
        int row=0;
        for(int j=0;j<n;j++){
            if(row==m) break;
            int tar=-1;
            T v=Detail::EPS;
            for(int i=row;i<m;i++){
                if(!Detail::isZero(mat[i][j]) && abs(v)<abs(mat[i][j])){
                    tar=i;
                    v=mat[i][j];
                }
            }
            if(tar==-1) continue;
            if(row!=tar){ 
                auto tmp=mat[row];
                mat[row]=mat[tar];
                mat[tar]=tmp;
            }
            for(int i=row+1;i<m;i++){
                if(!Detail::isZero(mat[i][j])){
                    T r=mat[i][j]/mat[row][j];
                    for(int k=j;k<n;k++){
                        mat[i][k]-=r*mat[row][k];
                    }
                }
            }
            row++;
        }
        return mat;
    }

    template<typename T,typename Detail=Detail<T>>
    int rank(const Matrix<T>& mat){
        auto tmp=gaussialElimination(mat);
        int m=tmp.rowSize(),n=tmp.colSize();
        int i=0,j=0;
        while(i<m && j<n){
            if(Detail::isZero(tmp[i][j])) j++;
            else i++,j++;
        }
        return i;
    }

    template<typename T>
    Matrix<T> inv(const Matrix<T>& mat){
        assert(mat.rowSize()==mat.colSize());
        int n=mat.rowSize();
        Matrix<T> tmp(n,2*n);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                tmp[i][j]=mat[i][j];
                tmp[i][j+n]=0;
            }
            tmp[i][i+n]=1;
        }
        mat=gaussianElimination(mat);
        Matrix<T> res(n);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                res[i][j]=mat[i][j+n]/=mat[i][i];
            }
        }
        return res;
    }
 
    template<typename T>
    void debug(Matrix<T> mat){
        int m=mat.rowSize(),n=mat.colSize();
        std::cerr<<"###Matrix_Debug###"<<std::endl;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                std::cerr<<mat[i][j]<<" ";
            }
            std::cerr<<std::endl;
        }
    }
}

using namespace ProconLib;

int main(){
    int s,n,k;
    cin>>s>>n>>k;
    if(n==1){
        cout<<(s%k==0 ? abs(s)/k : -1)<<endl;
        return 0;
    }
    
    int sz=n*k+1;
    
    vector<vector<double>> dp(k+1,vector<double>(sz));
    dp[0][0]=1;
    for(int i=0;i<k;i++){
        for(int j=0;j<sz;j++){
            for(int k=1;k<=n;k++){
                if(j+k<sz) dp[i+1][j+k]+=dp[i][j]/n;
            }
        }
    }
    
    Matrix<double> B(sz,sz+1);
    {
        B[0][0]=1;
        B[0][sz]=0;
        for(int i=1;i<sz;i++){
            B[i][i]=-1;
            for(int j=0;j<sz;j++){
                // i to j
                int d=i-j;
                if(0<=d && d<sz){
                    B[i][j]+=dp[k][d];
                    B[i][sz]-=dp[k][d];
                }
                d=i+j;
                if(j!=0 && 0<=d && d<sz){
                    B[i][j]+=dp[k][d];
                    B[i][sz]-=dp[k][d];
                }
            }
        }
    }
    B=gaussianElimination(B);
    Vector<double> vec(sz+1);
    for(int i=sz-1;i>=0;i--){
        for(int j=0;j<i;j++){
            B[j][sz]-=B[j][i]/B[i][i]*B[i][sz];
            B[j][i]=0;
        }
    }
    for(int i=0;i<sz;i++){
        vec[sz-i-1]=B[i][sz]/B[i][i];
    }
    vec[sz]=1;

    Matrix<double> A(sz+1,sz+1);
    for(int i=1;i<sz;i++){
        A[i][i-1]=1;
    }
    A[sz][sz]=1;
    for(int i=0;i<sz;i++){
        int d=i+1;
        if(d<sz) A[0][i]=dp[k][d];
        if(d<sz) A[0][sz]+=dp[k][d];
    }
    A=pow(A,abs(s));
    double res=0;
    for(int i=0;i<=sz;i++){
        res+=A[sz-1][i]*vec[i];
    }
    cout<<fixed<<setprecision(10);
    cout<<res<<endl;

    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3548
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;


namespace ProconLib{
    
    template<typename T>
    struct Detail{
        static constexpr T EPS = T(1e-15);
        static bool isZero(T v){
            return abs(v)<=EPS;
        }
    };

    template<typename T>
    class Vector{
        int N;
        std::vector<T> dat;
        public:
        Vector(int n):N(n),dat(n){}
        Vector(int n,T x):N(n),dat(n,x){}
        Vector(std::vector<T> vec):N(vec.size()),dat(vec){}
        Vector(const std::vector<T>& vec):N(vec.size()),dat(vec){}
        Vector(const Vector& vec){dat=vec.dat;}
        int size(){return N;}
        T& operator[](int pos){return dat[pos];};
        const T& operator[](int pos) const{return dat[pos];};
        T& at(int pos){return dat.at(pos);}
        const T& at(int pos) const {return dat.at(pos);}
    };
    
    template<typename T>
    class Matrix{
        int r,c;
        std::vector<Vector<T>> dat;
        public:
        Matrix(int r,int c):r(r),c(c),dat(r,Vector<T>(c)){}
        Matrix(int r,int c,T x):r(r),c(c),dat(r,Vector<T>(c,x)){}

        Vector<T>& operator[](int pos){return dat[pos];};
        const Vector<T>& operator[](int pos) const{return dat[pos];};
        Vector<T>& at(int pos){return dat.at(pos);}
        const Vector<T>& at(int pos) const {return dat.at(pos);}
        int rowSize() const {return r;}
        int colSize() const {return c;}
    };
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs);
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs);
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& mat);
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& mat);
    template<typename T>
    Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs);
    template<typename T>
    Matrix<T> Identity(int n);
    template<typename T>
    Matrix<T> pow(const Matrix<T>& mat,int k);
    template<typename T,typename Detail=Detail<T>>
    Matrix<T> gaussianElimination(Matrix<T> mat);
    template<typename T>
    int rank(const Matrix<T>& mat);
    template<typename T>
    Matrix<T> inv(const Matrix<T>& mat);
    
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs){
        assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());
        int r=lhs.rowSize(),c=lhs.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                res[i][j]=lhs[i]+rhs[i];
            }
        }
        return res;
    }
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs){
        assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());
        int r=lhs.rowSize(),c=lhs.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                res[i][j]=lhs[i]-rhs[i];
            }
        }
        return res;
    }
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& mat){
        int r=mat.rowSize(),c=mat.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];
        return res;
    }
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& mat){
        int r=mat.rowSize(),c=mat.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];
        return res;
    }
    
    template<typename T>
    Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs){
        assert(lhs.colSize()==rhs.rowSize());
        int r=lhs.rowSize(),c=rhs.colSize(),l=lhs.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++){
            for(int k=0;k<l;k++){
                for(int j=0;j<c;j++){
                    res[i][j]+=lhs[i][k]*rhs[k][j];
                }
            }
        }
        return res;
    }

    template<typename T>
    Matrix<T> Identity(int n){
        assert(n>=0);
        Matrix<T> res(n,n);
        for(int i=0;i<n;i++){
            res[i][i]=1;
        }
        return res;
    }

    template<typename T>
    Matrix<T> pow(const Matrix<T>& mat,int k){
        assert(mat.rowSize()==mat.colSize());
        Matrix<T> x=mat;
        Matrix<T> res=Identity<T>(mat.rowSize());
        while(k){
            if(k&1) res=res*x;
            x=x*x;
            k>>=1;
        }
        return res;
    }
    
    template<typename T,typename Detail=Detail<T>>
    Matrix<T> gaussianElimination(Matrix<T> mat){
        int m=mat.rowSize(),n=mat.colSize();
        int row=0;
        for(int j=0;j<n;j++){
            if(row==m) break;
            int tar=-1;
            T v=Detail::EPS;
            for(int i=row;i<m;i++){
                if(!Detail::isZero(mat[i][j]) && abs(v)<abs(mat[i][j])){
                    tar=i;
                    v=mat[i][j];
                }
            }
            if(tar==-1) continue;
            if(row!=tar){ 
                auto tmp=mat[row];
                mat[row]=mat[tar];
                mat[tar]=tmp;
            }
            for(int i=row+1;i<m;i++){
                if(!Detail::isZero(mat[i][j])){
                    T r=mat[i][j]/mat[row][j];
                    for(int k=j;k<n;k++){
                        mat[i][k]-=r*mat[row][k];
                    }
                }
            }
            row++;
        }
        return mat;
    }

    template<typename T,typename Detail=Detail<T>>
    int rank(const Matrix<T>& mat){
        auto tmp=gaussialElimination(mat);
        int m=tmp.rowSize(),n=tmp.colSize();
        int i=0,j=0;
        while(i<m && j<n){
            if(Detail::isZero(tmp[i][j])) j++;
            else i++,j++;
        }
        return i;
    }

    template<typename T>
    Matrix<T> inv(const Matrix<T>& mat){
        assert(mat.rowSize()==mat.colSize());
        int n=mat.rowSize();
        Matrix<T> tmp(n,2*n);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                tmp[i][j]=mat[i][j];
                tmp[i][j+n]=0;
            }
            tmp[i][i+n]=1;
        }
        mat=gaussianElimination(mat);
        Matrix<T> res(n);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                res[i][j]=mat[i][j+n]/=mat[i][i];
            }
        }
        return res;
    }
 
    template<typename T>
    void debug(Matrix<T> mat){
        int m=mat.rowSize(),n=mat.colSize();
        std::cerr<<"###Matrix_Debug###"<<std::endl;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                std::cerr<<mat[i][j]<<" ";
            }
            std::cerr<<std::endl;
        }
    }
}

using namespace ProconLib;

int main(){
    int s,n,k;
    cin>>s>>n>>k;
    if(n==1 && s%k!=0){
        cout<<-1<<endl;
        return 0;
    }
    int sz=n*k+1;
    
    vector<vector<double>> dp(k+1,vector<double>(sz));
    dp[0][0]=1;
    for(int i=0;i<k;i++){
        for(int j=0;j<sz;j++){
            for(int k=1;k<=n;k++){
                if(j+k<sz) dp[i+1][j+k]+=dp[i][j]/n;
            }
        }
    }
    
    Matrix<double> B(sz,sz+1);
    {
        B[0][0]=1;
        B[0][sz]=0;
        for(int i=1;i<sz;i++){
            B[i][i]=-1;
            for(int j=0;j<sz;j++){
                // i to j
                int d=i-j;
                if(0<=d && d<sz){
                    B[i][j]+=dp[k][d];
                    B[i][sz]-=dp[k][d];
                }
                d=i+j;
                if(j!=0 && 0<=d && d<sz){
                    B[i][j]+=dp[k][d];
                    B[i][sz]-=dp[k][d];
                }
            }
        }
    }
    B=gaussianElimination(B);
    Vector<double> vec(sz+1);
    for(int i=sz-1;i>=0;i--){
        for(int j=0;j<i;j++){
            B[j][sz]-=B[j][i]/B[i][i]*B[i][sz];
            B[j][i]=0;
        }
    }
    for(int i=0;i<sz;i++){
        vec[sz-i-1]=B[i][sz]/B[i][i];
    }
    vec[sz]=1;

    Matrix<double> A(sz+1,sz+1);
    for(int i=1;i<sz;i++){
        A[i][i-1]=1;
    }
    A[sz][sz]=1;
    for(int i=0;i<sz;i++){
        int d=i+1;
        if(d<sz) A[0][i]=dp[k][d];
        if(d<sz) A[0][sz]+=dp[k][d];
    }
    A=pow(A,abs(s));
    double res=0;
    for(int i=0;i<=sz;i++){
        res+=A[sz-1][i]*vec[i];
    }
    cout<<fixed<<setprecision(10);
    cout<<res<<endl;

    return 0;
}
accuracy: 0.1935483870967742
time_ms: 20
memory_kb: 3488
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;


namespace ProconLib{
    
    template<typename T>
    struct Detail{
        static constexpr T EPS = T(1e-9);
        static bool isZero(T v){
            return abs(v)<=EPS;
        }
    };

    template<typename T>
    class Vector{
        int N;
        std::vector<T> dat;
        public:
        Vector(int n):N(n),dat(n){}
        Vector(int n,T x):N(n),dat(n,x){}
        Vector(std::vector<T> vec):N(vec.size()),dat(vec){}
        Vector(const std::vector<T>& vec):N(vec.size()),dat(vec){}
        Vector(const Vector& vec){dat=vec.dat;}
        int size(){return N;}
        T& operator[](int pos){return dat[pos];};
        const T& operator[](int pos) const{return dat[pos];};
        T& at(int pos){return dat.at(pos);}
        const T& at(int pos) const {return dat.at(pos);}
    };
    
    template<typename T>
    class Matrix{
        int r,c;
        std::vector<Vector<T>> dat;
        public:
        Matrix(int r,int c):r(r),c(c),dat(r,Vector<T>(c)){}
        Matrix(int r,int c,T x):r(r),c(c),dat(r,Vector<T>(c,x)){}

        Vector<T>& operator[](int pos){return dat[pos];};
        const Vector<T>& operator[](int pos) const{return dat[pos];};
        Vector<T>& at(int pos){return dat.at(pos);}
        const Vector<T>& at(int pos) const {return dat.at(pos);}
        int rowSize() const {return r;}
        int colSize() const {return c;}
    };
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs);
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs);
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& mat);
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& mat);
    template<typename T>
    Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs);
    template<typename T>
    Matrix<T> Identity(int n);
    template<typename T>
    Matrix<T> pow(const Matrix<T>& mat,int k);
    template<typename T,typename Detail=Detail<T>>
    Matrix<T> gaussianElimination(Matrix<T> mat);
    template<typename T>
    int rank(const Matrix<T>& mat);
    template<typename T>
    Matrix<T> inv(const Matrix<T>& mat);
    
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& lhs,const Matrix<T>& rhs){
        assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());
        int r=lhs.rowSize(),c=lhs.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                res[i][j]=lhs[i]+rhs[i];
            }
        }
        return res;
    }
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& lhs,const Matrix<T>& rhs){
        assert(lhs.rowSize()==rhs.rowSize() && lhs.colSize()==rhs.colSize());
        int r=lhs.rowSize(),c=lhs.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                res[i][j]=lhs[i]-rhs[i];
            }
        }
        return res;
    }
    template<typename T>
    Matrix<T> operator+(const Matrix<T>& mat){
        int r=mat.rowSize(),c=mat.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];
        return res;
    }
    template<typename T>
    Matrix<T> operator-(const Matrix<T>& mat){
        int r=mat.rowSize(),c=mat.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++)for(int j=0;j<c;j++) res[i][j]=-mat[i][j];
        return res;
    }
    
    template<typename T>
    Matrix<T> operator*(const Matrix<T>& lhs,const Matrix<T> &rhs){
        assert(lhs.colSize()==rhs.rowSize());
        int r=lhs.rowSize(),c=rhs.colSize(),l=lhs.colSize();
        Matrix<T> res(r,c);
        for(int i=0;i<r;i++){
            for(int k=0;k<l;k++){
                for(int j=0;j<c;j++){
                    res[i][j]+=lhs[i][k]*rhs[k][j];
                }
            }
        }
        return res;
    }

    template<typename T>
    Matrix<T> Identity(int n){
        assert(n>=0);
        Matrix<T> res(n,n);
        for(int i=0;i<n;i++){
            res[i][i]=1;
        }
        return res;
    }

    template<typename T>
    Matrix<T> pow(const Matrix<T>& mat,int k){
        assert(mat.rowSize()==mat.colSize());
        Matrix<T> x=mat;
        Matrix<T> res=Identity<T>(mat.rowSize());
        while(k){
            if(k&1) res=res*x;
            x=x*x;
            k>>=1;
        }
        return res;
    }
    
    template<typename T,typename Detail=Detail<T>>
    Matrix<T> gaussianElimination(Matrix<T> mat){
        int m=mat.rowSize(),n=mat.colSize();
        int row=0;
        for(int j=0;j<n;j++){
            if(row==m) break;
            int tar=-1;
            T v=Detail::EPS;
            for(int i=row;i<m;i++){
                if(!Detail::isZero(mat[i][j]) && abs(v)<abs(mat[i][j])){
                    tar=i;
                    v=mat[i][j];
                }
            }
            if(tar==-1) continue;
            if(row!=tar){ 
                auto tmp=mat[row];
                mat[row]=mat[tar];
                mat[tar]=tmp;
            }
            for(int i=row+1;i<m;i++){
                if(!Detail::isZero(mat[i][j])){
                    T r=mat[i][j]/mat[row][j];
                    for(int k=j;k<n;k++){
                        mat[i][k]-=r*mat[row][k];
                    }
                }
            }
            row++;
        }
        return mat;
    }

    template<typename T,typename Detail=Detail<T>>
    int rank(const Matrix<T>& mat){
        auto tmp=gaussialElimination(mat);
        int m=tmp.rowSize(),n=tmp.colSize();
        int i=0,j=0;
        while(i<m && j<n){
            if(Detail::isZero(tmp[i][j])) j++;
            else i++,j++;
        }
        return i;
    }

    template<typename T>
    Matrix<T> inv(const Matrix<T>& mat){
        assert(mat.rowSize()==mat.colSize());
        int n=mat.rowSize();
        Matrix<T> tmp(n,2*n);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                tmp[i][j]=mat[i][j];
                tmp[i][j+n]=0;
            }
            tmp[i][i+n]=1;
        }
        mat=gaussianElimination(mat);
        Matrix<T> res(n);
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                res[i][j]=mat[i][j+n]/=mat[i][i];
            }
        }
        return res;
    }
 
    template<typename T>
    void debug(Matrix<T> mat){
        int m=mat.rowSize(),n=mat.colSize();
        std::cerr<<"###Matrix_Debug###"<<std::endl;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                std::cerr<<mat[i][j]<<" ";
            }
            std::cerr<<std::endl;
        }
    }
}

using namespace ProconLib;

int main(){
    int s,n,k;
    cin>>s>>n>>k;
    if(n==1 && s%k!=0){
        cout<<-1<<endl;
        return 0;
    }
    int sz=n*k+1;
    
    vector<vector<double>> dp(k+1,vector<double>(sz));
    dp[0][0]=1;
    for(int i=0;i<k;i++){
        for(int j=0;j<sz;j++){
            for(int k=1;k<=n;k++){
                if(j+k<sz) dp[i+1][j+k]+=dp[i][j]/n;
            }
        }
    }
    
    Matrix<double> B(sz,sz+1);
    {
        B[0][0]=1;
        B[0][sz]=0;
        for(int i=1;i<sz;i++){
            B[i][i]=-1;
            for(int j=0;j<sz;j++){
                // i to j
                int d=i-j;
                if(0<=d && d<sz){
                    B[i][j]+=dp[k][d];
                    B[i][sz]-=dp[k][d];
                }
                d=i+j;
                if(j!=0 && 0<=d && d<sz){
                    B[i][j]+=dp[k][d];
                    B[i][sz]-=dp[k][d];
                }
            }
        }
    }
    B=gaussianElimination(B);
    Vector<double> vec(sz+1);
    for(int i=sz-1;i>=0;i--){
        for(int j=0;j<i;j++){
            B[j][sz]-=B[j][i]/B[i][i]*B[i][sz];
            B[j][i]=0;
        }
    }
    for(int i=0;i<sz;i++){
        vec[sz-i-1]=B[i][sz]/B[i][i];
    }
    vec[sz]=1;

    Matrix<double> A(sz+1,sz+1);
    for(int i=1;i<sz;i++){
        A[i][i-1]=1;
    }
    A[sz][sz]=1;
    for(int i=0;i<sz;i++){
        int d=i+1;
        if(d<sz) A[0][i]=dp[k][d];
        if(d<sz) A[0][sz]+=dp[k][d];
    }
    A=pow(A,abs(s));
    double res=0;
    for(int i=0;i<=sz;i++){
        res+=A[sz-1][i]*vec[i];
    }
    cout<<fixed<<setprecision(10);
    cout<<res<<endl;

    return 0;
}
accuracy: 0.1935483870967742
time_ms: 20
memory_kb: 3436
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ld = long double;

template<class T>
class matrix {
	vector<vector<T>> dat;
public:
	matrix(int row) : dat(row, vector<T>(1)) {}
	matrix(int row, int col) : dat(row, vector<T>(col)) {}
	matrix(int row, int col, const T& id) : dat(row, vector<T>(col, id)) {}
	matrix(const vector<vector<T>>& v) : dat(v) {}
	matrix(const vector<T>& v) : dat(v.size(), vector<T>(1)) {
		for (int i = 0; i < (int)v.size(); ++i) dat[i][0] = v[i];
	}
	explicit operator vector<vector<T>>() const { return dat; }
	int row_size() const { return dat.size(); }
	int col_size() const { return dat[0].size(); }
	matrix<T>& operator+=(const matrix<T>& that) {
		int row = row_size(), col = col_size();
		assert(row_size() == that.row_size() && col_size() == that.col_size());
		for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] += that[i][j];
		return *this;
	}
	matrix<T>& operator-=(const matrix<T>& that) {
		int row = row_size(), col = col_size();
		assert(row_size() == that.row_size() && col_size() == that.col_size());
		for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] -= that[i][j];
		return *this;
	}
	matrix<T>& operator*=(const T& that) {
		int row = row_size(), col = col_size();
		for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] *= that;
		return *this;
	}
	matrix<T>& operator/=(const T& that) {
		int row = row_size(), col = col_size();
		for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) dat[i][j] /= that;
		return *this;
	}
	matrix<T> operator*(const matrix<T>& that) const {
		int x = row_size(), y = col_size(), z = that.col_size();
		assert(col_size() == that.row_size());
		matrix<T> res(x, z);
		for (int i = 0; i < x; ++i) {
			for (int j = 0; j < y; ++j) {
				for (int k = 0; k < z; ++k) {
					res[i][k] += dat[i][j] * that[j][k];
				}
			}
		}
		return res;
	}
	matrix<T>& operator*=(const matrix<T>& that) { return *this = *this * that; }

	vector<T>& operator[](size_t i) & { return dat[i]; }
	const vector<T>& operator[](size_t i) const& { return dat[i]; }
	vector<T> operator[](size_t i) const&& { return move(dat[i]); }

	T& get(int i, int j) & {
		assert(0 <= i && i < row_size());
		assert(0 <= j && j < col_size());
		return dat[i][j];
	}
	const T& get(int i, int j) const& {
		assert(0 <= i && i < row_size());
		assert(0 <= j && j < col_size());
		return dat[i][j];
	}
	T get(int i, int j) const&& {
		assert(0 <= i && i < row_size());
		assert(0 <= j && j < col_size());
		return move(dat[i][j]);
	}
	matrix<T> transpose() const {
		int row = row_size(), col = col_size();
		matrix<T> res(col, row);
		for (int i = 0; i < row; ++i) for (int j = 0; j < col; ++j) res[j][i] = dat[i][j];
		return res;
	}
};

template <class T> matrix<T> operator+(const matrix<T>& a, const matrix<T>& b) { return matrix<T>(a) += b; }
template <class T> matrix<T> operator-(const matrix<T>& a, const matrix<T>& b) { return matrix<T>(a) -= b; }
template <class T> matrix<T> operator*(const matrix<T>& a, const T& b) { return matrix<T>(a) *= b; }
template <class T> matrix<T> operator*(const T& a, const matrix<T>& b) { return matrix<T>(b) *= a; }
template <class T> matrix<T> operator/(const matrix<T>& a, const T& b) { return matrix<T>(a) /= b; }

template <class T> matrix<T> identity_matrix(int n) {
	matrix<T> a(n, n);
	for (int i = 0; i < n; i++) a[i][i] = T(1);
	return a;
}

template <class T> matrix<T> pow(const matrix<T>& a, long long b) {
	const int n = a.row_size();
	assert(a.row_size() == a.col_size() && 0 <= b);
	matrix<T> res = identity_matrix<T>(n), x(a);
	while (true) {
		if (b & 1LL) res *= x;
		if (!(b >>= 1LL)) break;
		x *= x;
	}
	return res;
}

const ld eps = 1e-10;

template <typename T>
bool is_zero(T val) {
	return val == T(0);
}

template <>
bool is_zero(ld val) {
	return std::abs(val) < eps;
}

template <typename T>
vector<T> gauss_jordan(const matrix<T>& A, const vector<T>& b) {
	const int n = A.row_size();
	matrix<T> B(n, n + 1);
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) B[i][j] = A[i][j];
		B[i][n] = b[i];
	}
	for (int i = 0; i < n; ++i) {
		int pivot = i;
		for (int j = i; j < n; ++j) {
			if (std::abs(B[j][i]) > std::abs(B[pivot][i])) pivot = j;
		}
		if (i != pivot) swap(B[i], B[pivot]);
		if (is_zero(B[i][i])) return vector<T>(); // no solution
		for (int j = i + 1; j <= n; ++j) B[i][j] /= B[i][i];
		for (int j = 0; j < n; ++j) {
			if (i == j) continue;
			for (int k = i + 1; k <= n; ++k) B[j][k] -= B[j][i] * B[i][k];
		}
	}
	vector<T> x(n);
	for (int i = 0; i < n; ++i) x[i] = B[i][n];
	return x;
}

int main()
{
	cout << fixed << setprecision(8);
	int S, N, K;
	cin >> S >> N >> K; S = abs(S);
	if (S == 0) {
		cout << 0 << endl;
		return 0;
	}
	if (N == 1) {
		cout << (S % K == 0 ? S / K : -1) << endl;
		return 0;
	}
	vector<ld> dp = { 1 };
	for (int i = 0; i < K; i++) {
		vector<ld> tmp((i + 1) * N + 1);
		for (int j = 0; j <= i * N; j++) {
			for (int k = 1; k <= N; k++) {
				tmp[j + k] += dp[j] / N;
			}
		}
		dp.swap(tmp);
	}
	int M = N * K;
	matrix<ld> mat(M, M);
	vector<ld> b(M);
	mat[0][0] = 1;
	b[0] = 0;
	for (int i = 1; i < M; i++) {
		mat[i][i] = -1;
		ld kei = i * 2 <= M ? 1 / (1 - dp[i * 2]) : 1;
		b[i] -= kei;
		for (int j = 0; j <= M; j++) {
			int p = abs(i - j);
			if (p == i) continue;
			mat[i][p] += dp[j] * kei;
		}
	}
	auto vec = gauss_jordan(mat, b);
	assert(!vec.empty());
	vec.push_back(1);
	matrix<ld> kei(M + 1, M + 1);
	for (int i = 0; i < M - 1; i++) {
		kei[i][i + 1] = 1;
	}
	for (int i = 0; i < M; i++) {
		kei[M - 1][i] = dp[M - i];
	}
	kei[M - 1][M] = 1;
	kei[M][M] = 1;
	kei = pow(kei, S);
	ld res = 0;
	for (int i = 0; i <= M; i++) {
		res += kei[0][i] * vec[i];
	}
	cout << res << endl;
	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3796
final_rank: 7