First One
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 881 Accepted Submission(s): 273
soda has an integer array $a_1,a_2,dots,a_n$. Let $S(i,j)$ be the sum of $a_i,a_i+1,dots,a_j$. Now soda wants to know the value below:
[sum_{i = 1}^{n}sum_{j = i}^{n}(lfloor log_{2}{S(i,j)}
floor + 1) imes (i+j) ]
Note: In this problem, you can consider $log_{2}{0}$ 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 geq n leq 10^5)$,the number of integers in the array.
The next line contains n integers $0 leq a_{i} leq 10^{5}$
Output
For each test case, output the value.
Sample Input
1
2
1 1
Sample Output
12
Source
解题:这道题目有意思,我们可以发现 $a_{i} leq 10^{5}$ 这是一个信息,破题的关键.
$log_{2}{sum}$大概会在什么范围呢?sum最多是 $10^{5} imes 10^{5} = 10^{10}$
也就是说$log_{2}{sum} leq 34$
才35,sum的特点是什么?很明显都是非负数,那么sum必须是递增的,单调的,F-100. 我们可以固定$log_{2}{S(i,j)}$ 后 固定左区间j,找出以j作为左区间,然后当然是找出最小的r 和 最大的 R
最小 最大?当然是这样的区间,该区间的和取对数是我们刚刚固定的那个数。区间可以表示成这样 $[j,rdots R]$ 那么从j到r,j到 r+1,...,j 到R ,这些区间的和取对数都会等于同一个数。。
好了如何算$[j,rdots R]$ 的下标和?很明显吗。。j r,j r+1, j r+2,..., j R.把左区间下标一起算了,右区间是个等差数列,求和。
我们在最后把那个表达式里面的1加上
同样的计算方法
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 100010; 5 LL p[38] = {1},sum[maxn]; 6 void init() { 7 for(int i = 1; i <= 36; ++i) 8 p[i] = (p[i-1]<<1); 9 } 10 int main() { 11 init(); 12 int kase,n; 13 scanf("%d",&kase); 14 while(kase--){ 15 scanf("%d",&n); 16 for(int i = 1; i <= n; ++i){ 17 scanf("%I64d",sum+i); 18 sum[i] += sum[i-1]; 19 } 20 LL ret = 0; 21 for(int i = 1; i <= 35 && p[i] <= sum[n]; ++i){ 22 int L = 1,R = 0; 23 LL tmp = 0; 24 for(int j = 1; j <= n; ++j){ 25 while(L <= n && sum[L] - sum[j-1] < p[i]) ++L; 26 while(R + 1 <= n && sum[R + 1] - sum[j-1] < p[i+1]) ++R; 27 if(L <= R) tmp += (LL)j*(R - L + 1) + 1LL*(L + R)*(R - L + 1)/2; 28 } 29 ret += tmp*i; 30 } 31 for(int i = 1; i <= n; ++i) 32 ret += LL(n + i)*(n - i + 1)/2 + LL(i)*(n - i + 1); 33 printf("%I64d ",ret); 34 } 35 return 0; 36 }