概率计算- 组合 计数

九度:

http://ac.jobdu.com/problem.php?pid=1421

求总的期望值。可以单个计算每个人,最后相加。


As we all known, AbOr has a lots of girls in a palace called “Hou” (About 30 millions).

Now you are given n people without knowing their gender(Male and Female are equally likely for anyone). You also know the relationship between them! You want to know what is the expecting number of "abor" that could be found , where "abor" is defined as:

 (1) Only Male might be considered;

(2) The Male who has at least m Females friends is called "abor".

输入:

        The first line is one integer T indicates the number of the test cases. (T <= 10000)

Then for every case, the first line has only two integers n and m, indicating the number of people and the value of m.(0≤m≤n≤20)

        Then one n by n symmetric matrix A, where A[i][j] is 1 if j is a friend of i(vice versa) and 0 otherwise;(A[i][i] is always 0)

输出:

        Output the expecting number of "abor" in the given case. The answer must round to two digital after the decimal point.

样例输入:
32 101102 001103 2011101110
样例输出:
0.501.000.38

#include<cstdio>
#include<iostream>
using namespace std;
const int MaxN=25;

int N,M;
char s[MaxN][MaxN];
int C[MaxN][MaxN];
int pow2[MaxN];

int main()
{
	freopen("in.txt", "r", stdin);
    for (int i=0;i<=20;++i)
        C[i][0]=1;
    for (int i=1;i<=20;++i)
        for (int j=1;j<=i;++j)
            C[i][j]=C[i-1][j-1]+C[i-1][j];
    for (int i=pow2[0]=1;i<=20;++i)
        pow2[i]=pow2[i-1]*2;
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&N,&M);
        for (int i=1;i<=N;++i)
            scanf("%s",s[i]+1);
        long double ans=0;
        for (int i=1;i<=N;++i)
        {
            int p=0;
            for (int j=1;j<=N;++j)
                p+=s[i][j]-48;
            int v=0;
            for (int j=M;j<=p;++j)
                v+=C[p][j]; //v为
            ans+=(long double)v/(long double)pow2[p];
        }
        printf("%.2lf\n",(double)(ans/2));
    }
    return 0;
}






你可能感兴趣的:(概率计算- 组合 计数)