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

43 篇文章 0 订阅
23 篇文章 0 订阅

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;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值