結果
| 問題 |
No.463 魔法使いのすごろく🎲
|
| コンテスト | |
| ユーザー |
omochana2
|
| 提出日時 | 2019-04-08 00:55:34 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 2,000 ms |
| コード長 | 3,850 bytes |
| コンパイル時間 | 1,508 ms |
| コンパイル使用メモリ | 168,188 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-28 10:05:21 |
| 合計ジャッジ時間 | 2,723 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 36 |
ソースコード
#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cout << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
cout<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 300010;
const int MAX_V = 100010;
const double eps = 1e-20;
const ll mod = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////
int get_rank(vector<vi>& B) { //bit vectors, S x S + 1 dimentions
int S = sz(B);
int r = 0;
vector<int> used(S, 0);
rep(i, 0, S) {
int pivot = -1;
rep(j, 0, S) {
if(B[j][i] & !used[j]) pivot = j;
}
if(pivot == -1) continue;
r++;
swap(B[i], B[pivot]);
used[i] = true;
rep(j, 0, S) {
if(i != j && B[j][i]) {
rep(k, 0, S + 1) B[j][k] ^= B[i][k];
}
}
}
return r;
}
vector<double> gauss_jordan(const vector<vector<double>>& A, const vector<double>& b) {
//watch out for precision!!!!!!!
int n = sz(A);
vector<vector<double>> B(n, vector<double>(n + 1));
rep(i, 0, n)
rep(j, 0, n) B[i][j] = A[i][j];
rep(i, 0, n) B[i][n] = b[i];
rep(i, 0, n) {
int pivot = i;
rep(j, i, n) {
if(abs(B[j][i]) > abs(B[pivot][i])) pivot = j;
}
swap(B[i], B[pivot]);
if(abs(B[i][i]) < eps) return vector<double>();
rep(j, i + 1, n + 1) B[i][j] /= B[i][i];
rep(j, 0, n) {
if(i != j) {
rep(k, i + 1, n + 1) B[j][k] -= B[j][i] * B[i][k];
}
}
}
vector<double> x(n);
rep(i, 0, n) x[i] = B[i][n];
return x;
}
/////////////////////////////////////////////////////////////////
int N, M;
double C[MAX_N];
double dp[MAX_N];
void solve() {
cin >> N >> M;
rep(i, 1, N - 1) cin >> C[i];
vector<vector<double>> A(N, vector<double>(N, 0.0));
vector<double> b(N, 0.0);
rep(i, 0, N - 1) {
A[i][i] = 1.0;
rep(j, 1, M + 1) {
if(i + j != N - 1) {
int nex = i + j < N - 1 ? i + j : 2 * N - 2 - (i + j);
// debug(i, nex);
b[i] += 1.0 / M * C[nex];
A[i][nex] -= 1.0 / M;
}
}
}
A[N - 1][N - 1] = 1;
b = gauss_jordan(A, b);
// debug(b);
rer(i, N - 1, 0) {
if(i + M >= N - 1) dp[i] = 0;
else {
double tmp = 0;
rep(j, 1, M + 1) {
int nex = i + j < N - 1 ? i + j : 2 * N - 2 - (i + j);
tmp += 1.0 / M * (dp[nex] + C[nex]);
}
dp[i] = min(tmp, b[i]);
rep(j, 1, M + 1) {
int nex = i + j < N - 1 ? i + j : 2 * N - 2 - (i + j);
MIN(dp[i], b[nex] + C[nex]);
}
}
}
cout << dp[0] << "\n";
}
int main() {
#ifndef LOCAL
ios::sync_with_stdio(false);
cin.tie(0);
#endif
cout << fixed;
cout.precision(20);
srand((unsigned int)time(NULL));
#ifdef LOCAL
//freopen("in.txt", "wt", stdout); //for tester
freopen("in.txt", "rt", stdin);
#endif
solve();
#ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
omochana2