Array
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 729 Accepted Submission(s): 347
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.
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)
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
3 1 3 5
Sample Output
1 4 7
Source
Recommend
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int N = 105;
LL num[N],sum[N];
void init(){
num[1]=1;
for(int i=2;i<=63;i++){
num[i]=num[i-1]*2+1;
}
sum[1]=1;
for(int i=2;i<=63;i++){
sum[i]=sum[i-1]*2+num[i-1]+1;
}
}
int main(){
int T;
scanf("%d",&T);
LL n;
init();
while(T--){
scanf("%lld",&n);
LL ans=0,count=0;
while(n){
int p=1;
while(num[p]<=n){
p++;
}
p--;
ans+=sum[p]+num[p]*count;
n-=num[p];
if(n!=0){
count++;
ans+=count;
n--;
}
}
printf("%lld\n",ans);
}
return 0;
}