zoj3551 Bloodsucker ——概率DP

August 18, 2013
DP

Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4530

A[i]数组表示当吸血鬼有 I 个的时候,还需要的天数。可以列出方程:

A[i] = p1(a[i+1]+1) + (1-p1)(a[i]+1) p1=(i*(n-i))p / (n(n-1)/2)

这道题目错了很多次,原因就是强制类型转换的时候,有一个细节,

(double)(n(n-1))/2.0 是错的,应该写成:(double)n(n-1)/2.0。这个小细节以后注意吧

# include<stdio.h>
# include<string.h>
# include<stdlib.h>
int main(void)
{
//    freopen("in.txt", "r",stdin );
    int t; scanf("%d",&t);
    while (t--)
    {
        int n; double p;
        scanf("%d%lf",&n,&p);
        double sad=0.0,dw=(double)n*(n-1)/2.0;
        for(int i=n-1;i>=1;--i)
        {
            double lxy=i; lxy*=(n-i); lxy*=p;
            lxy=lxy/dw;
            sad+=1.0/lxy;
        }
        printf("%.3f\n",sad);
    }
    return 0;
}

O(╯□╰)o

comments powered by Disqus