hdu5358 First One
2015-08-09 16:55
330 查看
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=1n∑j=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 (1≤n≤105),
the number of integers in the array.
The next line contains n integers a1,a2,…,an (0≤ai≤105).
Output
For each test case, output the value.
Sample Input
Sample Output
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=1n∑j=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 (1≤n≤105),
the number of integers in the array.
The next line contains n integers a1,a2,…,an (0≤ai≤105).
Output
For each test case, output the value.
Sample Input
1 2 1 1
Sample Output
12 这题题意容易懂,就是求和,其中(⌊log2S(i,j)⌋+1)的意思就是S(i,j)化成二进制后的比特位个数,因为S(i,j)不超过10^10,所以比特位不会超过35个。我们可以先初始化b[],记录比特位为i的所有数中的最后一个数2^i-1,用sum[i]把从1到i的总和记录下来,然后用35个指针pt[i]记录以i为起点的最大下标k满足sum[k]-sum[i-1]<=b[j]。最后注意要用G++交,C++会超时。。#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<vector> #include<map> #include<set> #include<queue> #include<stack> #include<string> #include<algorithm> using namespace std; #define ll long long #define maxn 100060 ll b[50],sum[maxn];//b[1]=2^i-1 ll a[maxn]; int pt[44];//指针 void init() { int i,j; b[0]=-1; b[1]=1; for(i=2;i<=35;i++){ b[i]=(1LL<<i)-1; //也可以是b[i]=((ll)1<<i)-1;,但不加的话会爆int } } int main() { int n,m,i,j,T,len; ll ans; init(); scanf("%d",&T); while(T--) { scanf("%d",&n); sum[0]=0;ans=0; for(i=1;i<=n;i++){ scanf("%lld",&a[i]); sum[i]=sum[i-1]+a[i]; } for(i=1;i<=35;i++)pt[i]=0; for(i=1;i<=n;i++){ pt[0]=i-1; for(j=1;j<=34;j++){ while(sum[pt[j]+1]-sum[i-1]<=b[j] && pt[j]<n){//如果a>b,那么pt[a]一定大于等于pt[b] pt[j]++; } //if(sum[pt[j]]-sum[i-1]>b[j-1] && sum[pt[j]]-sum[i-1]<=b[j] && pt[j]>=i ){ 这一句可以不用写 len=(pt[j]-pt[j-1]); ans+=(ll)j*len*i; ans+=(ll)j*len*(pt[j-1]+1+pt[j])/2; //} } } printf("%lld\n",ans); } return 0; }
相关文章推荐
- uva 1486 Transportation (费用流+拆边)
- 《围住神经猫》的逃跑路径算法
- 轻量级文本编辑器,Notepad最佳替代品:Notepad++
- Android Format的使用
- Linux下用php调用C接口(扩展)的资料
- 管道,信号量,共享内存,socket的实际使用场景和NSPipe管道的使用
- jQuery中的Ajax
- (算法:二分查找)在排序数组中,找出给定数字出现的次数
- 2015公务员工资改革消息:广州人社局长称基层公务员工资肯定会增不少
- Java的date和以datetime为字段的数据库的处理办法
- hdu-2114-Calculate S(n)
- 为什么你宁愿每周工作90小时,也不做任何改变?(转载)
- 使用代码为textview设置drawableLeft
- 大苏格兰2015年中学等级考试的一道初等数学题
- Java查看List扩容后的容量
- 10.1——pair,map,set,multimap,multiset
- OpenGl编程指南例2.4大白话分析
- MVC简单实现查询列表
- Leetcode52 N-Queens II
- MongoDB查询