# Hdu 5921 Binary Indexed Tree

cnti$cnt_i$表示i$i$的二进制表达式中1$1$的个数，lcp(i,j)$lcp(i,j)$表示i$i$j$j$的二进制表示的最长公共前缀（如果长度不同，右对齐之后在左边补0），那么就是求

r=1nl=0r1(cntl+cntr2cntlcp(l,r))=12l=0nr=0n(cntl+cntr2cntlcp(l,r))

#include<bits/stdc++.h>
using namespace std;

const int maxn = 63,mod = 1e9+7;
#define LL long long

int inser(int *s,LL *v,LL x){
memset(s,0,sizeof(int)*maxn);
int len = 0;
while(x)
s[len++] = x & 1, x >>= 1;
v[0] = s[0];
for(int i=1;i<len;i++)
v[i] = v[i-1]+((1ll*s[i])<<i);
for(int i=0;i<len;i++)
v[i] = (v[i] + 1) % mod;
return len;
}

LL dp[maxn][2][2][2], lr[maxn];
int lrs[maxn];

LL tim(int pos,bool lnd,bool rnd){
if(pos < 0) return 1;
LL full = (1ll << (pos+1)) % mod, ret = 1;
if(lnd) (ret *= lr[pos]) %= mod;
else (ret *= full) %= mod;
if(rnd) (ret *= lr[pos]) %= mod;
else (ret *= full) %= mod;
return ret;
}

LL dfs(int pos,bool lbnd,bool rbnd,bool scnt){
if(pos < 0) return 0;
LL & ndp = dp[pos][lbnd][rbnd][scnt];
if(ndp != -1) return ndp;
int lb = lbnd ? lrs[pos] : 1, rb = rbnd ? lrs[pos] : 1;
ndp = 0;
for(int i=0;i<=lb;i++){
for(int j=0;j<=rb;j++){
(ndp += dfs(pos-1
,lbnd && i == lb
,rbnd && j == rb
,scnt || (i != j)) ) %= mod;
(ndp += (scnt||(i!=j))*(i+j)
* tim(pos-1
,lbnd && i == lb
,rbnd && j == rb) ) %= mod;
}
}
return ndp;
}

LL cal(LL num){
memset(dp,-1,sizeof(dp));
int n = inser(lrs,lr,num);
return dfs(n-1,true,true,false);
}

int main(){
int T,icase = 1;
scanf("%d",&T);
LL n;
memset(dp,-1,sizeof(dp));
while(T-- && ~scanf("%lld",&n)){
LL rev = (mod + 1) / 2;
LL ans = cal(n)* rev % mod;
printf("Case #%d: %lld\n",icase++,ans);
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120