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 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:

[Math Processing Error]

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

[Math Processing Error]

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:

[Math Processing Error]

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

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


题意:就代个公式,比较坑的是没说清楚pi=0的情况怎么处理,试了好几遍,发现pi==0的时候,直接处理为0就行。建议e的精度取高点。


代码1:

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>
#define e 2.7182818284590
using namespace std;

int main()
{
    long double hx,E; 
    int t,n,tmp;
    string s;
    cin>>t;
    while(t--)
    {
    	hx=0;
    	cin>>n>>s;
   	    if(s[0]=='b')
   	    E=2.0;
   	    else if(s[0]=='n')
   	    E=e;
   	    else 
   	    E=10;
   	    for(int i=1;i<=n;i++)
   	    {
   	    	cin>>tmp;
   	    	if(tmp==0)
   	    	continue;
   	    	else
    	    hx+=(1.0*tmp/100)*(log(1.0*tmp/100)/log(E));  	
 	    }
 	    hx=0-hx;
 	    cout<<fixed<<setprecision(12)<<hx<<endl;
    }
	return 0;
}

代码2:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <cstdlib>
#include <set>
#include <algorithm>
#include <string>
#include <iomanip>
#define LL long long
using namespace std;
LL a,b,s;
double cal(int n,double x)
{
	int tmp;
	double res=0.0;
	for(int i=0;i<n;i++)
	{
		cin>>tmp;
		if(tmp)
		{
           res+=(-0.01*tmp*log(0.01*tmp)/log(x));
		}
	}
	return res;
}
int main()
{
	int n,t;
	cin>>t;
	string s;
	while(t--)
	{
		double ans=0.0;
		cin>>n>>s;
        if(s[0]=='b')
		{
			ans=cal(n,2);
		}
		else if(s[0]=='n')
		{
			ans=cal(n,2.718281828);
		}
		else 
		{
			ans=cal(n,10.0);
		}
		cout<<fixed<<setprecision(8)<<ans<<endl;
	}
	return 0;
}


你可能感兴趣的:(入门,ZOJ,区域赛)