题解:列出从0到n的二进制,然后观察一下,我们会们发现在从右到左第一位所有数对答案的贡献是n,第二位n/2,第三位n/(2*2),第四位是n/(2*2*2),然后规律就有了,写一个循环就行了。
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
void solve(){
LL a;
scanf("%lld", &a);
LL ans = 0;
for(int i = 0; i < 60; ++i)
if(a & (1LL << i))
ans += (1LL << (i + 1)) - 1;
printf("%lld\n", ans);
}
int main(){
int quest;
scanf("%d", &quest);
while(quest--)
solve();
return 0;
}