博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy
阅读量:5865 次
发布时间:2019-06-19

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

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

Author: 
ZHOU, Yuchen

Source: 
The 2014 ACM-ICPC Asia Mudanjiang Regional Contest

题目链接:

解题思路:又是一道签到题!!!当时想多了。竟然纠结在积分上,果真是想多了,根本不须要什么技术,就是依照那个数学公式算。又学到了个小方法。表示对数能够用log(m, n)= log(n) / log(m). 还有就是e = exp(1)。

还有就是注意当p = 0时,函数值为0。

AC代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define INF 0x7fffffff#define e exp(1.0)int main(){ #ifdef sxk freopen("in.txt","r",stdin); #endif int T, n, p; string s; scanf("%d",&T); while(T--) { double ans = 0; scanf("%d",&n); cin>>s; if(s == "bit"){ for(int i=0; i

转载地址:http://hejnx.baihongyu.com/

你可能感兴趣的文章
crmv2项目
查看>>
[Asp.net MVC]HandleErrorAttribute异常过滤器
查看>>
mvc
查看>>
Linux - 常见端口和服务的对照和解释
查看>>
数据库(MySQL)
查看>>
HDOJ_ACM_连连看
查看>>
力扣算法题—037解数独
查看>>
hadoop更换硬盘
查看>>
【快速排序】
查看>>
图文介绍如何在Eclipse统计代码行数
查看>>
Elementary Methods in Number Theory Exercise 1.4.30
查看>>
使用Asymptote的循环功能画出绿叶阵
查看>>
Local minimum and local maximum,second derivative
查看>>
flask的上下文管理
查看>>
msf、armitage
查看>>
ubuntu14.04 配置realvnc
查看>>
VS2010帮助文档所有安装出错的解决办法
查看>>
跟我一起学习ASP.NET 4.5 MVC4.0(五)(转)
查看>>
Java基础知识(二)
查看>>
Android 快捷方式相关操作
查看>>