hdu 3625 Examining the Rooms //组合数学DP

做的十分顺利,1Y


#include <cstdio>
#include <cstring>
long long dp[21][21];
double ans[21][21];
double out[21][21];
long long sum = 0;
int main()
{
    memset(dp, 0, sizeof(dp));
    memset(ans, 0, sizeof(ans));
    memset(out, 0, sizeof(out));
    dp[1][1] = 1;
    sum = 1;
    for(int i = 2; i <= 20; i++)
    {
        dp[i][i] = 1;
        sum = sum * i;
        for(int j = 1; j < i; j++)
        {
            dp[i][j] = (i - 1) * dp[i-1][j] + dp[i-1][j-1];
            ans[i][j] = (1.0 * ((i - 1) * dp[i-1][j])) / sum;//第i个为1,只能加入别人,不能自己成环
        }
    }

    for(int i = 2; i <= 20; i++)
    {
        //out[i][1] = ans[i][1];
        //if(i == 3) printf("%0.4lf\n", out[i][1]);
        for(int j = 1; j < i; j++)
        {
            out[i][j] = ans[i][j];
            out[i][j] += out[i][j-1];
        }
    }

    int T;
    scanf("%d", &T);
    int n, k;
    while(T--)
    {
        scanf("%d%d", &n, &k);
        printf("%0.4lf\n", out[n][k]);
    }
    return 0;
}


你可能感兴趣的:(hdu 3625 Examining the Rooms //组合数学DP)