ZOJ 3827 Information Entropy(数学题)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5381

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:

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

PS:2014年ACM/ICPC 亚洲区域赛牡丹江(第一站)

代码如下:

#include<cstdio>
#include<cmath>
#include <cstring>
const double e = exp(1.0);
double judge(char s[])
{
    if(strcmp("bit",s) == 0)
        return 2.0;
    else if(strcmp("nat",s) == 0)
        return e;
    else if(strcmp("dit",s) == 0)
        return 10.0;

}
int main()
{
    int t;
    int n;
    char s[7];
    double p[117];
    //printf("%lf\n",e);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        scanf("%s",s);
        double b = judge(s);
        for(int i = 0; i < n; i++)
        {
            scanf("%lf",&p[i]);
            p[i] /= 100.0;
        }
        double ans = 0;
        double tt = log(b);
        for(int i = 0; i < n; i++)
        {
            if(p[i] != 0)
                ans+=p[i]*log(p[i])/tt;
            else if(p[i] == 0)
            {
                ans+=0;
            }
        }
        ans = -ans;
        printf("%.12lf\n",ans);
    }
    return 0;
}
时间: 2024-10-12 14:22:07

ZOJ 3827 Information Entropy(数学题)的相关文章

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

题目链接:ZOJ 3827 Information Entropy 根据题目的公式算吧,那个极限是0 AC代码: #include <stdio.h> #include <string.h> #include <math.h> const double e=exp(1.0); double find(char op[]) { if(op[0]=='b') return 2.0; else if(op[0]=='n') return e; else if(op[0]=='

ZOJ 3827 Information Entropy

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3827 题面: 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 co

ZOJ 3827 Information Entropy 水

水 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

zoj 3827 Information Entropy 【水题】

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 t

zoj 3827 Information Entropy(2014牡丹江区域赛I题)

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 t

zoj 3827(牡丹江现场赛I题)

ZOJ Problem Set - 3827 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 informatio

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

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 t

information entropy as a measure of the uncertainty in a message while essentially inventing the field of information theory

https://en.wikipedia.org/wiki/Claude_Shannon In 1948, the promised memorandum appeared as "A Mathematical Theory of Communication," an article in two parts in the July and October issues of the Bell System Technical Journal. This work focuses on

POJ1607 &amp; HDU 1330 &amp; ZOJ 1216 Deck(数学题)

题目链接: POJ  1607 : http://poj.org/problem?id=1607 HDU 1330 :http://acm.hdu.edu.cn/showproblem.php?pid=1330 ZOJ  1216 : Description A single playing card can be placed on a table, carefully, so that the short edges of the card are parallel to the table