結果

問題 No.133 カードゲーム
ユーザー koyumeishikoyumeishi
提出日時 2015-01-30 17:26:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 1,462 bytes
コンパイル時間 950 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 8,204 KB
最終ジャッジ日時 2023-09-07 09:11:22
合計ジャッジ時間 1,854 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 8 ms
8,176 KB
testcase_06 AC 8 ms
8,036 KB
testcase_07 AC 8 ms
8,032 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 8 ms
8,204 KB
testcase_11 AC 8 ms
8,104 KB
testcase_12 AC 8 ms
8,104 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 8 ms
8,044 KB
testcase_17 AC 8 ms
8,100 KB
testcase_18 AC 8 ms
8,060 KB
testcase_19 AC 8 ms
8,180 KB
testcase_20 AC 8 ms
8,060 KB
testcase_21 AC 8 ms
8,104 KB
testcase_22 AC 8 ms
8,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

double dfs(vector<vector<vector<double>>> &dp, const int &n, const int &a, const int &b, int s, int t, int w){
	if(w<0) return 0.0;
	if(dp[s][t][w] >= 0) return dp[s][t][w];
	if(s==a){
		if(w==0)	return 1.0;
		else return 0.0;
	}

	double ret = 0;

	double p = 1.0/(bit_count(s)+1);
	for(int i=0; i<2*n; i++){
		if( ((1<<i) & a) == 0 ) continue;
		if( ((1<<i) & s) > 0 ) continue;
		for(int j=0; j<2*n; j++){
			if( ((1<<j) & b) == 0 ) continue;
			if( ((1<<j) & t) > 0 ) continue;

			if(i>j){
				ret += dfs(dp, n, a,b,s^(1<<i),t^(1<<j), w-1) * p * p;
			}else if(i<j){
				ret += dfs(dp, n, a,b,s^(1<<i),t^(1<<j), w) * p * p;
			}
		}
	}

	dp[s][t][w] = ret;
	return ret;
}

int main(){
	int n;
	cin >> n;
	vector<int> a(n);
	vector<int> b(n);
	for(int i=0; i<n; i++){
		cin >> a[i];
		a[i]--;
	}
	for(int i=0; i<n; i++){
		cin >> b[i];
		b[i]--;
	}
	
	vector<vector<vector<double>>> dp(1<<(2*n), vector<vector<double>>(1<<(2*n), vector<double>(n+1, -1)));
	int s = 0;
	int t = 0;
	for(int i=0; i<n; i++){
		s |= (1<<a[i]);
		t |= (1<<b[i]);
	}

	double ans = 0.0;
	for(int i=n/2+1; i<=n; i++){
		ans += dfs(dp,n,s,t,0,0, i);
	}

	printf("%.6f\n", ans);

	return 0;
}
0