balls
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=5570
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,1+ai,2+…+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 5)
For each cases, first come 2 integers, n,m(1≤n≤1000,1≤m≤1000)
Then follows n lines with m numbers ai,j(1≤ai≤100)
Output
For each cases, please output the answer with two decimal places.
Sample Input
2 2
1 1
3 5
2 2
4 5
4 2
2 2
2 4
1 4
Sample Output
3.00
2.96
3.20
HINT
题意
题解:
贡献为x^2,其实就是问你期望的对数有多少个
假设p[i][j]表示第一个位置出现颜色为j的的概率是多少,那么我们可以推出来该颜色期望的对数的公式是
ans = p[i][1] + p[i][2]+ … + p[i][m] + p[i][1]*p[i][2] + p[i][1]*p[i][3] + …. + p[i][2]*p[i][1]+ … + p[i][m]*p[i][m-1]
然后化简,就可以得到ans = sigma(p[i][k]) + sigma(p[i][k]) * sigma(p[i][k]) – sigma(p[i][k]*p[i][k])
就可以On来处理答案啦~
代码:
#include<iostream> #include<stdio.h> using namespace std; int n,m; double a[1001][1001]; double s[1001]; int main() { while(scanf("%d%d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) { s[i]=0; for(int j=1;j<=m;j++) { scanf("%lf",&a[i][j]); s[i]+=a[i][j]; } } double ans = 0; for(int j=1;j<=m;j++) { double s1 = 0; double s2 = 0; for(int i=1;i<=n;i++) { double p = a[i][j]/s[i]; s1+=p; s2+=p*p; } ans += s1 + s1*s1 - s2; } printf("%.2f\n",ans); } }