Information Entropy


Time Limit: 2 Seconds      Memory Limit: 131072 KB      Special Judge


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 functionP(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

    牡丹江赛区的第二道签到题。难度不大,基本A题通过后全部人I题也開始非常快通过,题目看起来非常难理解,可是一旦看懂事实上发现这事实上就是一道英文题。题目就是给一堆序列。然后给n个数a1,a2...an。然后对每一个数字求出(ai/sum)*log(ai/sum)的总和,当bit是,log以2为底,当nat时,log以e为底,当dit时。log以10为底,之后计算证明当ai=0时,对结果无影响,所以能够直接忽略,然后直接带入计算就可以AC此题~~~详细AC代码例如以下:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int a[105];
int main()
{
// freopen("in.txt","r",stdin);
int t;
cin>>t;
while(t--)
{
int n;
string s;
scanf("%d",&n);
cin>>s;
int sum=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
double res=0;
if(s=="bit")
{
for(int i=0;i<n;i++)
{
if(a[i]==0)
continue;
double p1=-log2(a[i]*1.0/sum)*a[i]/sum;
res+=p1;
}
}
else if(s=="nat")
{
for(int i=0;i<n;i++)
{
if(a[i]==0)
continue;
double p1=-log(a[i]*1.0/sum)*a[i]/sum;
res+=p1;
}
}
else
{
for(int i=0;i<n;i++)
{
if(a[i]==0)
continue;
double p1=-log10(a[i]*1.0/sum)*a[i]/sum;
res+=p1;
}
}
printf("%.12f\n",res);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题的更多相关文章

  1. ZOJ3819 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江司A称号 Average Score 注册标题

    Average Score Time Limit: 2 Seconds      Memory Limit: 131072 KB Bob is a freshman in Marjar Univers ...

  2. 2014年亚洲区域赛北京赛区现场赛A,D,H,I,K题解(hdu5112,5115,5119,5220,5122)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 下午在HDU上打了一下今年北京区域赛的重现,过了5题,看来单挑只能拿拿铜牌,呜呜. ...

  3. [hdu5136]Yue Fei's Battle 2014 亚洲区域赛广州赛区J题(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 现场赛的时候由于有个地方有点小问题,没有成功AC,导致与金牌失之交臂. 由于今天下 ...

  4. 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)-L Bit Sequence

    题意 给你两个数l,m,大小为m的数组a,求[0,l]之间满足以下条件的数x的个数: 对于任何i输入[0,m-1],f(x+i)%2=a[i]:f(k):代表k在二进制下1的个数 m的范围<=1 ...

  5. 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)

    队名:Unlimited Code Works(无尽编码)  队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...

  6. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  7. 2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem

    2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem 题意: 给定一个长度为\(n\)的序列,有两种操作: 1:单点修改. 2:查询区间\([L,R]\)范围内所有子 ...

  8. ICPC 2018 亚洲横滨赛 C Emergency Evacuation(暴力,贪心)

    ICPC 2018 亚洲横滨赛 C Emergency Evacuation 题目大意 你一个车厢和一些人,这些人都坐在座位上,求这些人全部出去的时间最小值 Solution 题目咋说就咋做 直接模拟 ...

  9. 2014ACM/ICPC亚洲区域赛牡丹江站汇总

    球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...

随机推荐

  1. HDU 5107 线段树扫描线

    给出N个点(x,y).每一个点有一个高度h 给出M次询问.问在(x,y)范围内第k小的高度是多少,没有输出-1 (k<=10) 线段树扫描线 首先离散化Y坐标,以Y坐标建立线段树 对全部的点和询 ...

  2. 使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(二)——自定义安装

    原文:使用WiX Toolset创建.NET程序发布Bootstrapper(安装策略管理)(二)--自定义安装 自定义产品卸载方式 继续从上一次的基础上前进,现在我们已经知道了最简单的bootstr ...

  3. Java的byte数组

    今天我们讨论一下Java的byte数组.byte数组就是字节数组,每一位存一个字节.看下面程序,猜猜输出多少? package com.wjy.bytes; import java.io.File; ...

  4. HDU 1226 超级密码 (搜素)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1226 题意简单,本来是一道很简单的搜素题目. 但是有两个bug: 1.M个整数可能有重复的. 2.N可 ...

  5. 静态网页VS动态网页

    干<沙朗新闻发布系统>什么时候.我们已经建立的网页.html的,并且.aspx,第一次接触.我认为这些东西是一样的,因为它,在观看ASP.NET视频的时候.听见里面讲课的老师有提到了这两样 ...

  6. Java调用IIS发布的WebService

    之前的一篇博客说了一个实例,就是用VS2005在IIS上发布WebService.今天我们来实现在Eclipse上用Java来调用昨天发布的WebService. 首先咋在浏览器中输入http://1 ...

  7. extjs_11_mvc模式

    1.非mvc模式 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWRhbV93enM=/font/5a6L5L2T/fontsize/400/fill/I ...

  8. linux管理员

    sudo password 添加管理员用户,设置其密码. exit 退出管理员.

  9. JS创建 trim() 方法,此方法在IE7、IE8中不存在 需要自定义

    function trim(str){ //创建空格对象 var space = new String(" "); /* str = trimLeft(str,space); st ...

  10. 九度 题目1044:Pre-Post

    转载请注明本文链接http://blog.csdn.net/yangnanhai93/article/details/40658571 题目链接:pid=1044">http://ac ...