D - The Moon(2018CCPC吉林赛区)(概率dp)

D - The Moon(2018CCPC吉林赛区)(概率dp)

Time limit:1000 ms
Memory limit:262144 kB
Special judge:Yes
judge:
HDU 6558
vjudge

Description

The Moon card shows a large, full moon in the night’s sky, positioned between two large towers. The Moon is a symbol of intuition, dreams, and the unconscious. The light of the moon is dim, compared to the sun, and only vaguely illuminates the path to higher consciousness which winds between the two towers.

Random Six is a FPS game made by VBI(Various Bug Institution). There is a gift named “Beta Pack”. Mr. K wants to get a beta pack. Here is the rule.
Step 0. Let initial chance rate q q q = 2%.
Step 1. Player plays a round of the game with winning rate p p p.
Step 2. If the player wins, then will go to Step 3 else go to Step 4.
Step 3. Player gets a beta pack with probability q q q. If he doesn’t get it, let q = m i n ( 100 % , q + 2 % ) q = min(100\%, q + 2\%) q=min(100%,q+2%) and he will go to Step 1.
Step 4. Let q = m i n ( 100 % , q + 1.5 % ) q = min(100\%, q + 1.5\%) q=min(100%,q+1.5%) and goto Step 1.
Mr. K has winning rate p p p% , he wants to know what’s the expected number of rounds before he needs to play.

Input

The first line contains testcase number T ( T ≤ 100 ) T (T ≤ 100) T(T100). For each testcase the first line contains an integer p ( 1 ≤ p ≤ 100 ) p (1 ≤ p ≤ 100) p(1p100).

Output

For each testcase print Case i : and then print the answer in one line, with absolute or relative error not exceeding 1 0 6 10^6 106.

Sample Input

2
50
100

Sample Output

Case 1: 12.9933758002
Case 2: 8.5431270393

题意

给你一个p
q的初始值是2

步骤1,有p的概率会跳到步骤3,有1-p的概率会跳到步骤4。
步骤2,啥也没有发生。
步骤三,有q的概率结束游戏!有1-q的概率使q:=q+2,然后跳到步骤1。
步骤4,使q:=q+1.5,然后跳到步骤1。
在这里插入图片描述

题解

把一个回合看作是一个整体,把q看作是变量进行递推。

设d[q]为概率为q时的期望次数。

那么可以得到状态转移图:
在这里插入图片描述

也就是说 q q q 状态有 1 − p 1-p 1p 的概率会转移到 d p [ m i n ( 100 , q + 1.5 ) ] dp[min(100, q+1.5)] dp[min(100,q+1.5)] 的状态,如果发生了,那么 d p [ q ] dp[q] dp[q] 就会变成 d p [ m i n ( 100 , q + 1.5 ) ] dp[min(100, q+1.5)] dp[min(100,q+1.5)] 。那么把 q q q 所有的出处都加起来就能得到 d p [ q ] dp[q] dp[q] 的值。即倒着推。

同时为了消去小数,计算时把 q q q 的范围放大 1 1 1 倍。

状态转移方程:
d p [ q ] = 1 + ( 1 − p ) ∗ d p [ m i n ( 200 , q + 3 ) ] + p ∗ ( 1 − q / 200.0 ) ∗ d p [ m i n ( 200 , q + 4 ) ] dp[q] = 1 + (1 - p) * dp[min(200, q + 3)] + p * (1 - q / 200.0) * dp[min(200, q + 4)] dp[q]=1+(1p)dp[min(200,q+3)]+p(1q/200.0)dp[min(200,q+4)]

边界条件:

q = 100 q=100 q=100 时是边界,此时只要步骤1转移到步骤3就必赢,此回合即赢的概率是 p p p ,否则转移到步骤1继续。符合几何分布,其期望是 1 / p 1/p 1/p

d p [ 200 ] = 1.0 / p dp[200] = 1.0 / p dp[200]=1.0/p

代码

#include <bits/stdc++.h>
#define sc(x) scanf("%d", &x)
using namespace std;

int T, P;
double dp[203];

void sol() {
    
    
	double p = P / 100.0;
	dp[200] = 1.0 / p;
	for (int q = 199; q >= 4; --q) {
    
    
		dp[q] = 1 + (1 - p) * dp[min(200, q + 3)] + p * (1 - q / 200.0) * dp[min(200, q + 4)];
	}
	printf("%.10f\n", dp[4]);
}

int main() {
    
    
	while (cin >> T) {
    
    
		for (int i = 1; i <= T; ++i) {
    
    
			sc(P);
			printf("Case %d: ", i);
			sol();
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42856843/article/details/105456368