結果

問題 No.463 魔法使いのすごろく🎲
ユーザー mamekinmamekin
提出日時 2017-06-11 21:18:41
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,390 bytes
コンパイル時間 1,044 ms
コンパイル使用メモリ 111,864 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-24 16:35:01
合計ジャッジ時間 2,189 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;
const double EPS = 1.0e-10;
int elementaryMatrix(vector<vector<double> >& 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<n; ++i){
if(abs(mat[i][x]) > abs(mat[tmp][x]))
tmp = i;
}
if(abs(mat[tmp][x]) > EPS){
mat[y].swap(mat[tmp]);
for(int i=y+1; i<n; ++i){
for(int j=m-1; j>=x; --j){
mat[i][j] -= mat[y][j] * (mat[i][x] / mat[y][x]);
}
}
++ y;
}
++ x;
}
mat.resize(y);
return y;
}
bool linearSystem(vector<vector<double> >& mat, vector<double>& 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; ++j)
x[i] -= mat[i][j] * x[j];
x[i] /= mat[i][i];
}
return true;
}
const double INF = DBL_MAX / 10;
int main()
{
int n, m;
cin >> n >> m;
vector<int> c(n, 0);
for(int i=1; i<n-1; ++i)
cin >> c[i];
vector<vector<double> > mat(n, vector<double>(n+1, 0.0));
for(int i=0; i<n-1; ++i){
mat[i][i] = 1.0;
mat[i][n] = c[i];
for(int j=1; j<=m; ++j){
int a = i + j;
if(a > n - 1)
a -= (a - (n - 1)) * 2;
mat[i][a] -= 1.0 / m;
}
}
mat[n-1][n-1] = 1.0;
vector<double> x;
linearSystem(mat, x);
vector<double> 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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0