結果

問題 No.174 カードゲーム(Hard)
ユーザー koyumeishikoyumeishi
提出日時 2015-03-27 02:05:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,239 ms / 2,000 ms
コード長 1,689 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 83,520 KB
実行使用メモリ 11,880 KB
最終ジャッジ日時 2023-09-21 02:48:16
合計ジャッジ時間 11,386 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1,238 ms
11,880 KB
testcase_03 AC 1,237 ms
11,732 KB
testcase_04 AC 1,237 ms
11,744 KB
testcase_05 AC 1,232 ms
11,784 KB
testcase_06 AC 1,238 ms
11,716 KB
testcase_07 AC 1,237 ms
11,720 KB
testcase_08 AC 1,239 ms
11,844 KB
testcase_09 AC 1,231 ms
11,740 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
#include <ctime>
using namespace std;

int bit_count(int s){
	int ret = 0;
	while(s>0){
		ret++;
		s -= s&-s;
	}
	return ret;
}

int main(){
	int n;
	double pa,pb;
	cin >> n >> pa >> pb;

	vector<int> a(n),b(n);
	for(int i=0; i<n; i++){
		cin >> a[i];
	}
	sort(a.begin(), a.end());

	for(int i=0; i<n; i++){
		cin >> b[i];
	}
	sort(b.begin(), b.end());


	// O(2^n * n^2)
	auto get_prob = [&](double p){
		vector<double> dp_s(1<<n, 0);
		dp_s[(1<<n)-1] = 1.0;
		for(int i=n; i>0; i--){
			for(int j=0; j<(1<<n); j++){
				if(bit_count(j) != i) continue;

				for(int k=0; k<n; k++){
					if((j>>k)&1){
						double p_;
						if(bit_count(j) == 1) p_ = 1.0;
						else if( (1<<k) == (j&-j) ) p_ = p;
						else p_ = (1.0-p)/(bit_count(j)-1);

						dp_s[ j & ~(1<<k) ] += dp_s[j] * p_;
					}
				}

			}
		}

		//dp[k][x] := 残りk枚の時カードxを出す確率
		vector<vector<double>> dp(n+1, vector<double>(n, 0));
		for(int i=0; i<(1<<n); i++){
			int cnt = bit_count(i);
			for(int k=0; k<n; k++){
				if((i>>k)&1){
					double p_;
					if(bit_count(i) == 1) p_ = 1.0;
					else if( (1<<k) == (i&-i) ) p_ = p;
					else p_ = (1.0-p)/(bit_count(i)-1);
					dp[cnt][k] += dp_s[i]*p_;
				}
			}
		}

		return dp;
	};

	auto prob_a = get_prob(pa);
	auto prob_b = get_prob(pb);

	double e = 0;
	for(int k=0; k<=n; k++){
		for(int i=0; i<n; i++){
			for(int j=0; j<n; j++){
				if(a[i] < b[j]) continue;
				e += (a[i]+b[j]) * prob_a[k][i] * prob_b[k][j];
			}
		}
	}

	printf("%.16f\n", e);


	return 0;
}
0