ArrayTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 378 Accepted Submission(s): 189
Problem Description
Vicky is a magician who loves math. She has great power in copying and creating.
One day she gets an array {1}。 After that, every day she copies all the numbers in the arrays she has, and puts them into the tail of the array, with a signle '0' to separat. Vicky wants to make difference. So every number which is made today (include the 0) will be plused by one. Vicky wonders after 100 days, what is the sum of the first M numbers.
Input
There are multiple test cases.
First line contains a single integer T, means the number of test cases. (1≤T≤2∗103) Next T line contains, each line contains one interger M. (1≤M≤1016)
Output
For each test case,output the answer in a line.
Sample Input
Sample Output
Source
其实
Ai
为i二进制中1的个数。每次变化
Ak+2i=Ak+1
,
(k<2i)
不产生进位,二进制1的个数加1。然后数位dp统计前m个数二进制1的个数,计算每一位对答案的贡献。只需考虑该位填1,其高位与低位的种数即可。
|
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
long long bit[70];
long long bit1[70];
long long POW(int n){
long long ans=1;
while(n--){
ans*=2;
}
return ans;
}
void dabiao(){
bit[0]=1;bit[1]=2;
for(int i=0;i<=64;++i){
bit1[i]=POW(i);
}
for(int i=2;i<=64;++i){
bit[i]=2*bit[i-1]+bit1[i-1]-1;
}
}
int main()
{
dabiao();
int t;
long long n;
scanf("%d",&t);
while(t--){
scanf("%lld",&n);
long long ans=0,num=0;
while(n){
int pos=0;
while(n>=bit1[pos])pos++;
pos--;
n-=bit1[pos];
ans+=bit[pos]+num*bit1[pos];
num++;
}
printf("%lld\n",ans);
}
return 0;
}