I - Information Entropy

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Submit Status

Description

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 Piwill 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 按照题目中所给的第二个公式求出结果,当字符为“bit”时log的底数为2当字符为“nat”时底数为e字符为“dit”时底数为10
注意所给数据中出现0时要把0 排除
注:求log₂X,log10X,lnx 直接调用math头文件中的log2(),log10(),log()即可
#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define maxn 110
using namespace std;
int main()
{
int t,n;
double a[maxn];
double sum;
char s[4];
scanf("%d",&t);
while(t--)
{
sum=0;
scanf("%d %s",&n,s);
for(int i=1;i<=n;++i)
scanf("%lf",&a[i]);
if(strcmp(s,"bit")==0)
{
for(int i=1;i<=n;++i)
{
if(a[i]!=0)
sum-=(a[i]*0.01*((log10(a[i]*0.01)/log10(2))));
}
}
else if(strcmp(s,"nat")==0)
{
for(int i=1;i<=n;++i)
{
if(a[i]!=0)
sum-=(a[i]*0.01*log(a[i]*0.01));
}
}
else if(strcmp(s,"dit")==0)
{
for(int i=1;i<=n;++i)
{
if(a[i]!=0)
sum-=(a[i]*0.01*log10(a[i]*0.01));
}
}
printf("%.12lf\n",sum);
}
return 0;
}

  

2014 牡丹江现场赛 i题 (zoj 3827 Information Entropy)的更多相关文章

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

    套公式 Sample Input 33 bit25 25 50 //百分数7 nat1 2 4 8 16 32 3710 dit10 10 10 10 10 10 10 10 10 10Sample ...

  2. zoj 3820(2014牡丹江现场赛B题)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5374 思路:题目的意思是求树上的两点,使得树上其余的点到其中一个点的 ...

  3. zoj 3819(2014牡丹江现场赛 A题 )

    题意:给出A班和B班的学生成绩,如果bob(A班的)在B班的话,两个班级的平均分都会涨.求bob成绩可能的最大,最小值. A班成绩平均值(不含BOB)>A班成绩平均值(含BOB) &&a ...

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

    题目链接:ZOJ 3827 Information Entropy 依据题目的公式算吧,那个极限是0 AC代码: #include <stdio.h> #include <strin ...

  5. ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5381 Information Theory is one of t ...

  6. 【解题报告】牡丹江现场赛之ABDIK ZOJ 3819 3820 3822 3827 3829

    那天在机房做的同步赛,比现场赛要慢了一小时开始,直播那边已经可以看到榜了,所以上来就知道A和I是水题,当时机房电脑出了点问题,就慢了好几分钟,12分钟才A掉第一题... A.Average Score ...

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

    Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Information ...

  8. ZOJ 3827 Information Entropy 水题

    Information Entropy Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/sh ...

  9. ZOJ 3827 Information Entropy 水

    水 Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Informati ...

随机推荐

  1. ERROR ITMS-90167: "No .app bundles found in the package"

    http://stackoverflow.com/questions/37838487/error-itms-90167-no-app-bundles-found-in-the-package 简单说 ...

  2. Action<>和Func<>区别

    Action<>和Func<>其实都是委托的[代理]简写形式. 简单的委托写法: //普通的委托 public delegate void myDelegate(string ...

  3. MSSQL Server语句

    随机从数据库中取出20条数据:select top 20 * from 表名 order by newid()

  4. Win32中GDI+应用(三)---Graphics类

    在我理解看来,Graphics是一个device context和你的drawing conetent之间的一个中介.它存储了device context的相关属性,以及drawing content ...

  5. oracle简单两个操作

    sqlplus sys/密码 as sysdba ALTER USER 账号 IDENTIFIED BY 新密码; select *  from (select rownum 别名 ,表名.* fro ...

  6. js上传图片并预览

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  7. ecshop安装程序源码阅读-安装脚本(2)

    检查环境变量: 程序目录:图片目录,数据目录,临时目录 模板目录下模板文件 数据库连接函数 数据库配置: 读取数据库列表 创建配置文件(数据库,语言,session有效期等) 创建数据表 创建初始化数 ...

  8. resolvconf: Error: /etc/resolv.conf isn't a symlink, not doing anything.

    一.问题出现的原因 resolv.conf默认是个软链接,resolvconf默认会检查resolv.conf不是软链接就报错 默认的情况是这样的: #ls -l /etc/resolv.conflr ...

  9. 如何通过友盟分析发布后App崩溃日志-b

    要分析崩溃日志,首先需要保留发布时的编译出来的.xcarchive文件.这个文件包含了.DSYM文件. 我一般的做法是,发布成功后,把这个文件.xcarchive直接提交到代码版本库对应的版本分支里, ...

  10. 从UI Automation看Windows平台自动化测试原理

    前言 楼主在2013年初研究Android自动化测试的时候,就分享了几篇文章 Android ViewTree and DecorView Android自动化追本溯源系列(1): 获取页面元素 An ...