打气球(记忆化dp+数学期望)

来源:互联网 发布:php httpclient 编辑:程序博客网 时间:2024/05/17 10:43

过于菜QAQ

#include <cstdio>#include <iostream>#include <iomanip>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>using namespace std;inline int R(){    char t=getchar();int o=0;bool F=0;    while(t<48||t>57)F|=t==45,t=getchar();    for(;t<58&&t>47;t=getchar())o=(o<<1)+(o<<3)+t-48;    return F?-o:o;}bool h[2010], s[2010];int n;double f[2010][2010];int m;double dp( int x, int y ){    if( f[x][y] != -1 ) return f[x][y];    if( ! x && ! y ) return f[x][y] = 0;    double ans = 0;    if( x ) ans += dp( x-1, y ) * x * ( n - y );    if( y ) ans += dp( x, y-1 ) * ( n - x ) * y;    if( x && y ) ans += dp( x-1, y-1 ) * x * y;    return f[x][y] = (double) ( ans + (double) n * n ) / ( (double) n * ( x + y ) - x * y ) ;}int main(){//  freopen("data4.in","r",stdin);//  freopen("shoot.out","w",stdout);    n = R(); m = R(); int a, b;    for( int i = 1; i <= m; i ++ ){        a = R(); b = R();        h[a] = 1; s[b] = 1;    }    int c = 0, r = 0;    for( int i = 1; i <= n; i ++ ){        if( ! h[i] ) c ++;        if( ! s[i] ) r ++;    }    for( int i = 0; i <= n; i ++ )        for( int j = 0; j <= n; j ++ )            f[i][j] = -1;    printf("%.2lf",dp(c,r));    return 0;}
原创粉丝点击