Play the Dice
傳送門1
傳送門2
There is a dice with n sides, which are numbered from
Input
Input consists of multiple cases. Each case includes two lines.
The first line is an integer
The second line is an integer
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寫
這裏用另一種看起來很高端的寫法.
第一次擲骰子獲得分數期望顯然是
運氣好的話有
……
當然運氣好到爆的話你會擲k次
於是擲k次期望就是
會發現乘號前面其實是等比數列求和,其首項
於是期望爲
也就是說只要記錄
printf("%.2lf\n",1.0*sum/(n-m));
上面是最普通的情況
顯然
n==m
時puts("inf");
,但是別忘了sum==0
的時候你運氣再好一分都沒有(puts("0");
ps:
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;
}