結果

問題 No.162 8020運動
ユーザー 👑 kmjpkmjp
提出日時 2015-03-07 00:42:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,799 bytes
コンパイル時間 1,594 ms
コンパイル使用メモリ 144,860 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 20:29:48
合計ジャッジ時間 2,854 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

double memo[70][20][2];
int P[3];

// N本の歯がY年後に何本残るか?
double func(int Y, int N, int first) {
	
	// おめでとうございます。80歳までN本残りました。
	if(Y == 0)
		return N;
	
	// もう歯がありません。
	if(N <= 0)
		return 0;
	
	// メモ参照
	if(memo[Y][N][first] >= 0)
		return memo[Y][N][first];
	
	if(N == 1) {
		// 歯が1本の時は確率が独特なので、別処理した方が楽です。
		// 1年残る確率×(Y-1)年残る確率
		// もともと1本しかないか、何本かあるうちの最後かで確率が異なる
		memo[Y][N][first] = (100-P[first==0])/100.0 * func(Y-1, 1, 1);
	}
	else {
		memo[Y][N][first] = 0;
		// 残りの歯のうち、左から見て最初に虫歯になる歯は?
		double prob=1;
		for(int id=0; id < N; id++) {
			//id番目が虫歯になる確率は?
			double bad;
			if((id==0 && first) || id==N-1) //両端だけ確率が異なる
				bad = P[1]/100.0;
			else //両端以外
				bad = P[2]/100.0;
			
			// id番目が初めて虫歯になるケース
			// id+1本目以降は再帰で年を変えず処理する
			memo[Y][N][first] += prob * bad * (func(Y-1, id, 1) + func(Y, N-id-1, 0));
			
			// id番目までの歯が残るケース
			prob *= 1-bad;
		}
		// N本全部残るケース
		memo[Y][N][first] += prob * func(Y-1, N, 1);
	}
	
	return memo[Y][N][first];
}

int main(int argc,char** argv){
	int x,y,T;
	
	cin >> T;
	cin >> P[0] >> P[1] >> P[2];
	
	// メモ初期化
	for(x = 0; x < 70; x++)
		for(y = 0; y < 20; y++)
			memo[x][y][0] = memo[x][y][1] = -1;
	
	// 上の歯と下の歯の期待値は同じなので、片方を求めて2倍すればよい
	printf("%.9lf\n" , 2 * func(80-T, 14, 1));
	
	return 0;
}
0