ACM选修hust 1075 组合+数学+期望值

Description

Input

Output

Sample Input

2
2 1
0 1
1 0
3 1
0 1 1
1 0 1
1 1 0

Sample Output

0.500
1.125

题意:给出N个node,当节点为女性,且认识K个男性时,该节点就满足条件,求出所有情况满足条件的节点的期望值
想法:由于输入矩阵i X j来表示i是否认识j。故可以记录每个i认识的个数。枚举每个i,再进行排列组合计算,满足条件的个数计入count,计算期望值时除以所有情况(2^n)。
(最开始愚蠢的我写了2的幂指函数,及非打表方式求组合函数,结果没有确认精度损失的问题WA无数次……捂脸)
 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <string.h>
 5 using namespace std;
 6 
 7 
 8 
 9 int a[35][35], b[35];
10 double c[33][33];
11 int main()
12 {
13     //freopen("data.in", "r", stdin);
14     //freopen("data.out", "w", stdout);
15     int T, N, K,x, y, count;
16     double  ans, num;
17 
18     c[0][0]=1;
19     for(int i = 1; i <= 30; i++)
20     {
21         c[i][0] = c[i][i]=1;
22         for(int j = 1; j < i; j++)
23         {
24                 c[i][j] = c[i-1][j-1] + c[i-1][j];
25         }
26     }
27     scanf("%d", &T);
28 
29         while(T--)
30         {
31              scanf("%d%d",&N, &K);
32 
33             memset(b, 0, sizeof(b));
34 
35 
36             for(int i=0; i<N; i++)
37             {
38                 for(int j=0; j<N; j++)
39                 {
40                     scanf("%d", &a[i][j]);
41                         b[i]+=a[i][j];
42                 }
43             }
44             //printf("%d",b[4]);
45             ans=0;
46             for(int i = 0; i < N; i++)
47             {
48                 if(b[i]>=K)
49                 {
50                     num=0;
51                     for(int j = K; j <= b[i]; j++)
52                     {
53                         num+=c[b[i]][j];
54                     }
55                     for(int j = 1; j <= b[i] + 1; j++)
56                         num/=2.00;
57 
58                     ans+=num;
59                 }
60             }
61             printf("%.3lf
", ans);
62         }
63         return 0;
64 
65 
66     //fclose(stdin);
67     //fclose(stdout);
68 }
 
原文地址:https://www.cnblogs.com/Yumesenya/p/5347432.html