ZOJ 3468 Dice War【PD求概率】


ZOJ 3468 Dice War

2010ZOJ月赛B题
大意:有两堆筛子,个数分别为a,b,
求第一堆筛子点数和大于第二堆筛子点数和的概率
算法DP,注意精度,比赛时就死在这里了~~~~(>_<)~~~~

#include < stdio.h >
#include
< string .h >
const int N = 490 ;
const int M = 9 ;
double dp[M][N];
double sum[M][N];
void init()
{
int i,j,k;
for (i = 0 ;i < M;i ++ )
for (j = 0 ;j < N;j ++ )
dp[i][j]
= 0 ;
for (i = 1 ;i <= 6 ;i ++ )
dp[
1 ][i] = 1 ;


for (i = 2 ;i < M;i ++ )
{
for (k = 0 ;k < N;k ++ )
for (j = 1 ;j <= 6 ;j ++ )
{
dp[i][k
+ j] += dp[i - 1 ][k];
}


}

for (i = 1 ;i < M;i ++ )
{
int upper = i * 6 ;
sum[i][upper]
= dp[i][upper];
for (k = upper - 1 ;k >= 0 ;k -- )
sum[i][k]
= sum[i][k + 1 ] + dp[i][k];
}
}
int main()
{
int a,b;
init();
while (scanf( " %d%d " , & a, & b) != EOF)
{
double pre = 1 ;
double ans = 0 ;
// while(a>1)
if (a > 1 )
{
double tans = 0 ;
int i,j;
int ub = b * 6 ;
for (i = b;i <= ub;i ++ )
tans
= tans + sum[a][i + 1 ] * dp[b][i];
int ab = a + b;
while (ab -- )tans /= 6 ;
ans
= ans + tans * pre;
pre
= ( 1 - tans) * pre;

a
= a - 1 ;
// b++;
}

printf(
" %.16lf\n " ,ans);
}
return 0 ;
}

你可能感兴趣的:(ICE)