E.税收问题(Pesky Publicans)

题目描述
Tax collectors in the Roman empire, called publicans, are not well perceived, and for good reason. However, they are necessary for the running of the empire. Your job is to estimate how much money Rome will receive in taxes. Not everyone pays, but at least one person is guaranteed to pay. The trouble is you can never really know who will and won’t pay.
Given a list of what citizens would pay if they did pay taxes,output the average of all possible sums of taxes received.
输入
The first line of the input will be a single integer, n ≤ 1, 000. There will be n test cases that follow.
Each test case begins with a single integer p denoting the number of people in Rome, 1 ≤ p ≤ 10, 000. The next line will contain p space separated integers, 0 < vi < 10, 000,denoting the amount of taxes that each citizen would pay if they decided to pay.
输出
Each test case should contain a single floating point number denoting the average taxes paid, accurate and truncated to three decimal places.
样例输入
2
5
1 2 3 4 5
5
10 11 12 13 14
样例输出
7.742
30.968

题目大意
找出所有子集,求和,再用这个和除以子集个数求出平均值,注意不可能出现全0的情况,所以子集数量要减1。
思路
一开始以为是DP,但是没有取模数字太大过不了,所以只能计算公式求解,每个数被取到的次数是2^(n - 1)次,所以说答案就是sum * 2^(n - 1) / (2^n - 1),但是当n很大的时候无法计算出2^n,但是考虑到题目值保留三位小数,所以n比较大的时候可以把分母直接当成 2^n 来计算即可。
代码

#include <iostream>
#include <cmath>
 
using namespace std;
 
typedef long long LL;
 
const int N = 10010;
 
int a[N];
double ans;
 
int qmi(int a, int b)
{
    LL res = 1;
    while (b)
    {
        if (b & 1) res = res * a;
        a = a * a;
        b >>= 1;
    }
     
    return res;
}
 
int main()
{
    int t;
    cin >> t;
    while (t -- )
    {
        ans = 0;
        int n;
        cin >> n;
        for (int i = 0; i < n; i ++ )
        {
            cin >> a[i];
            ans += a[i];
        }
        // n较大直接忽略减1
        if (n > 55) printf("%.3f\n", ans / 2);
        else
        {
            double t = pow(2, n - 1);
            double x = pow(2, n) - 1;
            printf("%.3f\n", t / x * ans);
        }
    }
     
    return 0;
}
posted on 2021-04-10 15:27  Laurance  阅读(58)  评论(0编辑  收藏  举报