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
#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;
}

你可能感兴趣的:(数论)