Codeforces647div2C
原题:
The last contest held on Johnny’s favorite competitive programming platform has been received rather positively. However, Johnny’s rating has dropped again! He thinks that the presented tasks are lovely, but don’t show the truth about competitors’ skills.
The boy is now looking at the ratings of consecutive participants written in a binary system. He thinks that the more such ratings differ, the more unfair is that such people are next to each other. He defines the difference between two numbers as the number of bit positions, where one number has zero, and another has one (we suppose that numbers are padded with leading zeros to the same length). For example, the difference of 5=10125=1012 and 14=1110214=11102 equals to 33, since 01010101 and 11101110 differ in 33 positions. Johnny defines the unfairness of the contest as the sum of such differences counted for neighboring participants.
Johnny has just sent you the rating sequence and wants you to find the unfairness of the competition. You have noticed that you’ve got a sequence of consecutive integers from 00 to nn. That’s strange, but the boy stubbornly says that everything is right. So help him and find the desired unfairness for received numbers.
Input
The input consists of multiple test cases. The first line contains one integer tt (1≤t≤100001≤t≤10000) — the number of test cases. The following tt lines contain a description of test cases.
The first and only line in each test case contains a single integer nn (1≤n≤1018)1≤n≤1018).
Output
Output tt lines. For each test case, you should output a single line with one integer — the unfairness of the contest if the rating sequence equals to 00, 11, …, n−1n−1, nn.
思路:规律,见代码
ACcodes:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
int test=1;
int BigLength(long long n)
{
int c=0;
while(n)
{
c++;
n>>=1;
}
return c;
}
int func(long long n)
{
int cnt=0;
while(n)
{
n = n&(n-1);
cnt++;
}
return cnt;
}
int main()
{
scanf("%d",&test);
while(test--)
{
long long n;cin>>n;
cout << 2*n-func(n) << endl;
}
//system("pause");
return 0;
}