hdu 5358 First One 2015多校联合训练赛#6 枚举

First One

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 142    Accepted Submission(s): 37


Problem Description
soda has an integer array  a1,a2,,an. Let  S(i,j) be the sum of  ai,ai+1,,aj. Now soda wants to know the value below:
i=1nj=in(log2S(i,j)+1)×(i+j)

Note: In this problem, you can consider  log20 as 0.
 

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  (1n105), the number of integers in the array.
The next line contains  n integers  a1,a2,,an  (0ai105).
 

Output
For each test case, output the value.
 

Sample Input

   
1 2 1 1
 

Sample Output

   
12
 

Source
 

因为下取整log(sum)的值是非常小的。

能够枚举每一个位置为開始位置,然后枚举每一个log(sum)仅仅需36*n的。

中间j的累加和

推公式就可以。

可是找log值同样的区间,须要用log(sum)*n的复杂度预处理出来,假设每次二分位置会超时。



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define maxn 100007
ll num[maxn];
ll pos[maxn][36];

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        for(int i = 0;i < n; i++){
            scanf("%I64d",&num[i]);
        }

        num[n] = 0;
        for(ll i = 0;i < 36; i++){
            ll di = 1LL<<(i+1);
            ll su = num[0];
            int p = 0;
            for(int j = 0;j < n; j++){
                if(j) su -= num[j-1];
                while(su < di && p < n){
                    su += num[++p];
                }
                pos[j][i] = p;
            }
        }

        ll ans = 0,res;
        for(int i = 0;i < n; i++){
            ll p = i,q;
            for(int j = 0;j < 36 ;j ++){
                q = pos[i][j];
                res = (j+1)*((i+1)*(q-p)+(p+q+1)*(q-p)/2);
                ans += res;
                p = q;
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


转载于:https://www.cnblogs.com/claireyuancy/p/7151945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值