您好,登錄后才能下訂單哦!
#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的二進制1的個數
//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的個數
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]內i 的二進制下1的個數 總合
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。