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. 元素堆叠问题、z-index、position

    多次在项目中遇到html页面元素的非期待重叠错误,多数还是position定位情况下z-index的问题.其实每次解决类似问题思路大致都是一样的,说到底还是对z-index的理解比较模糊,可以解决问题 ...

  2. 利用split

    java.lang.string.splitsplit 方法将一个字符串分割为子字符串,然后将结果作为字符串数组返回.stringObj.split([separator,[limit]])strin ...

  3. log4cplus 直接创建logger 对象

    #include <log4cplus/loggingmacros.h> #include <log4cplus/fileappender.h> #include <lo ...

  4. 深入理解FTP协议

    文件传输协议FTP(File Transfer Protocol)是因特网中使用最广泛的文件传输协议.FTP使用交互式的访问,允许客户指定文件的类型和格式(如指明是否使用ASCII码),并允许文件具有 ...

  5. 你足够了解Context吗?

    你足够了解Context吗? 这里有关于Context的一切-写在前面: 当我还是一个24K纯Android新手的时候(现在是也是个小Android萌新),拿着工具书对着电脑敲敲打打,那个时候我就有一 ...

  6. 01 if

    if - else     if语句是一种控制语句,执行一代码块,如果一个表达式计算为true if (expression)     statement1 else   statement2   如 ...

  7. onclick用法 超链接简单弹出窗口实例

    问题 需要异步处理不同状态 1. onclick用法 if判断弹出窗口 解题思路 1. onclick用法 if判断弹出窗口 注意.. <a href="javascript:void ...

  8. caffe源码分析 vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom

    Blob:4个维度 n x c x h x w: bottom[0] .bottom[1]代表该层有几个输入. bottom[0]->count(): 输入中,元素的总维数(个数) bottom ...

  9. 浅析nodeJS中的Crypto模块,包括hash算法,HMAC算法,加密算法知识,SSL协议

    node.js的crypto在0.8版本,这个模块的主要功能是加密解密. node利用 OpenSSL库(https://www.openssl.org/source/)来实现它的加密技术, 这是因为 ...

  10. 怎样调整XenServer下面Linux虚拟机的磁盘大小

    登录到XenServer. 修改虚拟机磁盘大小修改storage 磁盘大小 启动虚拟机 修改分区大小Hex code (type L to list codes): 8eChanged system ...