結果
問題 | No.463 魔法使いのすごろく🎲 |
ユーザー |
|
提出日時 | 2016-12-14 13:46:09 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 3,777 bytes |
コンパイル時間 | 889 ms |
コンパイル使用メモリ | 82,688 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-30 07:08:16 |
合計ジャッジ時間 | 2,078 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
#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;}