結果

問題 No.58 イカサマなサイコロ
ユーザー ty70ty70
提出日時 2015-06-09 03:05:00
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,848 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 86,136 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 20:06:50
合計ジャッジ時間 1,230 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
/*
	No.58 イカサマなサイコロ

	★ DP解

	dpT[i][j] := i 個目のサイコロを使い 合計 j になる確率

	dpT[0][0] = 1.
	dpT[i+1][j+l]  += dpT[i][j]/6; (i <  K のとき l = 1,2,3,4,5,6 )
	dpT[i+1][j+l'] += dpT[i][j]/6; (i >= K のとき l'= 4,4,5,5,6,6 )

	★太郎が勝つ確率を求める

	F  := 太郎が勝つ事象
	Ei := 二郎のサイコロの合計が i の事象

	P(F) = ΣP(F ∩ Ei)	(全確率の公式)
	P(F) = P(F ∩ E1) + P(F ∩ E2) + P(F ∩ E3) + ... + P(F ∩ E60)
	P(F) = P(F|E1)P(E1) + P(F|E2)P(E2) + P(F|E3)P(E3) + ... + P(F|E60)P(E60)	(乗法公式、条件付確率の公式)	
	P(F) = P(F)P(E1) + P(F)P(E2) + P(F)P(E3) + ... + P(F)P(E60) (F と Ei は独立事象)
	^	^ ここは個別の確率 P(E1) の場合の P(F) だから 左辺の P(F) とは異なる! 他の P(F) も同様。
	ここは求めたい確率
*/

const int MAX_N = 12;
const int MAX_M = 70;

const int dice[7] = { 0, 1, 2, 3, 4, 5, 6 };
const int fake[7] = { 0, 4, 4, 5, 5, 6, 6 };

double dpT[MAX_N][MAX_M];	// 太郎のDPテーブル
double dpJ[MAX_N][MAX_M];	// 二郎のDPテーブル

int main()
{
	memset (dpT, 0., sizeof (dpT ) );
	memset (dpJ, 0., sizeof (dpJ ) );
	ios_base::sync_with_stdio(0);
	int N, K; cin >> N >> K;

	dpT[0][0] = 1.;
	rep (i, N ){
		for (int j = 6*N; j >= 0; j-- ){
			if (dpT[i][j] != 0. ){
				for (int k = 1; k <= 6; k++ ){
					if (i < K ){
						dpT[i+1][j + fake[k]] += dpT[i][j]/6.;
					}else{
						dpT[i+1][j + dice[k]] += dpT[i][j]/6.;
					} // end if
				} // end for
			} // end if
		} // end for
	} // end rep

	dpJ[0][0] = 1.;
	rep (i, N ){
		for (int j = 6*N; j >= 0; j-- ){
			if (dpJ[i][j] != 0. ){
				for (int k = 1; k <= 6; k++ ){
					dpJ[i+1][j + dice[k]] += dpJ[i][j]/6.;
				} // end for
			} // end if
		} // end for
	} // end rep

	double res = 0.;
	for (int i = 1; i <= 6*N; i++ ){
		for (int j = 1; j < i; j++ ){
			res += dpT[N][i]*dpJ[N][j];
		} // end for
	} // end for

	printf ("%.6lf\n", res );

	return 0;
}
0