HDU 5358 first one (ruler)

Source: Internet
Author: User

Title: http://acm.hdu.edu.cn/showproblem.php?pid=5358

Test instructions

Analysis: The first thing to know [log2 (x)]+1 represents the number of bits of x, and according to test instructions no more than 35, then the number of enumeration bits i:1~35. For each bit I find the interval [x, y], so that the number of bits in the binary representation of s (x, y) equals I, at this point the contribution is i* (x+y). So for each I, how to find all qualifying intervals [x,y]?1~n enum start x, then Y will satisfy the condition within a range [l,r]. The next time X becomes x+1, that is, the beginning X shifts right, then the range of Y to find now is [l ', R '],l ' not less than L same R ' not less than R. This makes it possible to find all intervals with the complexity of O (n) so that the number of digits of the interval and the binary representation is enumerated I. The total time complexity is 35*n.

Code:

#include <iostream> #include <cstdio> #include <cstring>using namespace std;typedef long Long ll;const int MAXN = 1e5+6; LL Pow2l[50],pow2r[50],s[maxn];int Main () {for (int i=1;i<=40;i++) {pow2l[i]= (1ll<<i);p ow2r[i]= ((1ll< < (i+1))-1);} Pow2l[0]=0;pow2r[0]=1;int ncase,i,j,n,x;scanf ("%d", &ncase), while (ncase--) {scanf ('%d ', &n); for (i=1;i<= n;i++) {scanf ("%d", &x); s[i]=s[i-1]+x;} LL ans=0;for (i=1;i<=35;i++) {if (s[n]<pow2l[i-1]) break; LL l=1,r=0,temp=0;for (j=1;j<=n;j++) {l= (l>j?l:j), while (L<=n && s[l]-s[j-1]<pow2l[i-1]) l++;r= (r >L-1?R:L-1); while (R+1<=n && s[r+1]-s[j-1]>=pow2l[i-1] && s[r+1]-s[j-1]<=pow2r[i-1]) r++ ; if (r>=l) temp+= (r-l+1) *j+ (r+l) * (r-l+1)/2;} Ans+=temp*i;} printf ("%i64d\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.