博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ZOJ3827 ACM-ICPC 2014 亚洲区域赛的比赛现场牡丹江I称号 Information Entropy 水的问题...
阅读量:6265 次
发布时间:2019-06-22

本文共 3180 字,大约阅读时间需要 10 分钟。

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)=limp0+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

33 bit25 25 507 nat1 2 4 8 16 32 3710 dit10 10 10 10 10 10 10 10 10 10

Sample Output

1.5000000000001.4808108324651.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
#include
#include
#include
#include
#include
#include
#include
#include
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

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

你可能感兴趣的文章
使用vscode写typescript(node.js环境)起手式
查看>>
飞天技术汇大视频专场:全民视频时代下的创新技术之路
查看>>
以太坊分片详解
查看>>
Redis安装以及PHP开启Redis扩展
查看>>
JAVA IO BIO NIO AIO
查看>>
使用iview的组件 Table 表格,有固定列,设置其中一个列适应屏幕大小
查看>>
Vue学习笔记1
查看>>
用户输入一个网址到页面展示内容的这段时间内,浏览器和服务器都发生了生么事情?...
查看>>
动手搞一个Promise
查看>>
[case32]alibaba限流组件Sentinel实战
查看>>
用python来给图片加水印
查看>>
【跃迁之路】【550天】程序员高效学习方法论探索系列(实验阶段307-2018.08.09)...
查看>>
link和@import的区别浅析
查看>>
vscode 相关
查看>>
nodejs 全自动使用 Tinypng (免费版,无需任何配置)压缩图片
查看>>
彻底理解Java中的基本数据类型转换(自动、强制、提升)
查看>>
在CentOS中安装redis5.0
查看>>
重构-改善既有代码的设计(六)--重新组织函数
查看>>
panic: time: missing Location in call to Time.In
查看>>
在K8S集群中一步步构建一个复杂的MySQL数据库
查看>>