zoj 3293 找规律

其实我不知道要说什么,因为这道题的解法也是从别人那里看的,不想盗版,看看这位神牛的吧,讲得挺不错,就是要好生理解一下。

http://blog.csdn.net/xymscau/article/details/6688671

View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 typedef long long LL;
 6 int const N = 33;
 7 LL dp[N],pow2[N];
 8 void pre()
 9 {
10      dp[1]=2;
11      pow2[0]=1,pow2[1]=2;
12      for(int i=2;i<=32;i++)
13          dp[i]=(dp[i-1]<<1)+((1LL<<(i-1))-1)*(1LL<<(i-1))+(1LL<<i),pow2[i]=(pow2[i-1]<<1);
14 }
15 LL getsum1(LL n)
16 {
17    int l=0,r=32;
18    while(l<r)
19    {
20         int m=(l+r)>>1;
21         pow2[m]-1>n?(r=m):(l=m+1);
22    }
23    l--;
24    return (pow2[l]-1==n)?(dp[l]):(dp[l]+pow2[l]*(n-pow2[l]+1)+getsum1(n-pow2[l]+1));
25 }
26 int main()
27 {
28     LL l,r;
29     pre();
30     while(~scanf("%lld %lld",&l,&r))
31     {
32           printf("%lld\n",getsum1(r)-getsum1(l-1));
33     }
34     return 0;
35 }
posted @ 2013-05-09 15:32  诺小J  阅读(192)  评论(0编辑  收藏  举报