2014 牡丹江预选赛 i题

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;
}

  

时间: 2024-09-30 19:09:51

2014 牡丹江预选赛 i题的相关文章

zoj 3822 Domination 概率dp 2014牡丹江站D题

Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboar

BNUOJ 34985 Elegant String 2014北京邀请赛E题 动态规划 矩阵快速幂

Elegant String Time Limit: 1000msMemory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main We define a kind of strings as elegant string: among all the substrings of an elegant string, none of them is a permutation of "0, 1,-, k

2014 BNU 邀请赛E题(递推+矩阵快速幂)

Elegant String 题意:给定一个字符串,由0-k数字组成,要求该串中,子串不包含0-k全排列的方案数 思路:dp[i][j]表示放到i个数字,后面有j个不相同,然后想递推式,大概就是对应每种情况k分别能由那几种状态转移过来,在纸上画画就能构造出矩阵了,由于n很大,所以用快速幂解决 代码: #include <stdio.h> #include <string.h> const long long MOD = 20140518; int t; long long n; i

2014软考下午题攻略(一)—数据流图

引言 根据新版大纲,最近几年的软件设计师水平考试来看,数据流图的设计已成为下午题中必考的知识点.常出题型为:补充数据流图,解释设计原则,偶尔也会考数据字典的内容,但是都很简单. 考点分析 根据最近几年的出题趋势看,本题分值为15分,共分为4小题.前3小题的题型都是比较固定的,比较简单,而第4小题考的则比较灵活,稍微有点难度,所以只要大家细心(对,就是细心,切记!一定要细心)分数是很容易拿到手的.本题考察的知识点主要有通过给定的背景分析,掌握数据流图的画法及一些设计原则.下面是几个必考的设计原则,

hdu4862 2014多校B题/ 费用流(最优情况下用不大于K条路径覆盖)(不同的解法)

题意: 一个数字矩阵,可以出发K次,每次可以从右边或者下面走,要求(在收益最大情况下)覆盖全图,不能则输出-1.(规则:每次跳一步的时候若格子数字相等则获得该数字的能量,每跳一步消耗距离的能量).每个格子走且仅能走一次. 选<=K条路径,最优情况来覆盖全图. 显然用拆点为二分图. 一种解法:边(流量,费用) 源点向X部连边(1,0)Y部向汇点连边(1,0)X到Y,若能到,则有边(1,消耗-获得).关键点(解决每个点都覆盖,恰好起到填补的作用):在X部最上面添加一个点,源点连之(k,0)它向所有Y

BNUOJ 34985 Elegant String 2014北京邀请赛E题 矩阵快速幂

题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 题目大意:问n长度的串用0~k的数字去填,有多少个串保证任意子串中不包含0~k的某一个全排列 邀请赛上A的较多的一道题,比赛的时候死活想不出,回来之后突然就想通了,简直..... = =! 解题思路: 对于所有串我们都只考虑末尾最多有多少位能构成全排列的一部分(用l来表示),即最多有多少位不重复的数字出现,将问题转化为求末尾最多有k位能构成全排列的串的总数量 假设k为5,有一个

2014北京邀请赛E题-矩阵快速幂

题意:长度为n(1<=n<=10^18)的并且任意连续子串都不是0-k(1<=k<=9)的一个排列的字符串有多少种. 解法:矩阵快速幂.dp[i][j]表示i长度最后连续j个不同(即最后j个无重复,最后j+1个有重复)的字符串的个数.状态选好很重要.设计状态时最重要考虑是唯一性和可传递性,比赛时明明知道肯定是矩阵快速幂,但是一直没想到这个状态表示,自己设计的自己都不会转移. dp[i][j]有了后,后边加一个字符,这个字符可以是j之内的任意一个,也可以是j以外的,这样枚举每种情况,

2014 BNU 邀请赛A题(构造问题)

A Matrix 题意:按照题目中给定的方法,给你一个矩阵,求出变换出该矩阵的字符串 思路:构造问题,在纸上多画几组就能发现,每次必须从上往下找到一条路径,最后输出这些路径,按照开头最大的最晚输出,找的过程中只要不断往下一层找一个大的即可,并且如果一开使有一行是非递增就是错误 代码: #include <stdio.h> #include <string.h> #include <vector> #include <map> using namespace

2014 BNU 邀请赛B题(枚举)

Beautiful Garden 题意:x轴上放了一些树,现在要移动一些树使得所有树都等间距,问最少要移动多少棵 思路:枚举,枚举第一棵树,和另一棵树,以及中间有多少树,这样就能知道等差数列的首项和公差,然后再循环一边计算出答案,保存最小值 代码: #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; #define