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

题目链接:

problemId=5381">Information Entropy

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

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

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
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<n; i++){
scanf("%d", &p);
if(p)
ans += -(p/100.0*log(p/100.0)/log(2));
}
}
else if(s == "nat"){
for(int i=0; i<n; i++){
scanf("%d", &p);
if(p)
ans += -(p/100.0*log(p/100.0)/log(e));
}
}
else{
for(int i=0; i<n; i++){
scanf("%d", &p);
if(p)
ans += -(p/100.0*log(p/100.0)/log(10));
}
}
printf("%.14lf\n",ans);
}
return 0;
}

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy的更多相关文章

  1. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  2. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation

    Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation ...

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

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

  4. 2014ACM/ICPC亚洲区域赛牡丹江站汇总

    球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...

  5. 2014ACM/ICPC亚洲区域赛牡丹江现场赛总结

    不知道怎样说起-- 感觉还没那个比赛的感觉呢?如今就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,由于要来比赛,所以那些事情队友的国奖不能答辩.自己的励志奖班里乱搞要投票,自己又不在,真是无 ...

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

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

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

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

  8. 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)

    队名:Unlimited Code Works(无尽编码)  队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...

  9. 2013ACM/ICPC亚洲区南京站现场赛---Poor Warehouse Keeper(贪心)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4803 Problem Description Jenny is a warehouse keeper. ...

随机推荐

  1. PHP基于phpqrcode类生成二维码的方法详解

    前期准备: 1.phpqrcode类文件下载,下载地址:https://sourceforge.net/projects/phpqrcode/2.PHP环境必须开启支持GD2扩展库支持(一般情况下都是 ...

  2. svn基本使用详情

    1.增加(Add)先提到变更列表中,再commit到配置库中,选择新增文件,右键SVN菜单执行“Add“操作提交到”变更列表中”,然后右键SVN菜单执行”SVNCommit”提交到版本库中. 2.删除 ...

  3. RabbitMQ 关键词解释

    源地址: https://www.cnblogs.com/hz04022016/p/6518138.html RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQ ...

  4. python_time和datetime模块

    time和datatime模块 时间相关的操作,时间有三种表示方式: 时间戳               1970年1月1日之后的秒,即:time.time() 格式化的字符串    2019-1-1 ...

  5. [Android]使用button切换TextView可见和不可见

    <Button android:id="@+id/btnTest" android:layout_width="match_parent" android ...

  6. C# easyui json类

    using System; using System.Data; using System.Text; namespace Common { public class JsonHelp { priva ...

  7. hdu 1043 A*

    http://www.cnblogs.com/183zyz/archive/2011/08/12/2135827.html #include<stdio.h> #define N 3630 ...

  8. 安卓巴士Android开发神贴整理

    10个经典的Android开源应用项目 http://www.apkbus.com/android-13519-1-1.html 安卓巴士总结了近百个Android优秀开源项目,覆盖Android开发 ...

  9. [Usaco2006 Nov]Bad Hair Day 乱发节

    Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 1268  Solved: 625[Submit][Status][Discuss] Description ...

  10. list如何remove

    http://blog.sina.com.cn/s/blog_621b6f0e0100s5n5.html 在java中对list进行操作很频繁,特别是进行list启遍历,这些操作我们都会,也很熟悉,但 ...