結果

問題 No.463 魔法使いのすごろく🎲
ユーザー omochana2omochana2
提出日時 2019-04-08 00:54:58
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,847 bytes
コンパイル時間 1,545 ms
コンパイル使用メモリ 168,608 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-28 10:03:42
合計ジャッジ時間 2,667 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;
}

0