ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

数列字段和

2021-04-06 23:02:06  阅读:160  来源: 互联网

标签:数列 int pay ll 字段 ans 序列 include


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.

样例输入 Copy

2
5
1 2 3 4 5
5
10 11 12 13 14

样例输出 Copy

7.742
30.968

这个题就是求他的全部的子段和除以他的个数
就是 (全部字段和)/(2^n-1)
全部字段和为sum*(2^(n-1))

但是这个题是n很大,所以盲猜一波当n很大的时候1/(2^(n-1))为0
sum*(2^(n-1))/(2^n-1)==sum/(2-1/(2^(n-1))
#include<iostream>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+100;
ll a[maxn];
ll qpow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            ans=(ans*a);
        } 
        a=(a*a);
        b>>=1;
    }
    return ans;
}
int main(){ 
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        ll ans=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            ans+=a[i];
        }
        if(n>62){
            printf("%.3lf\n",1.0*ans/2.0);
        }
        else{
            ll tmp=qpow(2,n-1);
            ll tmp1=tmp*2-1;
            double tmp2=1.0*ans*tmp/tmp1;
            printf("%.3lf\n",tmp2); 
        } 
    }
} 

序列字段积和

链接:https://ac.nowcoder.com/acm/contest/13504/I
来源:牛客网

众所周知,一个序列拥有许多非空子序列。

所谓子序列就是在原序列中任意删除 0 个或多个元素,然后保持剩下元素的顺序不变所形成的序列。非空子序列集意味着剩下的子序列不能为空。

比如对于序列[1, 2, 3],它的所有非空子序列为:[1, 2, 3],[1, 2],[1, 3],[2, 3],[1],[2],[3]。再比如序列 [1, 1],它的非空子序列有:[1, 1],[1] (删除了第一个 1),[1] (删除了第二个1) 。

现在母牛哥手里有一个长度为 n 的正整数序列,他现在要为这个序列的所有非空子序列打分。对于一个序列而言,它的评分标准为序列里所有数的乘积(只有一个数的序列,分数就是这个数)。

母牛哥想要知道所有分数的和,但由于结果太大了,所以你只要告诉母牛哥结果对 1000000007 取模即可。

输入描述:

第一行为 n,表示这个序列长度为 n (1 <= n <= 10^6)。
接下来的一行有 n 个数字 a1, a2, …… , an (1 <= ai <= 2 * 10^9) 表示序列的 n 个数字。

 

输出描述:

一个非负整数,表示结果对 1000000007 取模。
示例1

输入

复制
3
1 2 3

输出

复制
23
示例2

输入

复制
2
1 1

输出

复制
3


这是一个结论题
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=5e6+100;
const int mod=1e9+7;
ll a[maxn];
int main(){
    int n;
    cin>>n;
    ll sum=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        sum+=a[i];
    } 
    if(n==1){
        cout<<sum<<endl;
        return 0;
    } 
    ll ans=0;
    for(int i=1;i<=n;i++){
        ll z=sum-a[i];
        ans=(ans+z*a[i])%mod;
    } 
    cout<<(ans+1)%mod<<endl;
}

 

标签:数列,int,pay,ll,字段,ans,序列,include
来源: https://www.cnblogs.com/lipu123/p/14624478.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有