結果

問題 No.463 魔法使いのすごろく🎲
ユーザー kimiyukikimiyuki
提出日時 2016-12-14 13:46:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,777 bytes
コンパイル時間 839 ms
コンパイル使用メモリ 81,632 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 06:33:38
合計ジャッジ時間 2,237 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <vector>
#include <algorithm>
#include <array>
#include <set>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <functional>
#include <cmath>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i))
#define repeat_reverse(i,n) for (int i = (n)-1; (i) >= 0; --(i))
#define repeat_from_reverse(i,m,n) for (int i = (n)-1; (i) >= int(m); --(i))
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
typedef long long ll;
using namespace std;
template <class T> void setmax(T & a, T const & b) { if (a < b) a = b; }
template <class T> void setmin(T & a, T const & b) { if (b < a) a = b; }
template <typename X, typename T> auto vectors(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto vectors(X x, Y y, Z z, Zs... zs) { auto cont = vectors(y, z, zs...); return vector<decltype(cont)>(x, cont); }

using real = double;
const real eps = 1e-14;

vector<vector<real> > operator * (vector<vector<real> > const & p, vector<vector<real> > const & q) {
    int n = p.size();
    vector<vector<real> > r(n, vector<real>(n));
    repeat (y,n) repeat (z,n) repeat (x,n) r[y][x] += p[y][z] * q[z][x];
    return r;
}
vector<real> operator * (vector<vector<real> > const & p, vector<real> const & q) {
    int n = p.size();
    vector<real> r(n);
    repeat (y,n) repeat (z,n) r[y] += p[y][z] * q[z];
    return r;
}
vector<vector<real> > unit_matrix(int n) {
    vector<vector<real> > e(n, vector<real>(n));
    repeat (i,n) e[i][i] = 1;
    return e;
}
vector<vector<real> > zero_matrix(int n) {
    vector<vector<real> > o(n, vector<real>(n));
    return o;
}

vector<real> gaussian_elimination(vector<vector<real> > f, vector<real> x) {
    int n = x.size();
    repeat (y,n) {
        int pivot = y;
        while (pivot < n and abs(f[pivot][y]) < eps) ++ pivot;
        assert (pivot < n);
        swap(f[y], f[pivot]);
        x[y] /= f[y][y];
        repeat_from (x,y+1,n) f[y][x] /= f[y][y];
        f[y][y] = 1;
        repeat (ny,n) if (ny != y) {
            x[ny] -= f[ny][y] * x[y];
            repeat_from (x,y+1,n) f[ny][x] -= f[ny][y] * f[y][x];
            f[ny][y] = 0;
        }
    }
    return x;
}

int main() {
    int n, m; scanf("%d%d", &n, &m);
    vector<int> c(n); repeat (i,n-2) scanf("%d", &c[i+1]);
    vector<real> init; {
        vector<vector<real> > f = vectors(m-1, m-1, real());
        vector<real> v = vectors(m-1, real());
        repeat (y,m-1) {
            f[y][y] += m;
            repeat (x,m) {
                int z = min(y+x+1, 2*(m-1)-(y+x+1));
                if (z == m-1) continue;
                f[y][z] -= 1;
                v[y] += c[n-m+z];
            }
        }
// repeat (y,m-1) { repeat (x,m-1) { cerr << f[y][x] << ' '; } cerr << " : " << v[y]<< endl; }
        init = gaussian_elimination(f, v);
// cerr << "-> "; repeat (y,m-1) cerr << init[y] << ' ' ; cerr << endl;
    }
    vector<real> dp1(n);
    repeat_reverse (i,n) {
        if (i == n-1) {
            // zero
        } else if (n-m <= i) {
            dp1[i] = init[i-(n-m)];
        } else {
            repeat (j,m) dp1[i] += (c[i+j+1] + dp1[i+j+1]) / m;
        }
    }
    vector<real> dp0(n);
    repeat_reverse (i,n) {
        if (n-m-1 <= i) {
            // zero
        } else {
            repeat (j,m) dp0[i] += (c[i+j+1] + dp0[i+j+1]) / m;
            repeat (j,m) setmin(dp0[i], c[i+j+1] + dp1[i+j+1]);
        }
    }
// repeat (i,n) cerr << dp1[i] << ' '; cerr << endl;
// repeat (i,n) cerr << dp0[i] << ' '; cerr << endl;
    printf("%.12lf\n", dp0[0]);
    return 0;
}
0