結果

問題 No.174 カードゲーム(Hard)
ユーザー nsd_fbnsd_fb
提出日時 2015-03-27 00:59:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 417 ms / 2,000 ms
コード長 1,934 bytes
コンパイル時間 1,291 ms
コンパイル使用メモリ 154,620 KB
実行使用メモリ 19,748 KB
最終ジャッジ日時 2023-09-21 02:29:49
合計ジャッジ時間 5,288 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 413 ms
19,544 KB
testcase_03 AC 411 ms
19,748 KB
testcase_04 AC 412 ms
19,560 KB
testcase_05 AC 417 ms
19,688 KB
testcase_06 AC 411 ms
19,552 KB
testcase_07 AC 411 ms
19,568 KB
testcase_08 AC 409 ms
19,684 KB
testcase_09 AC 410 ms
19,552 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <sys/time.h>

#ifdef LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif

using namespace std;

#define REP(i, a, b) for(int i = (a); i < int(b); ++i)
#define rep(i, n) REP(i, 0, n)
#define ALL(x) begin(x), end(x)

template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }
template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }

vector<vector<double>> calc(int n, double p) {
	const double q = 1.0 - p;

	vector<vector<double>> res(n, vector<double>(n, 0));
	vector<double> dp(1 << n, 0.0);
	vector<double> next_dp(1 << n, 0.0);
	dp[(1 << n) - 1] = 1.0;

	for(int i = 0; i < n; ++i) {
		const int num = n - i;
		fill(begin(next_dp), end(next_dp), 0.0);

		double sum = 0.0;
		for(int card = 0; card < (1 << n); ++card) {
			if(__builtin_popcount(card) != num) continue;
			const auto cp = dp[card];

			bool first = true;

			for(int j = 0; j < n; ++j) {
				if((card >> j) & 1) {
					double np;
					if(num == 1) {
						np = cp;
					}
					else if(first) {
						np = cp * p;
					}
					else {
						np = cp * q / (num - 1);
					}

					first = false;
					res[j][i] += np;
					next_dp[card ^ (1 << j)] += np;
				}
			}

			sum += cp;
		}

//		assert(abs(sum - 1.0) < 1e-8);
		dp.swap(next_dp);
	}

	return res;
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout.setf(ios::fixed);
	cout.precision(10);

	int n;
	double pa, pb;

	cin >> n >> pa >> pb;

	vector<int> a(n);
	vector<int> b(n);

	for(auto &e : a) cin >> e;
	for(auto &e : b) cin >> e;

	sort(begin(a), end(a));
	sort(begin(b), end(b));

	const auto pa_card = calc(n, pa);
	const auto pb_card = calc(n, pb);

	double ans = 0.0;
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < n; ++j) {
			if(a[i] < b[j]) continue;

			for(int k = 0; k < n; ++k) {
				ans += (a[i] + b[j]) * pa_card[i][k] * pb_card[j][k];
			}
		}
	}

	cout << ans << endl;

	return EXIT_SUCCESS;
}
0