当前位置: 首页 > 工具软件 > Flip! > 使用案例 >

Flip the Bits

西门经国
2023-12-01

You are given a positive integer n. Your task is to build a number m by flipping the minimum number of bits in the binary representation of nsuch that m is less than n (m < n) and it is as maximal as possible. Can you?

Input

The first line contains an integer T (1 ≤ T ≤ 105) specifying the number of test cases.

Each test case consists of a single line containing one integer n (1 ≤ n ≤ 109), as described in the statement above.

Output

For each test case, print a single line containing the minimum number of bits you need to flip in the binary representation of n to build the number m.

Example

input

Copy

2
5
10

output

Copy

1
2

题解:就是计算n异或n-1的1的个数,看看变化几次,然后学到一个新函数__builtin_popcount(n^(n-1))来计算二进制中1的个数。

代码如下:

#include <iostream>
#include <cstdio>
#include <stdlib.h>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <ctime>
#define maxn 1007
#define N 100005
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define eps 0.000000001
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define Debug(x) cout<<x<<" "<<endl
using namespace std;
typedef long long ll;

int n;
int main()
{
    int t;
    cin>>t;
    while (t--)
    {
        cin>>n;
        cout<<__builtin_popcount(n^(n-1))<<endl;//取异或
    }
    return 0;
}

 

 类似资料:

相关阅读

相关文章

相关问答