The Great XOR (HackerRank the-great-xor)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Waves___/article/details/70643526
https://vjudge.net/problem/HackerRank-the-great-xor

题: 给一个数x,问存在多少个a
    满足 a ^ x > x 且  0 < a < x
    (^为异或)

对于x的二进制表示,bn......b3b2b1b0,
枚举每一个bi,如果bi为0,那么只要把bi变成1,

然后比bi低的位变成什么都是可以的,对答案的贡献是2^i,把贡献捡起来就行 



#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <set>
#define pi acos(-1.0)
#define LL long long
#define ULL unsigned long long
#define inf 0x3f3f3f3f
#define INF 1e18
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef pair<int, int> P;
const double eps = 1e-10;
const int maxn = 1e6 + 5;
const int N = 1e4 + 5;
const int mod = 1e8;
 
LL p[66]; 
int main(void)
{
//	freopen("in.txt", "r", stdin);
	int T;
	LL n;
	p[0] = 1;
	for (int i = 1; i <= 63; i++)
		p[i] = p[i-1] * 2; 
	cin >> T;
	while (T--){
		cin >> n; 
		int dex = -1;
		LL ans = 0;
		while (n){
			dex++; 
			if ((n & 1) == 0)
				ans += p[dex]; 
			n /= 2;
		}
		cout << ans << endl;
	}

	return 0;
}


猜你喜欢

转载自blog.csdn.net/Waves___/article/details/70643526
xor