您的位置:首页 > 其它

AOJ-AHU-OJ-294 NBA Finals

2014-03-18 19:17 387 查看
[align=center]NBA Finals[/align]
[align=center]Time Limit: 1000 ms   Case Time Limit: 1000 ms   Memory Limit: 64 MB
[/align]
[align=center][/align]
Description
Consider two teams, Lakers and Celtics, playing a series of NBA Finals until one of the teams wins n games. Assume that the probability of Lakers winning a game is the same for each game and equal to p and the probability of Lakers
losing a game is q = 1-p. Hence, there are no ties.Please find the probability of Lakers winning the NBA Finals if the probability of it winning a game is p.

Input
1st line: the game number (7<=n<=165) the winner played.

2nd line: the probability of Lakers winning a game p (0<p<1).

Output
1st line: The probability of Lakers winning the NBA Finals.

Round the result to 6 digits after the decimal point and keep trailing zeros.

Sample Input
OriginalTransformed
7
0.4


Sample Output
OriginalTransformed
0.228844

——————————————————刚吃饱了的分割线——————————————————

思路:简单分析之后,知道了此题涉及两个问题:1.N局M胜(M=N/2+1)制 2.组合数

先解决组合数的问题。很明显,涉及阶乘。阶乘弄不好就会超时或者溢出。解决方法:打表。打表避免了递归。组合数的打表模板是这样的:

Com[0][0] = 1;
for(int i = 1; i < MAXN; i++)
for(int j = 0; j <= i; j++){
if(!j || j==i) Com[i][j] = 1;
else Com[i][j] = Com[i-1][j] + Com[i-1][j-1];
}
其次是排列组合的问题。对于N局M胜的概率问题,当时我推导了一遍公式。经验是:肯定是以胜局结尾。然后按一共打了几场分类讨论。公式如下:

i = 0 ——> i = game-win-1
ans += Com[game-i-1][win-1] × p^win × (1-p)^(game-win-i);代码如下:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAXN 330
double Com[MAXN][MAXN];
double power(double a, int n)
{
double u = 1;
while(n--) u *= a;
return u;
}
int main(){
int win, game;//game表示是N局,win表示M胜
double p;
Com[0][0] = 1;
for(int i = 1; i < MAXN; i++)
for(int j = 0; j <= i; j++){
if(!j || j==i) Com[i][j] = 1;
else Com[i][j] = Com[i-1][j] + Com[i-1][j-1];
}
while(scanf("%d%lf", &win, &p)!=EOF){
double ans = 0;
game = win*2 - 1;
ans += power(p, win);//首先算出打了M场的时候,概率的值
for(int i = 0; i < game-win; i++)
ans += Com[game-i-1][win-1] * power(p, win) * power(1-p, game-win-i);
printf("%.6lf\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: