結果

問題 No.463 魔法使いのすごろく🎲
ユーザー chaemon
提出日時 2016-12-14 00:29:52
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 4,700 bytes
コンパイル時間 1,103 ms
コンパイル使用メモリ 97,812 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-30 01:47:55
合計ジャッジ時間 1,965 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

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

// #includes {{{
#include <algorithm>
#include <numeric>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <list>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <cmath>
using namespace std;
// }}}
// pre-written code {{{
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define RREP(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();++i)
#define LET(x,a) __typeof(a) x(a)
//#define IFOR(i,it,c) for(__typeof((c).begin())it=(c).begin();it!=(c).end();++it,++i)
#define ALL(c) (c).begin(), (c).end()
#define MP make_pair
#define EXIST(e,s) ((s).find(e)!=(s).end())
#define RESET(a) memset((a),0,sizeof(a))
#define SET(a) memset((a),-1,sizeof(a))
#define PB push_back
#define DEC(it,command) __typeof(command) it=command
//debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define debug2(x) cerr << #x << " = [";REP(__ind,(x).size()){cerr << (x)[__ind] << ", ";}cerr << "] (L" << __LINE__ << ")" << endl;
const int INF=0x3f3f3f3f;
typedef long long Int;
typedef unsigned long long uInt;
#ifdef __MINGW32__
typedef double rn;
#else
typedef long double rn;
#endif
typedef pair<int,int> pii;
/*
#ifdef MYDEBUG
#include"debug.h"
#include"print.h"
#endif
*/
// }}}
//{{{ mat
typedef rn nm;
typedef vector<nm> vec;
typedef vector<vec> mat;
//int
/*
nm add(const nm &x,const nm &y){return (x+y)%mod;}
nm opposite(const nm &n){return ((-n)%mod+mod)%mod;}
nm mul(const nm &x,const nm &y){return (unsigned long long)x*y%mod;}
//nm inverse(const nm &n){return invMod(n,mod);}
//nm modulo(const nm &n,int mod=::mod){return ((n+mod)%mod+mod)%mod;}
*/
//real number
nm add(const nm &x,const nm &y){return x+y;}
nm opposite(const nm &n){return -n;}
nm mul(const nm &x,const nm &y){return x*y;}
nm inverse(const nm &n){return 1L/n;}
// O( n )
mat identity(int n) {
mat A(n, vec(n));
for (int i = 0; i < n; ++i) A[i][i] = 1;
return A;
}
// O( n )
nm inner_product(const vec &a, const vec &b) {
nm ans = 0;
for (int i = 0; i < a.size(); ++i)
ans = add(ans,mul(a[i],b[i]));
return ans;
}
mat add(const mat &A, const mat &B) {
mat C(A.size(), vec(B[0].size()));
for (int i = 0; i < C.size(); ++i)
for (int j = 0; j < C[i].size(); ++j)
C[i][j] = add(A[i][j],B[i][j]);
return C;
}
// O( n^2 )
vec mul(const mat &A, const vec &x) {
vec y(A.size());
for (int i = 0; i < A.size(); ++i)
for (int j = 0; j < A[0].size(); ++j)
y[i] = add(y[i], mul(A[i][j],x[j]));
return y;
}
// O( n^3 )
mat mul(const mat &A, const mat &B) {
mat C(A.size(), vec(B[0].size()));
for (int i = 0; i < C.size(); ++i)
for (int j = 0; j < C[i].size(); ++j)
for (int k = 0; k < A[i].size(); ++k)
C[i][j] = add(C[i][j],mul(A[i][k],B[k][j]));
return C;
}
// O( n^3 log e )
mat pow(const mat &A, long long e) {
return e == 0 ? identity(A.size()) :
e % 2 == 0 ? pow(mul(A, A), e/2) : mul(A, pow(A, e-1));
}
//}}}
//{{{ gauss
int gauss(mat &A, vec &b) {
//int gauss(mat& A) {//returns rank
const int n=A.size(),m=A[0].size();
int pi=0;
for(int pj=0;pj<m;pj++){
for(int i = pi+1; i < n; i++) {
if (abs(A[i][pj]) > abs(A[pi][pj])) {
swap(A[i], A[pi]);
swap(b[i], b[pi]);
}
}
if (abs(A[pi][pj]) > 0) {
nm d = inverse(A[pi][pj]);
REP(j, m)
A[pi][j] = mul(A[pi][j],d);
b[pi] = mul(b[pi],d);
REP(i,n){
if(i==pi)continue;
nm k = A[i][pj];
REP(j, m)
A[i][j] = add(A[i][j],opposite(mul(k, A[pi][j])));
b[i] = add(b[i],opposite(mul(k, b[pi])));
}
pi++;
}
}
return pi;
/*
for(int i = pi; i < n; i++)
if (abs(b[i]) > 0)
throw Inconsistent();
if (pi < m || pj < m)
throw Ambiguous();
for(int j = m-1; j >= 0; j--)
REP(i, j)
b[i] = modulo(b[i] - b[j] * A[i][j]);
*/
}
//}}}
int n,m;
rn c[110];
vec b;
rn dp[110];
rn calc(int i){
if(dp[i]>-0.5L)return dp[i];
if(i+m>=n-1)return dp[i]=c[i];
rn ans = 1.0L/0.0L;
REP(j,m){
ans = min(ans,b[i+j+1]);
}
rn s = 0.0L;
REP(j,m){
s += calc(i+j+1);
}
s/=(rn)m;
ans = min(ans,s);
return dp[i]=ans+c[i];
}
int main(){
cin>>n>>m;
REP(i,n)dp[i] = -1.0L;
c[0] = c[n-1] = 0.0L;
REP(i,n-2)cin>>c[i+1];
mat A(n-1,vec(n-1));
b.assign(n-1,0.0L);
REP(i,n-1)A[i][i] -= 1.0L;
REP(i,n-1)b[i] = -c[i];
REP(i,n-1){
REP(j,m){
int s = i+j+1;
if(s>n-1){
int d = s-(n-1);
s = n-1-d;
}
if(s==n-1){
}else{
A[i][s]+=1.0L/m;
}
}
}
int pi = gauss(A,b);
assert(pi==n-1);
/*
REP(i,b.size())cout<<b[i]<<" ";
cout<<endl;
*/
printf("%.10Lf\n",calc(0));
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0