Link to this code:
https://cses.fi/paste/1052a83da5315cb7c5d15b/#pragma GCC optimize("Ofast")
#include"bits/stdc++.h"
using namespace std;
#define endl '\n'
#define mod 1000000007
template<class T, int n = 601, int m = 601>
struct Matrix : array<array<T, m>, n> {
constexpr Matrix() : std::array<std::array<T, m>, n>{} {}
template<int m2>
constexpr Matrix<T, n, m2> operator *(const Matrix<T, m, m2>& other) const {
Matrix<T, n, m2> result;
for (int i = 0 ; i < n ; i++) {
for (int k = 0 ; k < m ; k++) {
for (int j = 0 ; j < m2 ; j++) {
result[i][j] += (*this)[i][k] * other[k][j];
}
}
}
return result;
}
};
// in some compilers you have to remove this or it considers any usages of it also constexpr ._. (e.g CSES)
constexpr auto get_transition () {
Matrix<double> transition;
for (int i = 0 ; i < 601 ; i++) {
for (int j = 1 ; j <= 6 ; j++) if (i + j < 601) transition[i][i+j] = 1/6.0;
}
return transition;
}
// constexpr a prefix of these definitions to offload them to compile time
constexpr auto p0 = get_transition();
constexpr auto p1 = p0*p0;
constexpr auto p2 = p1*p1;
constexpr auto p3 = p2*p2;
auto p4 = p3*p3;
auto p5 = p4*p4;
auto p6 = p5*p5;
auto p7 = p6*p6;
auto powers = array{p0, p1, p2, p3, p4, p5, p6, p7};
signed main () {
cin.tie(0)->sync_with_stdio(0);
int n, a, b;
cin >> n >> a >> b;
double ans = 0;
Matrix<double> result;
result[0][0] = 1;
for (int i = 0 ; i < 8 ; i++) {
if (n & (1 << i)) result = result * powers[i];
}
for (signed i = a ; i <= b ; i++) ans += result[0][i];
cout << setprecision(6) << fixed << ans;
}