首页 > 代码库 > LightOJ 1248 Dice (III)

LightOJ 1248 Dice (III)

期望,$dp$。

设$dp[i]$表示当前已经出现过$i$个数字的期望次数。在这种状态下,如果再投一次,会出现两种可能,即出现了$i+1$个数字以及还是$i$个数字。

因此 $dp[i]=dp[i]*i/n+dp[i+1]*(n-i)/n+1$,即$dp[i]=dp[i+1]+n/(n-i)$,$dp[n]=0$,推出$dp[0]$即可。

#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 T,n;double dp[100010];int main(){    scanf("%d",&T); int cas=1;    while(T--)    {        scanf("%d",&n);        dp[n]=0;        for(int i=n-1;i>=0;i--)        {            dp[i]=dp[i+1]+1.0*n/(n-i);        }        printf("Case %d: %lf\n",cas++,dp[0]);    }    return 0;}

 

LightOJ 1248 Dice (III)