zoukankan      html  css  js  c++  java
  • ZOJ 3551 Bloodsucker

    Bloodsucker

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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 (T, T ≤ 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

    概率DP入门题

    设dp[i]表示从i个吸血鬼变成n个吸血鬼的期望天数,p1是已有i个吸血鬼时第二天有人变成吸血鬼的概率,p2是没人变成吸血鬼的概率。

    则:dp[i]=(dp[i+1]+1)*p1+(dp[i]+1)*p2

    移项后化简得: p1*dp[i]=dp[i+1]*p1+1

    注意这里是倒着做的,因为dp[n]=0,所以可以逆着求出dp[n-1]…dp[1]

     1 #include<iostream>
     2 #include<cstdio>
     3 
     4 using namespace std;
     5 
     6 long n;
     7 double p,p1;
     8 double dp[100001];
     9 
    10 int main()
    11 {
    12     int t;
    13 
    14     cin>>t;
    15 
    16     while(t--)
    17     {
    18         cin>>n>>p;
    19 
    20         dp[n]=0;
    21 
    22         for(int i=n-1;i>=1;i--)
    23         {
    24             double down=n;
    25             down*=(n-1);
    26             down/=2;
    27             double up=i;
    28             up*=(n-i);
    29             p1=up/down*p;
    30             dp[i]=dp[i+1]+1.0/p1;
    31         }
    32 
    33         printf("%.3lf
    ",dp[1]);
    34     }
    35 
    36     return 0;
    37 }
    [C++]
  • 相关阅读:
    Supreme(ง •̀_•́)ง
    基于VS快速排序的单元测试
    POST GET
    Go对比其他语言新特性1(字符类型、类型转换、运算符、键盘输入、for、switch)
    四则运算问题
    软件工程第三次作业!
    Servlet
    结对编程1
    Kafka技术原理知识点总结
    KafkaStream简介
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3219716.html
Copyright © 2011-2022 走看看