题意:
小明每晚都玩游戏,每一盘赢的概率都是p,如果第一盘就赢了,那么就去睡觉;否则继续玩,玩到赢的比例大于p才去睡;
如果一直玩了n盘还没完成,就再也不玩了;
问他玩游戏天数的期望;
思路:
和之前的麻球那题一样,每一晚都是独立的;所以我们先求一天,赢的比例一直不超过p的概率;
如果求出一天就这样的概率是s,那么期望天数就是1/s;
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 105;
double dp[N][N], p;
int n, a, b;
char c;
int main(){
int t;
int cas = 1;
scanf("%d", &t);
while(t--){
scanf("%d%c%d", &a, &c, &b);
p = (double) a / b;
scanf("%d", &n);
memset(dp, 0, sizeof(dp));
dp[1][0] = 1.0 - p;
for(int i = 2; i <= n; i++){
for(int j = 0; j <= n; j++){
dp[i][j] += dp[i-1][j] * (1.0-p);
if((j + 1) * b > a * i)
break;
dp[i][j+1] = dp[i-1][j] * p;
}
}
double s = 0.0;
for(int i = 0; i <= n; i++){
s += dp[n][i];
}
printf("Case #%d: %d\n", cas++, (int)(1.0 / s));
}
return 0;
}