数学期望与概率dp入门

SPOJ Favorite Dice(数学期望)

#include 
#include 
#include 
using namespace std;
double dp[1010];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        memset(dp,0,sizeof(dp));
        int n;
        scanf("%d",&n);
        for(int i = n-1;i>=0;i--){
            dp[i] = dp[i+1] + (double)n/(n-i);
        }
        printf("%.2f",dp[0]);
        if(t!=0) printf("\n");
    }
    return 0;
}

[ZOJ3640]Help Me Escape(概率期望dp)

#include 
#include 
#include 
#include 
using namespace std;
const int MAXN = 100010;
const double p = ((1.0+sqrt(5.0))*0.5);
double dp[2*MAXN];
double t[MAXN];
int c[MAXN];
void cclear(){
    memset(dp,0,sizeof(dp));
    memset(t,0,sizeof(t));
}
int main(){
    int n,f;
    while(~scanf("%d%d",&n,&f)){
        cclear();
        int maxc = -1;
        for(int i = 1;i<=n;++i){
            scanf("%d",&c[i]);
            t[i] = (double)floor(p*c[i]*c[i]);
            maxc = max(maxc,c[i]);
        }
        for(int i = maxc*2;i>=f;--i){
            for(int j = 1;j<=n;++j){
                if(i>c[j])  dp[i] += t[j]/n;
                else        dp[i] += (dp[i+c[j]]+1.0)/n;
            }
        }
        cout<

你可能感兴趣的:(数学期望与概率dp入门)