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 variableX with possible values
{x1, x2, ..., xn} and probability mass functionP(X) as:

rev=2.5.3" alt="" style="height:21px; width:6px; vertical-align:-5px; margin-right:0.09em">

rev=2.5.3" alt="" style="height:5px; width:15px; vertical-align:3px; margin-right:0.05em">

rev=2.5.3" alt="" style="height:14px; width:15px; margin-right:-0.02em">

rev=2.5.3" alt="" style="height:21px; width:7px; vertical-align:-5px; margin-right:0.05em">

rev=2.5.3" alt="" style="height:14px; width:6px; margin-right:0.01em">

rev=2.5.3" alt="" style="height:21px; width:6px; vertical-align:-5px; margin-right:0.09em">

rev=2.5.3" alt="" style="height:21px; width:6px; vertical-align:-5px; margin-right:0.09em">

Here E is the expected value operator. When taken from a finite sample, the entropy can explicitly be written as

rev=2.5.3" width="7" height="21" alt="" style="height:21px; width:7px; vertical-align:-5px; margin-right:0.05em">

rev=2.5.3" alt="" style="height:5px; width:15px; vertical-align:3px; margin-right:0.05em">

rev=2.5.3" alt="" style="height:9px; width:6px; margin-right:0.07em">

rev=2.5.3" alt="" style="height:14px; width:6px; margin-right:0.01em">

rev=2.5.3" width="7" height="21" alt="" style="height:21px; width:7px; vertical-align:-5px; margin-right:0.05em">

rev=2.5.3" width="6" height="21" alt="" style="height:21px; width:6px; vertical-align:-5px; margin-right:0.09em">

Where b is the base of the logarithm used. Common values of b are 2, Euler's numbere, 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:

rev=2.5.3" alt="" style="height:13px; width:10px; vertical-align:-4px; margin-right:0.01em">

rev=2.5.3" alt="" style="height:14px; width:9px; margin-right:0.04em">

rev=2.5.3" width="15" height="5" alt="" style="height:5px; width:15px; vertical-align:3px; margin-right:0.05em">

rev=2.5.3" width="6" height="14" alt="" style="height:14px; width:6px; margin-right:0.01em">

rev=2.5.3" alt="" style="height:9px; width:7px; margin-right:0.04em">

rev=2.5.3" alt="" style="height:13px; width:10px; vertical-align:-4px; margin-right:0.01em">

rev=2.5.3" alt="" style="height:1px; width:1px; margin-right:0.24em">

rev=2.5.3" alt="" style="height:9px; width:6px; margin-right:0em">

rev=2.5.3" width="7" height="21" alt="" style="height:21px; width:7px; vertical-align:-5px; margin-right:0.05em">

rev=2.5.3" alt="" style="height:13px; width:11px; vertical-align:-4px; margin-left:-0.03em; margin-right:0em">

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 stringS. 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 P1,P2, ..,
PN. Pi means the probability of thei-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
题意:给你N个数和一个字符串str。

若str为bit。则计算sigma( - log2a[i])(1 <= i <= N);            str为nat时,计算sigma(- loga[i])(1 <= i <= N);           str为dit时,计算sigma(- log10a[i])(1 <= i <= N)。


AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#define LL long long
#define INF 0x3f3f3f3f
#define MAXN 1000
#define MAXM 100000
using namespace std;
int main()
{
int t;
int N;
char str[10];
double a[110];
scanf("%d", &t);
while(t--)
{
scanf("%d%s", &N, str);
double sum = 0;
for(int i = 0; i < N; i++)
scanf("%lf", &a[i]), sum += a[i];
double ans = 0;
if(strcmp(str, "bit") == 0)
{
for(int i = 0; i < N; i++)
{
if(a[i] == 0) continue;
ans += -log2(a[i] / sum) * (a[i] / sum);
}
}
else if(strcmp(str, "nat") == 0)
{
for(int i = 0; i < N; i++)
{
if(a[i] == 0) continue;
ans += -log(a[i] / sum) * (a[i] / sum);
}
}
else
{
for(int i = 0; i < N; i++)
{
if(a[i] == 0) continue;
ans += -log10(a[i] / sum) * (a[i] / sum);
}
}
printf("%.12lf\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 水

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

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

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

  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. [ACM] ZOJ 3819 Average Score (水题)

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

  8. ZOJ 2679 Old Bill ||ZOJ 2952 Find All M^N Please 两题水题

    2679:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1679 2952:http://acm.zju.edu.cn/onli ...

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

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

随机推荐

  1. (33)C#正则表达式

    正则表达式:专门用于字符串处理的语言,用来描述字符串特征的表达式 元字符 . 之间可以出现任意单个字符(除了\n 换行) 例如: a.b   意思是这个表达式必须是三个字符,第一个字符是a,第三个字符 ...

  2. (4)C#工具箱-菜单和工具栏

    1.ContextMenuStrip(右键菜单栏) 把contextMenuStrip控件拖到窗体上,会在窗体下面显示,点击控件在最上行显示菜单栏,可以任意设置.(运行以后不会在界面上显示,它需要预控 ...

  3. Python的工具包[1] -> pandas数据预处理 -> pandas 库及使用总结

    pandas数据预处理 / pandas data pre-processing 目录 关于 pandas pandas 库 pandas 基本操作 pandas 计算 pandas 的 Series ...

  4. TCC分布式事务的实现原理(转载 石杉的架构笔记)

    拜托,面试请不要再问我TCC分布式事务的实现原理![石杉的架构笔记] 原创: 中华石杉 目录 一.写在前面 二.业务场景介绍 三.进一步思考 四.落地实现TCC分布式事务 (1)TCC实现阶段一:Tr ...

  5. vue编程中,需要注意的

    同名情况: data() 中的数据名   和  methods()  中的方法名  不能相同. 原因:因为在vue中这两个都能用this.XX拿出来,如果写一样的,将不能分辨,计算机会默认覆盖一个. ...

  6. rest ---hateoas

    推荐一篇博客. https://www.ibm.com/developerworks/cn/java/j-lo-SpringHATEOAS/

  7. 用swift开发自己的MacOS锁屏软件(一)

    最近看到了NearLock这款软件,感觉还是很不错的,当我兴致勃勃的安装了体验之后,发现效果和自己所想的差太多了,所以,便想着自己写一个吧. 刚开始当然是查资料之类的,不查不知道,一查吓一跳,国内基本 ...

  8. java多线程设计模式(3)读写锁模式

    1 Read-Write Lock Pattern Read-Write Lock Pattern是一种将对于共享资源的访问与修改操作分离,称为读写分离.即访问是reader,修改是write,用单独 ...

  9. Netbeans 中部署运行Webservice出错

      错误如下 at java.lang.StackTraceElement at public java.lang.StackTraceElement[] java.lang.Throwable.ge ...

  10. 利用PPPOE认证获取路由器中宽带账号密码

    前言 回家时买了一台极路由准备换掉家里老掉牙的阿里路由器,想进后台看一下宽带账号密码,咦???后台密码是什么来着??? 我陷入了沉思,家里的路由器一般都是pppoe拨号,而路由器在与pppoe认证服务 ...