您好,登錄后才能下訂單哦!
#include<bits/stdc++.h>
typedef long long ll;
const ll MO = 1000000007;
int T,bin[63],lens;
ll n,ans,power[63],pre[63],nxt[63];
ll read(){ll x;scanf("%lld",&x);return x;}
int main()
{
scanf("%d",&T), power[0] = 1;
for (int i=1; i<=61; i++) power[i] = (power[i-1]<<1)%MO;
for(int cas=1;cas<=T;cas++)
{
n = read(), ans = lens = 0;
for (ll x=n; x; x>>=1) bin[++lens] = x&1;
pre[0] = nxt[lens+1] = 0;
for (int i=1; i<=lens; i++) pre[i] = (pre[i-1]+power[i-1]*bin[i])%MO;
for (int i=lens; i>=1; i--) nxt[i] = ((nxt[i+1]<<1)+bin[i])%MO;
for (int i=1; i<=lens; i++)
{
if (bin[i]) pre[i-1]++,ans = (ans+pre[i-1])%MO;
ans = (ans+nxt[i+1]*power[i-1]%MO)%MO;
}
// ans為1到n中i的二進(jìn)制1的個數(shù)
//std::cout<<ans<<'\n';continue;
ans = (n+1ll)%MO*ans%MO;
for (int i=1; i<=lens; i++)
{
if (bin[i]) ans = (ans+MO-pre[i-1]*pre[i-1]%MO)%MO;
ans = (ans-nxt[i+1]*power[i-1]%MO*power[i-1]%MO+MO)%MO;
}
// ans-[i,j]前綴1的個數(shù)
printf("Case #%d: %lld\n",cas,ans);
}
return 0;
}
// for(ll i=1;i<=n;i<<=1,++len)
// if(n&i)ans+=n%i+1+(n>>len+1)<<len;
// else ans+=(n>>len+1)<<len;
// i屬于 [1,n]內(nèi)i 的二進(jìn)制下1的個數(shù) 總合
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。