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:
![](https://i-blog.csdnimg.cn/blog_migrate/10071fccdc4e8b3327b414f3e1963728.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ef52a6cc5c634caf3a71bce0b59f76d1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e42b7d19160ac67d96c7f6f14c9b6324.png)
![](https://i-blog.csdnimg.cn/blog_migrate/db238ccb05945630719b306bc2ab6b0e.png)
![](https://i-blog.csdnimg.cn/blog_migrate/529907827c93e9a6d7e0c213ae707533.png)
![](https://i-blog.csdnimg.cn/blog_migrate/909960b743f3fd8ee512e0f812f3f868.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ddafc18eaad0899dbd577a8df762cafc.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4a9f2dcce1581a64009f643405874161.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8d9fa696a15343a190971e07d023e18a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/95475c93a5e1e88bf5be9a2a8ed9c471.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ef52a6cc5c634caf3a71bce0b59f76d1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b7d25de2f7a69242758710dac2fa94ba.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ef52a6cc5c634caf3a71bce0b59f76d1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/becd1497d96098dca12918cd44568a21.png)
![](https://i-blog.csdnimg.cn/blog_migrate/db238ccb05945630719b306bc2ab6b0e.png)
![](https://i-blog.csdnimg.cn/blog_migrate/db238ccb05945630719b306bc2ab6b0e.png)
Here E is the expected value operator. When taken from a finite sample, the entropy can explicitly be written as
![](https://i-blog.csdnimg.cn/blog_migrate/10071fccdc4e8b3327b414f3e1963728.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ddafc18eaad0899dbd577a8df762cafc.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e42b7d19160ac67d96c7f6f14c9b6324.png)
![](https://i-blog.csdnimg.cn/blog_migrate/529907827c93e9a6d7e0c213ae707533.png)
![](https://i-blog.csdnimg.cn/blog_migrate/4a9f2dcce1581a64009f643405874161.png)
![](https://i-blog.csdnimg.cn/blog_migrate/57cf28ca1652649c1683c3556ef557be.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1617f1bed76a7f4d8912575404afc09f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/0690fbf5730772c7b12613b47e254d4f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/53ce3b0c03841114ef728eedc156f2a7.png)
![](https://i-blog.csdnimg.cn/blog_migrate/358841943ad6b78afc448f704e0de963.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b7d25de2f7a69242758710dac2fa94ba.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ef52a6cc5c634caf3a71bce0b59f76d1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/becd1497d96098dca12918cd44568a21.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1617f1bed76a7f4d8912575404afc09f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8d9fa696a15343a190971e07d023e18a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ae1db97e51b2da2aad0e36cd78e390af.png)
![](https://i-blog.csdnimg.cn/blog_migrate/2bfe47c01754349d9e96de2a6df471a6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d2b03e9645787acaa17ceae779485c20.png)
![](https://i-blog.csdnimg.cn/blog_migrate/fb34c57dc11c029fbecb4e41179effc3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ef52a6cc5c634caf3a71bce0b59f76d1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b7d25de2f7a69242758710dac2fa94ba.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ddafc18eaad0899dbd577a8df762cafc.png)
![](https://i-blog.csdnimg.cn/blog_migrate/becd1497d96098dca12918cd44568a21.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1617f1bed76a7f4d8912575404afc09f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/db238ccb05945630719b306bc2ab6b0e.png)
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:
![](https://i-blog.csdnimg.cn/blog_migrate/66c3113d66395ce17f9c2c03b84751a2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ae1db97e51b2da2aad0e36cd78e390af.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c21106372d668241175bce03e9f0d02d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d2b03e9645787acaa17ceae779485c20.png)
![](https://i-blog.csdnimg.cn/blog_migrate/fb34c57dc11c029fbecb4e41179effc3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ef52a6cc5c634caf3a71bce0b59f76d1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e63fd3b33f8d834a8951ac95958e2395.png)
![](https://i-blog.csdnimg.cn/blog_migrate/529907827c93e9a6d7e0c213ae707533.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8d9fa696a15343a190971e07d023e18a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/3a3bc9c2a2b76a24fa64ae748e87411d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ed01bf9a72c490eaa2a4cc3134893b83.png)
![](https://i-blog.csdnimg.cn/blog_migrate/52f5d144af8aee84107440cdac922c15.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8db2db31821c2164a1837b427243eed1.png)
![](https://i-blog.csdnimg.cn/blog_migrate/e0bcc06dc946b159f52f881ce3acafa0.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b94583372c96b5683dce5b04b45f1767.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b566ee9bfc9f611fbe594629ace75401.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ae1db97e51b2da2aad0e36cd78e390af.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c21106372d668241175bce03e9f0d02d.png)
![](https://i-blog.csdnimg.cn/blog_migrate/25c4f48bc0babf4ff3f900e8960884f2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/262261e2c94239977905abdeeee73b12.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ddafc18eaad0899dbd577a8df762cafc.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7d3c5055cca966395de176eb1828d710.png)
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;
}