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. nginx源代码分析--event事件驱动初始化

    1.在nginx.c中设置每一个核心模块的index ngx_max_module = 0; for (i = 0; ngx_modules[i]; i++) { ngx_modules[i]-> ...

  2. BroadcastReceiver总结

    一.工程整体图 二.activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ ...

  3. PHP监測memcache服务端的执行状况

    . 代码例如以下,代码为memcache官方代码,引用在此.做一下简单的说明: 1.设置username和password define('ADMIN_USERNAME','admin'); // A ...

  4. 你属于几K(千)?

    月薪2k.对出勤率负责:5k,对按时完毕率负责:8k,对质量负责:12k.对小团队的情绪负责:16k,对到款率负责:20k,要想着怎么保住自己的饭碗.40k.保住部门.100k.保住大部门:100k+ ...

  5. Tkinter隐藏窗口再让他显示出来的例子

    隐藏主要是 : withdraw()函数. 重新显示出来主要是: update()和deiconify()函数. 来源:http://www.blog.pythonlibrary.org/2012/0 ...

  6. HDU 1253-大逃亡(裸-DBFS)

    G - 胜利大逃亡 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  7. OCP-1Z0-051-题目解析-第30题

    30. Evaluate the following CREATE TABLE commands: CREATE TABLE orders (ord_no NUMBER(2) CONSTRAINT o ...

  8. SQL Server Insert操作中的锁

    原文:SQL Server Insert操作中的锁 这篇博文简单介绍一下在SQL Server中一条Insert语句中用到的锁. 准备数据 首先我们建立一张表Table_1,它有两列Id(bigint ...

  9. 计算机本科/硕士找offer的总结 节选

    在这样的目标定位下,我的求职历程从2006年10月8日开始,到2007年1月10日正式结束,一共持续了3个月的时间.整个过程可以分为三个阶段:第一阶段:2006年10月份 找工作刚刚开始的阶段,什么都 ...

  10. tar解压出错

    现象 # tar -zxvf aaa.tar.gz tar: This does not look like a tar archive tar: Skipping to next header ta ...