ZOJ-3827-Information Entropy
来源:互联网 发布:k速流量软件 编辑:程序博客网 时间:2024/06/14 04:24
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:
Here E is the expected value operator. When taken from a finite sample, the entropy can explicitly be written as
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:
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 P1, P2, .., PN. Pi 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
33 bit25 25 507 nat1 2 4 8 16 32 3710 dit10 10 10 10 10 10 10 10 10 10
Sample Output
1.5000000000001.4808108324651.000000000000
Author: ZHOU, Yuchen
Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest
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.
b的值具体按照这个进行选择
0log b(0)=limp→0+plog b(p)
x的值为0的时候要特判
S:
#include<stdio.h>#include<string.h>#include<math.h>double f(double x, int k){ if(x == 0) return 0; if (k == 1) return x * log2(x); else if (k == 2) return x * log(x); else return x * log10(x);}int main(){ int T, N, k; double x, ans; char S[10]; scanf("%d", &T); while(T--) { ans = 0; scanf("%d%s", &N, S); if (S[0] == 'b') k = 1; else if (S[0] == 'n') k = 2; else if (S[0] == 'd') k = 3; for(int i = 0; i < N; i++) { scanf("%lf", &x); ans += f(x / 100.0, k); } printf("%.12lf\n", -ans); } return 0;}
- ZOJ 3827 Information Entropy
- ZOJ-3827-Information Entropy
- ZOJ 3827 Information Entropy
- ZOJ-3827-Information Entropy
- zoj 3827 Information Entropy(水题)
- ZOJ 3827 Information Entropy 水
- zoj 3827 Information Entropy 【水题】
- ZOJ ——3827 Information Entropy
- ZOJ Problem Set - 3827Information Entropy
- 解题报告 之 ZOJ 3827 Information Entropy
- ZOJ 3827 Information Entropy【水题、简单相加】
- ZOJ 3827 Information Entropy (2014牡丹江区域赛)
- ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)
- zoj 3827 Information Entropy(2014牡丹江区域赛I题)
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy
- 信息熵 information Entropy
- Calculate Information Entropy
- 信息熵(Information Entropy)
- dfs 遍历 codeforces 24A
- linux查找命令
- 【多线程】(八)信号量
- java版的二叉树的先序遍历、中序遍历以及后序遍历(递归以及非递归方式)
- intval()
- ZOJ-3827-Information Entropy
- 第11周项目1:函数版星号图
- mysql基础命令
- listView 通过点击Item实现改…
- SVN:cannot map the&nbs…
- adb logcat 输出的几个小技巧
- 更改eclipse默认空间
- 修改带shape的View的背景色
- shortcut 如果后台还有activi…