hdu 5570 balls(期望好题)

时间:2021-02-23 05:46:13
Problem Description
There are n balls with m colors. The possibility of that the color of the i-th ball is color j is ai,jai,+ai,+...+ai,m. If the number of balls with the j-th is x, then you should pay x2 as the cost. Please calculate the expectation of the cost.
Input
Several test cases(about )

For each cases, first come  integers, n,m(≤n≤,≤m≤)

Then follows n lines with m numbers ai,j(≤ai≤)
 
Output
For each cases, please output the answer with two decimal places.
 
Sample Input

Sample Output
3.00
2.96
3.20
Source

附上中文题目:

hdu 5570 balls(期望好题)

附上官方题解:

hdu 5570 balls(期望好题)

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1006
#define inf 1e12
int n,m;
double a[N][N];
double p[N][N];
int main()
{
while(scanf("%d%d",&n,&m)==){
for(int i=;i<n;i++){
double sum=;
for(int j=;j<m;j++){
scanf("%lf",&a[i][j]);
sum+=a[i][j];
}
for(int j=;j<m;j++){
p[i][j]=a[i][j]*1.0/sum;
}
}
double ans=;
for(int j=;j<m;j++){
double sum=;
for(int i=;i<n;i++){
ans+=p[i][j]*(1.0-p[i][j]);
}
for(int i=;i<n;i++){
sum+=p[i][j];
}
ans+=sum*sum;
}
printf("%.2lf\n",ans);
}
return ;
}
hdu 5570 balls(期望好题)