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)