Sgu495: probability DP/push Formula

Source: Internet
Author: User

Probability question .. You can use either DP or formula.

Abstract questions:

There are n small balls, and the expected number of retrieved small balls is retrieved m times.

DP maintains the two States. The first retrieved is the probability of a ball that has not been taken out. DP [I] and the probability of a ball that has been taken out is NP [I];

If the second I-1 is taken out of the ball has been taken out, then the probability that the second I was not taken out of the ball is DP [I-1];

On the contrary, it is DP [I-1]-1/N (the ball that has not been taken out is missing one)

So we can obtain the state transition equation DP [I] = DP [I-1] * (DP [I-1]-1/n) + NP [I-1] * DP [I-1];

You can also push the formula .. However, I still think that the formula is based on the character. It is of course excellent to be YY-ready...

Code:

 

#include <iostream>#include <stdio.h>#include<string.h>#include<algorithm>#include<string>#include<math.h>#include<ctype.h>using namespace std;#define MAXN 10000int n,m;double dp[100010];double np[100010];double solve(){    double res=0;    memset(dp,0,sizeof(dp));    memset(np,0,sizeof(np));    dp[1]=1;    np[1]=0;    for(int i=2;i<=m;i++)    {        dp[i]=dp[i-1]*(dp[i-1]-1.0/(double)n)+np[i-1]*dp[i-1];        np[i]=1-dp[i];    }    for(int i=1;i<=m;i++)    {        res+=dp[i];    }    return res;}int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        printf("%.10lf\n",solve());    }    return 0;}


Formula ..

#include <stdio.h>#include<math.h>double n,m;int main(){    while(scanf("%lf%lf",&n,&m)!=EOF)    {        printf("%.10lf\n",n-n*pow(((n-1)/n),m));    }    return 0;}

 

Sgu495: probability DP/push Formula

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.