【HDU - 6558】The Moon(期望dp)
題干:
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?qq?= 2%.
Step 1. Player plays a round of the game with winning rate?pp.
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?qq. If he doesn’t get it, let?qq?= min(100%,?qq?+ 2%) and he will go to Step 1.
Step 4. Let?qq?= min(100%,?qq?+ 1.5%) and goto Step 1.
Mr. K has winning rate?pp% , he wants to know what’s the expected number of rounds before he needs to play.
Input
The first line contains testcase number?TT?(TT?≤ 100). For each testcase the first line contains an integer?pp?(1 ≤?pp?≤ 100).
Output
For each testcase print?Case?ii?: and then print the answer in one line, with absolute or relative error not exceeding?106106.
Sample Input
2 50 100Sample Output
Case 1: 12.9933758002 Case 2: 8.5431270393題目大意:
為了得到一件獎品而玩一個游戲(游戲有好多局),得到獎品后游戲結束。游戲中的每一局都有p%的概率會贏(p是個給定的常數),初始中獎率q=2,如果這一局贏了就抽獎,沒抽到就q+2,然后繼續游戲的下一局;如果這一局輸了就q+1.5。問玩的游戲局數的期望。中獎率最高為100%,也就是說如果這一局贏了且沒抽到獎,那q=min(q+2,100%) 如果這一局輸了,那q=min(q+1.5,100%)。
解題報告:
設dp[i]代表當前狀態的中獎率為 i% 時,得到獎品的期望天數。
因為已知狀態是終點的狀態,所以倒著dp。考慮初始化,dp[100]=1/p。(當然,注意p上來要先除以100,因為他給的是p%)
又因為此時中獎的概率是100%,也就是說贏即可以中獎,也就是轉化成贏的期望局數,贏一局的概率是p,顯然這符合幾何概型,所以期望局數=1/p。
然后轉移就是? ?dp[i]=? ? ? ? ? p *? ? ? ?[ q*1+(1-q)*(dp[i+2]+1)] + (1-p) * (dp[i+1.5] + 1)?
分別代表:? ? ? ?? ?贏游戲的前提下? ?( 中獎? ? ?沒中獎? ? ? ? ?)? ??? ? ? ? 沒贏游戲
然后化簡一下公式就可以了。注意這里因為有0.5不能放到狀態中,所以直接都*2,這樣狀態變成0~200.這樣就方便轉移了。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; double dp[255],p; int main() {int T,iCase=0;cin>>T;while(T--) {scanf("%lf",&p); p/=100;double gp = 1.0/p;dp[200] = gp;for(int i = 199; i>=1; i--) {dp[i] = p*((1-i/200.0)*dp[min(i+4,200)]+1) + (1-p)*(dp[min(i+3,200)]+1);}printf("Case %d: %.10f\n",++iCase,dp[4]);}return 0 ; }?
總結
以上是生活随笔為你收集整理的【HDU - 6558】The Moon(期望dp)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: switpa.exe - switpa是
- 下一篇: 【HDU - 2665】Kth numb