1023: Pong’s Birds
时间限制: 1 Sec 内存限制: 128 MB提交: 137 解决: 33
[ 提交][ 状态][ 讨论版]
题目描述
In order to train his birds, Pong is holding a competition for them. (He have birds, does he?) He have 2n
birds,
so he want to hold elimination series. Literally, if n = 2 and he has 4 birds identified as 1,2,3,4, he would first hold competition for 1 and 2, 3 and 4, the one fails would be eliminated and then he holds competition for the winners.
According to his long observation, for each pair of birds he knows the probability to win for each bird, and he want to know the probability of each bird to be the final winner.
so he want to hold elimination series. Literally, if n = 2 and he has 4 birds identified as 1,2,3,4, he would first hold competition for 1 and 2, 3 and 4, the one fails would be eliminated and then he holds competition for the winners.
According to his long observation, for each pair of birds he knows the probability to win for each bird, and he want to know the probability of each bird to be the final winner.
输入
For the first row there is an integer T(T ≤ 100), which is the number of test cases.
For each case , first row is an integer n(1 ≤ n ≤ 7), for the next 2n rows , each row has 2n real numbers as the probability to win for the i-th when competed to j-th bird. It is promised that for each i, j p[i][j] + p[j][i] = 1 and p[i][i] = 0;
For each case , first row is an integer n(1 ≤ n ≤ 7), for the next 2n rows , each row has 2n real numbers as the probability to win for the i-th when competed to j-th bird. It is promised that for each i, j p[i][j] + p[j][i] = 1 and p[i][i] = 0;
输出
For each case you should output a line with 2n
real numbers, which is the probability of i-th bird be the final
winner. Rounded to 3 decimal places.
Make sure to output a space character after each number, to prevent the Presentation Error.
Make sure to output a space character after each number, to prevent the Presentation Error.
样例输入
1
1
0 0.5
0.5 0
样例输出
0.500 0.500
题目大意:
给你2^N只鸟.现在进行2^N-1场比赛绝出冠军,问你每只鸟获得冠军的概率。
给出2^N只鸟互相战斗的胜率。
比赛规则:
相邻的两只鸟进行战斗,假如现在有1 2 3 4
一开始1和2战斗,3和4战斗。如果:2和4胜利了。那么2和4绝出冠军。
思路:
概率问题,暴力肯定要超时,那么我们考虑概率Dp.
设定Dp【i】【j】表示第i轮比赛,j获得了胜利的概率。
那么就有:
Dp【i】【j】=Dp【i-1】【j】*Dp【i-1】【k】*a【j】【k】.
这里j和k要在这一轮有可能战斗上才行。
所以预处理出来N轮比赛每只鸟可以和哪些鸟战斗即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
vector<int >mp[10][513];
double dp[10][513];
double a[513][513];
int color[10][513];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=(int)pow(2,n);i++)
{
for(int j=1;j<=(int)pow(2,n);j++)
{
scanf("%lf",&a[i][j]);
}
}
memset(color,0,sizeof(color));
for(int i=1;i<=n;i++)
{
int fanwei=(int)pow(2,i);
int cnt=0;
for(int j=1;j<=(int)pow(2,n);j+=fanwei)
{
cnt++;
for(int k=j;k<j+fanwei;k++)
{
color[i][k]=cnt;
}
}
}
memset(dp,0,sizeof(dp));
for(int i=1;i<=(int)pow(2,n);i++)dp[0][i]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=(int)pow(2,n);j++)
{
for(int k=1;k<=(int )pow(2,n);k++)
{
if(j==k)continue;
if(color[i][j]==color[i][k])
{
if(i==1)
{
dp[i][j]+=dp[i-1][j]*dp[i-1][k]*a[j][k];
}
else
{
if(color[i-1][j]!=color[i-1][k])
{
dp[i][j]+=dp[i-1][j]*dp[i-1][k]*a[j][k];
}
}
}
}
}
}
for(int i=1;i<=(int )pow(2,n);i++)
{
if(i>1)printf(" ");
printf("%.3lf",dp[n][i]);
}
printf("\n");
}
}