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. 解决iOS10的Safari下Meta设置user-scalable=no无效的方法

    苹果为了提高Safari中网站的辅助功能,屏蔽了Meta下的user-scalable=no功能.所以在iOS10下面,就算加上user-scalable=no,Safari浏览器也能支持手动缩放. ...

  2. HDU-6315 Naive Operations//2018 Multi-University Training Contest 2___1007 (线段树,区间除法)

    原题地址 Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/ ...

  3. Function——记忆化搜索

    题目描述 对于一个递归函数w(a,b,c) 如果a<=0 or b<=0 or c<=0就返回值1. 如果a>20 or b>20 or c>20就返回w(20,2 ...

  4. Topcoder 刷题之路_鶸的奋斗

    最近碰到的题不是水题就是坑题,实在没意思,听说神犇们都在Topcoder上刷SRM,于是我决定将SRM的DIV 1刷个遍.这里是目录 哎..好多转博客不注明出处的,这里给出本博客的出处:http:// ...

  5. Android简单文件浏览器源代码 (转)

    Android简单文件浏览器源代码 (转) activity_main .xml <LinearLayout xmlns:android="http://schemas.android ...

  6. Ubuntu 16.04出现:dpkg: 处理软件包 xxx (--configure)时出错:

    如下所示: 解决方法: #将info文件夹更名 sudo mv /var/lib/dpkg/info /var/lib/dpkg/info_old #再新建一个新的info文件夹 sudo mkdir ...

  7. 体验VisualStudio 2013中的内存分析功能

    内存分析一直是个比较令人头痛的问题,Visual Studio 2013中就集成了一个内存分析的功能,可以方便我们进行分析内存的占用情况.本文将简单的介绍一下如何使用这个功能. 首先以一个简单的程序为 ...

  8. 为Chrome多账户添加单独的快捷方式

    Chrome的多账户功能非常好用,每个账户都有自己的独立的收藏夹.个人设置等.但是,当你要使用的账户不是默认账户时,必须经过一个切换的操作.本文将简单的介绍一个如何各账户添加快捷方式,从而实现直接登陆 ...

  9. A Beginner’s Guide to the OUTPUT Clause in SQL Server

    原文 A Beginner’s Guide to the OUTPUT Clause in SQL Server T-SQL supports the OUTPUT clause after the ...

  10. 开源 ≠ 免费,开源协议License详解

    凡是做过软件开发的,都会接触到开源软件或开源组件,它们都会基于某种协议来提供源码和授权,那么这些开源协议到底有哪些约束呢? 在介绍之前,必须告诉大家,针对开源协议,必须打消“开源 = 免费”这个念头, ...