HDU4586 Play the Dice(數論)

Play the Dice

傳送門1
傳送門2
There is a dice with n sides, which are numbered from 1,2,...,n and have the equal possibility to show up when one rolls a dice. Each side has an integer ai on it. Now here is a game that you can roll this dice once, if the i-th side is up, you will get ai yuan. What’s more, some sids of this dice are colored with a special different color. If you turn this side up, you will get once more chance to roll the dice. When you roll the dice for the second time, you still have the opportunity to win money and rolling chance. Now you need to calculate the expectations of money that we get after playing the game once.

Input

Input consists of multiple cases. Each case includes two lines.
The first line is an integer n(2<=n<=200) ,following with n integers ai(0<=ai<200)
The second line is an integer m(0<=m<=n) , following with m integers bi(1<=bi<=n) , which are the numbers of the special sides to get another more chance.

Output

Just a real number which is the expectations of the money one can get, rounded to exact two digits. If you can get unlimited money, print inf.

Sample Input

6 1 2 3 4 5 6
0
4 0 0 0 0
1 3

Sample Output

3.50
0.00


題意

有一個正n面體骰子,給出每個面的得分。接着是m,表示丟到哪些面後可以獲得再擲一次的機會,問最後得分的期望。

分析

顯然這道題可以用概率dp/期望dp寫
這裏用另一種看起來很高端的寫法.
第一次擲骰子獲得分數期望顯然是

i=1nA[i]n

運氣好的話有mn 的概率擲第2次,那麼擲第2次的期望爲
mn×i=1nA[i]n

……
當然運氣好到爆的話你會擲k次(k)擲第k次的期望爲
(mn)k×i=1nA[i]n

於是擲k次期望就是
(i=0k(mn)i)×i=1nA[i]n

會發現乘號前面其實是等比數列求和,其首項ai=1 ,公比q=mn ,前k項和
Sk=1×(1(mn)k)1mn,

limkSk=nnm.

於是期望爲
nnm×i=1nA[i]n=i=1nA[i]nm

也就是說只要記錄A[i] 的和sum ,再除以nm 就是最後答案.
printf("%.2lf\n",1.0*sum/(n-m));
上面是最普通的情況(n>m) .
顯然n==mputs("inf");,但是別忘了sum==0的時候你運氣再好一分都沒有(很坑),也就是puts("0");


ps:好好的概率dp題寫成這個樣子

CODE

#include<cstdio>
#define N 205
int A[N],t;
int main() {
    int n,m;
    while(~scanf("%d",&n)) {
        int sum=0;
        for(int i=1;i<=n;i++) {
            scanf("%d",&A[i]);
            sum+=A[i];
        }
        scanf("%d",&m);
        for(int i=1;i<=m;i++)scanf("%d",&t);//顯然b[i]沒有任何用處
        if(sum==0)puts("0");
        else if(n==m)puts("inf");
        else printf("%.2lf\n",1.0*sum/(n-m));
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章