hdu-5358 first one (ruler)

Source: Internet
Author: User

Topic Links:

First one

Time limit:4000/2000 MS (java/others)

Memory limit:131072/131072 K (java/others)


problem DescriptionSoda have an integer arraya1,a2,... ,an . LetS(i,J) Be the sum of Ai,ai+< Span id= "mathjax-span-36" class= "mn" >1 , Aj . Now soda wants to know the value below: ∑I=1N∑J=IN(⌊ Log2 s ( I,j ) ⌋ +1) X (i +j)
Note:in This problem, you can consider log20 as 0.

InputThere is multiple test cases. The first line of input contains an integerT, indicating the number of test cases. For each test case:

The first line contains an integerN (1≤n≤5) The number of integers in the array.
The next line containsNIntegersa1,a2,... ,an (0≤ai≤5).

OutputFor each test case, output the value.

Sample Input121 1

Sample Output12 Test Instructions:I beg for this sum. Ideas:Still have to use the ruler, I find two end points, tle, it seems that the constant card is very tight ah ah; AC Code:
/*5358 1638MS 3120K 1119 B g++ 2014300227*/#include<bits/stdc++.h>using namespacestd;Const intn=1e5+4; typedefLong Longll;intN;ll A[n],sum[n];intMain () {intT; scanf ("%d",&t);  while(t--) {scanf ("%d",&N);  for(intI=1; i<=n;i++) {scanf ("%lld",&A[i]); } sum[0]=0; A[n+1]=0;  for(intI=1; i<=n+1; i++) {Sum[i]=sum[i-1]+A[i]; } ll ans=0, num=0; ll L=0, r=1, Len;  for(intI=0; i<= -; i++) {num=0; intL=1, r=1;  for(intj=1; j<=n;j++) {L=Max (j,l);  while(l<=n&&sum[l]-sum[j-1]&LT;L) l++; R=Max (r,l);  while(r<=n&&sum[r]-sum[j-1]<=r&&sum[r]-sum[j-1]&GT;=L) r++; Len=r-l; Num+=len* (LL) j+len* (l+r-1)/2; } L=r+1; R=2*l-1; Ans+=num* (LL) (i+1); } printf ("%lld\n", ans); }    return 0;}

hdu-5358 first one (ruler)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.