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[][];
double s[];
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
{
s[i]=;
for(int j=;j<=m;j++)
{
scanf("%lf",&a[i][j]);
s[i]+=a[i][j];
}
}
double ans = ;
for(int j=;j<=m;j++)
{
double s1 = ;
double s2 = ;
for(int i=;i<=n;i++)
{
double p = a[i][j]/s[i];
s1+=p;
s2+=p*p;
}
ans += s1 + s1*s1 - s2;
}
printf("%.2f\n",ans);
}
}

HDU 5570 balls 期望 数学的更多相关文章

  1. hdu 5570 balls(期望好题)

    Problem Description There are n balls with m colors. The possibility of that the color of the i-th b ...

  2. HDU 4611 Balls Rearrangement 数学

    Balls Rearrangement 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4611 Description Bob has N balls ...

  3. hdu 4710 Balls Rearrangement (数学思维)

    意甲冠军:那是,  从数0-n小球进入相应的i%a箱号.然后买一个新的盒子. 今天的总合伙人b一个盒子,Bob试图把球i%b箱号. 求复位的最小成本. 每次移动的花费为y - x ,即移动前后盒子编号 ...

  4. HDU 5810 Balls and Boxes(盒子与球)

     Balls and Boxes(盒子与球) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  5. HDU 5570:balls 期望。。。。。。。。。。。。。。。

    balls  Accepts: 19  Submissions: 55  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 65536/655 ...

  6. HDU 5810 Balls and Boxes 数学

    Balls and Boxes 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5810 Description Mr. Chopsticks is i ...

  7. hdu 3232 Crossing Rivers(期望 + 数学推导 + 分类讨论,水题不水)

    Problem Description   You live in a village but work in another village. You decided to follow the s ...

  8. HDU 4465 Candy (数学期望)

    题意:有两个盒子各有n个糖(n<=2*105),每天随机选1个(概率分别为p,1-p),然后吃掉一颗糖.直到有一天打开盒子一看,这个盒子没有糖了.输入n,p,求此时另一个盒子里糖的个数的数学期望 ...

  9. HDU 4336 Card Collector 数学期望(容斥原理)

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意简单,直接用容斥原理即可 AC代码: #include <iostream> ...

随机推荐

  1. CentOS SVN服务器安装配置小记

    SVN的安装 安装很简单,尤其对于CentOS这种,直接: # yum install subversion# yum install mod_dav_svn 不同发行版的Package安装方法参见h ...

  2. POJ 1160 Post Office

    题意:有n个村庄,要在其中m个村庄里建邮局,每个村庄去邮局的代价为当前村庄到最近的一个有邮局村庄的路程,问总最小代价是多少. 解法:dp.dp[i][j]表示在前j个村庄建立i个邮局后的代价,则状态转 ...

  3. Android WindowManager 监听返回键及home键

    一.监听home键盘,Android Home键系统负责监听,捕获后系统自动处理.有时候,我们需要监听home键处理自己的逻辑,监听方法如下: /** * 监听home键广播 */ private f ...

  4. android改变字体的颜色的三种方法

    写文字在Android模拟器中的方法 法一: main.xml配置文件: <TextView android:id="@+id/tv" android:layout_widt ...

  5. JS单例设计模式

     单例,指的是只有一个实例的对象.    在应用单例模式时,生成单例的类必须保证只有一个实例的存在,很多时候整个系统只需要拥有一个全局对象,才有利于协调系统整体的行为.比如在整个系统的配置文件中,配置 ...

  6. 如何在 Windows Azure 的虚拟机 ubuntu 上面安装和配置 openVPN(三)

    第三步:安装openVPN 请打开 ubuntu 官方文档.然后,从上到下,按照步骤,输入执行命令.大家只需要执行到 First trouble shooting 之前即可. 不要怕麻烦,仔细读,一步 ...

  7. python学习之dict的items(),values(),keys()

    Python的字典的items(), keys(), values()都返回一个list >>> dict = { 1 : 2, 'a' : 'b', 'hello' : 'worl ...

  8. python学习之self,cls,staticmethod,classmethod

    一.总体说明 python类里会出现这三个单词,self和cls都可以用别的单词代替,类的方法有三种, 一是通过def定义的 普通的一般的,需要至少传递一个参数,一般用self,这样的方法必须通过一个 ...

  9. 用MATLAB实现字符串分割

    strsplit更好用,用法: strsplit(strtrim(sprintf('  \t\nds   \nhs\t dssd    \t    \n'))) 以下转载 Matlab的字符串处理没有 ...

  10. JavaFx2.0中CSS的应用

    http://user.qzone.qq.com/773534839#!app=2&via=QZ.HashRefresh&pos=1326994508 ———————————————— ...