zoukankan      html  css  js  c++  java
  • ZOJ3551Bloodsucker (数学期望)

    In 0th day, there are n-1 people and 1 bloodsucker. Every day, two and only two of them meet. Nothing will happen if they are of the same species, that is, a people meets a people or a bloodsucker meets a bloodsucker. Otherwise, people may be transformed into bloodsucker with probability p. Sooner or later(D days), all people will be turned into bloodsucker. Calculate the mathematical expectation of D.

    Input

    The number of test cases (TT ≤ 100) is given in the first line of the input. Each case consists of an integer n and a float number p (1 ≤ n < 100000, 0 < p ≤ 1, accurate to 3 digits after decimal point), separated by spaces.

    Output

    For each case, you should output the expectation(3 digits after the decimal point) in a single line.

    Sample Input

    1
    2 1
    
    

    Sample Output

    1.000
    

     题意:

    开始有一个吸血鬼,n-1个平民百姓。每天一个百姓被感染的概率可求,问每个人都变成吸血鬼的天数期望。

    思路:

    一般期望题逆推,设dp[i]是目前已经有i个吸血鬼,所有人变成吸血鬼的期望。则dp[n]=0;答案是dp[1]。每一个dp[i]的感染概率可求是p[]=2.0*(n-i)*i/(n-1)/n*p; 

    则可得递推公式: dp[i] = (dp[i+1]*p[]+1)/p[];  

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    using namespace std;
    double dp[100010],p,tmp;
    int main()
    {
        int T,n,i;
        scanf("%d",&T);
        while(T--){
            scanf("%d%lf",&n,&p);
            dp[n]=0;
            for(i=n-1;i>=1;i--) {
                tmp=2.0*(n-i)*i/(n-1)/n*p; 
                dp[i] = (dp[i+1]*tmp+1)/tmp;  
            }
            printf("%.3lf
    ",dp[1]);
        }
        return 0;
    }
  • 相关阅读:
    spark on yarn模式下内存资源管理(笔记1)
    面试题10.3-变态跳台阶
    面试题10.2-青蛙跳
    面试题9-斐波那契数列
    面试题9-用两个栈来实现一个队列,完成队列的Push和Pop操作
    面试题6:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList
    鸢尾花数据集-iris.data
    class之cls
    python 装饰器
    supervisor python开发的进程管理工具
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8041900.html
Copyright © 2011-2022 走看看