#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const double EPS = 1.0e-10; int elementaryMatrix(vector >& mat) { const int n = mat.size(); const int m = mat[0].size(); int y = 0; int x = 0; while(y < n && x < m){ int tmp = y; for(int i=y+1; i abs(mat[tmp][x])) tmp = i; } if(abs(mat[tmp][x]) > EPS){ mat[y].swap(mat[tmp]); for(int i=y+1; i=x; --j){ mat[i][j] -= mat[y][j] * (mat[i][x] / mat[y][x]); } } ++ y; } ++ x; } mat.resize(y); return y; } bool linearSystem(vector >& mat, vector& x) { int n = mat[0].size() - 1; if(elementaryMatrix(mat) != n || mat[n-1][n-1] == 0) return false; x.resize(n); for(int i=n-1; i>=0; --i){ x[i] = mat[i][n]; for(int j=i+1; j> n >> m; vector c(n, 0); for(int i=1; i> c[i]; vector > mat(n, vector(n+1, 0.0)); for(int i=0; i n - 1) a -= (a - (n - 1)) * 2; mat[i][a] -= 1.0 / m; } } mat[n-1][n-1] = 1.0; vector x; linearSystem(mat, x); vector dp(n, 0.0); for(int i=n-2; i>n-2-m; --i) dp[i] = c[i]; for(int i=n-2-m; i>=0; --i){ for(int j=1; j<=m; ++j) dp[i] += dp[i+j] / m; for(int j=1; j<=m; ++j) dp[i] = min(dp[i], x[i+j]); dp[i] += c[i]; } printf("%.10f\n", dp[0]); return 0; }