首页 > 代码库 > POJ 2151 Check the difficulty of problems

POJ 2151 Check the difficulty of problems

概率,$dp$。

人是独立的,可以先分别计算出每个人在$m$题中做出$0$题、$1$题......$m$题的概率。这个dp推一下就可以算出来了。

设$dp[i][j][k]$表示第$i$个人在前$j$题中,做出$k$题的概率。$dp[i][j][k]=p[i][j]*dp[i][j-1][k-1]+(1-p[i][j])*dp[i][j-1][k]$。

如果所有人都做出题目的概率是$P1$;所有人都做出题,且每个人的题数均在$1$至$n-1$题之间的概率为$P2$;那么答案就是$P1-P2$,而$P1$,$P2$根据$dp$的结果直接可以算得。

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<iostream>using namespace std;typedef long long LL;const double pi=acos(-1.0),eps=1e-6;void File(){    freopen("D:\\in.txt","r",stdin);    freopen("D:\\out.txt","w",stdout);}template <class T>inline void read(T &x){    char c = getchar();    x = 0;    while(!isdigit(c)) c = getchar();    while(isdigit(c))    {        x = x * 10 + c - 0;        c = getchar();    }}int m,t,n;double p[1005][35],dp[1005][35][35];int main(){    while(~scanf("%d%d%d",&m,&t,&n))    {        if(m==0&&t==0&&n==0) break;        for(int i=1;i<=t;i++)            for(int j=1;j<=m;j++) scanf("%lf",&p[i][j]);        memset(dp,0,sizeof dp);        for(int i=1;i<=t;i++)        {            dp[i][0][0]=1;            for(int j=1;j<=m;j++) dp[i][j][0]=dp[i][j-1][0]*(1-p[i][j]);            for(int j=1;j<=m;j++)                for(int k=1;k<=j;k++)                    dp[i][j][k]=p[i][j]*dp[i][j-1][k-1]+(1-p[i][j])*dp[i][j-1][k];          //  for(int j=0;j<=m;j++) printf("%lf  ",dp[i][m][j]);          //  printf("\n");        }        double p1=1,p2=1;        for(int i=1;i<=t;i++)        {            double sum=0;            for(int j=1;j<n;j++) sum=sum+dp[i][m][j];            p2=p2*sum;            for(int j=n;j<=m;j++) sum=sum+dp[i][m][j];            p1=p1*sum;        }        printf("%.3f\n",p1-p2);    }    return 0;}

 

POJ 2151 Check the difficulty of problems