题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?

problemId=5381

Information Theory is one of the most popular courses in Marjar University. In this course, there is an important chapter about information entropy.

Entropy is the average amount of information contained in each message received. Here, a message stands for an event, or a sample or a character drawn from a distribution or a data stream.
Entropy thus characterizes our uncertainty about our source of information. The source is also characterized by the probability distribution of the samples drawn from it. The idea here is that the less likely an event is, the more information it provides when
it occurs.

Generally, "entropy" stands for "disorder" or uncertainty. The entropy we talk about here was introduced by Claude E. Shannon in his 1948 paper "A Mathematical Theory of Communication".
We also call it Shannon entropy or information entropy to distinguish from other occurrences of the term, which appears in various parts of physics in different forms.

Named after Boltzmann's H-theorem, Shannon defined the entropy Η (Greek letter Η, η) of a discrete random variable X with possible values {x1, x2,
..., xn}
 and probability mass function P(X) as:

H(X)=E(−ln(P(x)))

Here E is the expected value operator. When taken from a finite sample, the entropy can explicitly be written as

H(X)=−∑i=1nP(xi)log b(P(xi))

Where b is the base of the logarithm used. Common values of b are 2, Euler's number e, and 10. The unit of entropy is bit for b = 2, nat for b = e,
and dit (or digit) for b = 10 respectively.

In the case of P(xi) = 0 for some i, the value of the corresponding summand 0 logb(0) is taken to be a well-known limit:

0log b(0)=limp→0+plog b(p)

Your task is to calculate the entropy of a finite sample with N values.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first line contains an integer N (1 <= N <= 100) and a string S. The string S is one of "bit", "nat" or "dit", indicating the unit of entropy.

In the next line, there are N non-negative integers P1P2, .., PNPi means the probability
of the i-th value in percentage and the sum of Pi will be 100.

Output

For each test case, output the entropy in the corresponding unit.

Any solution with a relative or absolute error of at most 10-8 will be accepted.

Sample Input

3
3 bit
25 25 50
7 nat
1 2 4 8 16 32 37
10 dit
10 10 10 10 10 10 10 10 10 10

Sample Output

1.500000000000
1.480810832465
1.000000000000

Author: ZHOU, Yuchen

PS:2014年ACM/ICPC
亚洲区域赛牡丹江(第一站)现场赛

代码例如以下:

#include<cstdio>
#include<cmath>
#include <cstring>
const double e = exp(1.0);
double judge(char s[])
{
if(strcmp("bit",s) == 0)
return 2.0;
else if(strcmp("nat",s) == 0)
return e;
else if(strcmp("dit",s) == 0)
return 10.0; }
int main()
{
int t;
int n;
char s[7];
double p[117];
//printf("%lf\n",e);
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
scanf("%s",s);
double b = judge(s);
for(int i = 0; i < n; i++)
{
scanf("%lf",&p[i]);
p[i] /= 100.0;
}
double ans = 0;
double tt = log(b);
for(int i = 0; i < n; i++)
{
if(p[i] != 0)
ans+=p[i]*log(p[i])/tt;
else if(p[i] == 0)
{
ans+=0;
}
}
ans = -ans;
printf("%.12lf\n",ans);
}
return 0;
}

ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)的更多相关文章

  1. ZOJ 3827 Information Entropy (2014牡丹江区域赛)

    题目链接:ZOJ 3827 Information Entropy 依据题目的公式算吧,那个极限是0 AC代码: #include <stdio.h> #include <strin ...

  2. 2014 牡丹江现场赛 i题 (zoj 3827 Information Entropy)

    I - Information Entropy Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %l ...

  3. ZOJ 3827 Information Entropy 水题

    Information Entropy Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/sh ...

  4. ZOJ 3827 Information Entropy 水

    水 Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Informati ...

  5. zoj 3827 Information Entropy 【水题】

    Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Information ...

  6. ZOJ 3822 Domination(概率dp 牡丹江现场赛)

    题目链接:problemId=5376">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 Edward ...

  7. 【解题报告】牡丹江现场赛之ABDIK ZOJ 3819 3820 3822 3827 3829

    那天在机房做的同步赛,比现场赛要慢了一小时开始,直播那边已经可以看到榜了,所以上来就知道A和I是水题,当时机房电脑出了点问题,就慢了好几分钟,12分钟才A掉第一题... A.Average Score ...

  8. 2014ACMICPC亚洲区域赛牡丹江现场赛之旅

    下午就要坐卧铺赶回北京了.闲来无事.写个总结,给以后的自己看. 因为孔神要保研面试,所以仅仅有我们队里三个人上路. 我们是周五坐的十二点出发的卧铺,一路上不算无聊.恰巧邻床是北航的神犇.于是下午和北航 ...

  9. 2014ACM/ICPC亚洲区域赛牡丹江现场赛总结

    不知道怎样说起-- 感觉还没那个比赛的感觉呢?如今就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,由于要来比赛,所以那些事情队友的国奖不能答辩.自己的励志奖班里乱搞要投票,自己又不在,真是无 ...

随机推荐

  1. 21 调试我们的C#程序

    我们写的C#程序,很多的时候不是能一次就写对的.尤其是在实际的项目中,你经常要做的工作就是不断修改你写的程序,让它正常运行.程序有错误,主要是两种情况.一种是程序报错了,不能正常运行下去.另一种是程序 ...

  2. Java高级程序员(5年左右)面试的题目集

    Java高级程序员(5年左右)面试的题目集 https://blog.csdn.net/fangqun663775/article/details/73614850?utm_source=blogxg ...

  3. 由一维数组表示的N维数组实现(C++)

    工作中,经常需要表示多维数组(如二维矩阵),常见的做法是使用T **pArr: T **pArr = new T*[M];//创建二维数组[M][N] ;i<M;i++) { pArr[i] = ...

  4. css3 动画 vs js 动画

    之前被问到过,css3 动画与 js 动画孰优孰劣,脑袋的第一反应就是性能上肯定 css3 动画会好很多,但别人说不对,我就在想,不对?难道还有别的原因吗?答案是肯定的.先来看看二者实现动画的原理吧. ...

  5. php经典bug

    <?php $tem = 0=="a"?1:2; echo $tem; ?> 输出结果为:1 原因:在0=="a",这个比较中因为会将字符串&quo ...

  6. Learning Face Age Progression: A Pyramid Architecture of GANs

    前言 作为IP模式识别的CNN初始模型是作为单纯判别式-模式识别存在的,并以此为基本模型扩展到各个方向.基本功能为图像判别模型,此后基于Loc+CNN的检测模型-分离式.end2end.以及MaskC ...

  7. 世界上最受欢迎的10个Linux发行版

    帮助新的Linux用户在越来越多的Linux发行版中选择最合适的操作系统,是创建这个网页的原因.它列出了迄今为止最流行的10个Linux发行版(另外增加的是FreeBSD,到目前为止最为流行的BSD系 ...

  8. swift 工作日志

    开发问题汇总: tableview.register(CEImpWalletHomeCell.self, forCellReuseIdentifier: "cell") var c ...

  9. MySql(五)select排序查询

    举个栗子/**查询员工信息,要求工资按照从高到低进行排序(默认升序)**/SELECT * FROM employees ORDER BY salary ASC;/**方法2:**/SELECT * ...

  10. CAD得到当前选择的实体(网页版)

    主要用到函数说明: IMxDrawSelectionSet::CurrentSelect 得到当前已经选择的实体.详细说明如下: 参数 说明 filterData 过滤条件 js中使用如下: 1 2 ...