Information Entropy


Time Limit: 2 Seconds      Memory Limit: 65536 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 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

Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> using namespace std; double p[200]; double xxx(int kind,double x)
{
if(kind==1) return log(x);
else if(kind==2) return log2(x);
else return log10(x);
} int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
int n; char op[20];
scanf("%d%s",&n,op);
double ans=0.0;
int kind = 1;
if(op[0]=='b') kind=2;
else if(op[0]=='d') kind=3;
for(int i=0;i<n;i++)
{
scanf("%lf",p+i);
if(p[i]==0) continue;
p[i]/=100.;
ans+=-1*p[i]*xxx(kind,p[i]);
}
printf("%.10lf\n",ans);
}
return 0;
}

ZOJ 3827 Information Entropy 水的更多相关文章

  1. ZOJ 3827 Information Entropy 水题

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

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

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

  3. zoj 3827 Information Entropy 【水题】

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

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

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

  5. ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5381 Information Theory is one of t ...

  6. ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题

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

  7. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy

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

  8. 通俗易懂的信息熵与信息增益(IE, Information Entropy; IG, Information Gain)

    信息熵与信息增益(IE, Information Entropy; IG, Information Gain) 信息增益是机器学习中特征选择的关键指标,而学习信息增益前,需要先了解信息熵和条件熵这两个 ...

  9. information entropy as a measure of the uncertainty in a message while essentially inventing the field of information theory

    https://en.wikipedia.org/wiki/Claude_Shannon In 1948, the promised memorandum appeared as "A Ma ...

随机推荐

  1. Android动态设置纯色图标的颜色

    https://blog.csdn.net/qq_20082961/article/details/73718363 以前做了一个流量悬浮窗,悬浮窗里有当前网络状态的图标和网速的文字,想实现改变文字颜 ...

  2. Ubuntu 里面 apt-get 三个有关更新的命令的区别

    apt-get update 更新软件源中的所有软件列表. apt-get upgrade 更新软件. apt-get dist-upgrade 更新系统版本. 作者:耑新新,发布于  博客园 转载请 ...

  3. hdu 1875 给出每个结点的坐标 权值为两点间的距离 (MST)

    Sample Input2210 10 //坐标20 2031 12 21000 1000 Sample Output1414.2   //最小权值和*100  保留1位小数oh!       //不 ...

  4. [转] 基于NodeJS的前后端分离的思考与实践(五)多终端适配

    前言 近年来各站点基于 Web 的多终端适配进行得如火如荼,行业间也发展出依赖各种技术的解决方案.有如基于浏览器原生 CSS3 Media Query 的响应式设计.基于云端智能重排的「云适配」方案等 ...

  5. mysql_union操作符

    MySQL  UNION操作符 union操作符合并两个或多个 SELECT 语句的结果集. union:去重 union all:不去重 UNION 结果集中的列名总是等于 UNION 中第一个 S ...

  6. 浮点数在计算机中的表示(IEEE浮点数标准)

    转载自:https://wdxtub.com/2016/04/16/thin-csapp-1/

  7. PopupWindow分享页面

    效果图 步骤: 1.布局中添加分享按钮 2.画出分享页面 3.设置分享页面animator进出动画,并在style.xml中配置 4.MainActivity中添加方法 *画出布局 主页面: < ...

  8. C# 创建多级文件夹示例

    string str = ""; ; i < j; i++) { str = str + "\\新建文件夹" ; } System.IO.Director ...

  9. 8.6 正睿暑期集训营 Day3

    目录 2018.8.6 正睿暑期集训营 Day3 A 亵渎(DP) B 绕口令(KMP) C 最远点(LCT) 考试代码 A B C 2018.8.6 正睿暑期集训营 Day3 时间:5h(实际) 期 ...

  10. 20172302《程序设计与数据结构》实验四Android程序设计实验报告

    课程:<程序设计与数据结构> 班级: 1723 姓名: 侯泽洋 学号:20172302 实验教师:王志强老师 实验日期:2018年5月30日 必修/选修: 必修 1.实验内容 (1)And ...