前言
其实我考场上并没过,因为我不会拼写__builtin_popcount。。。
题目大意
给定两个数,求这两个数的区间里个数满足用二进制表示中1的个数为奇数的和。
题目思路
很容易就能想出一种暴力的写法:我们都对每个数进行依次进行二进制分解统计个数判断再累加即可。
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int l, r;
cin >> l >> r;
int ans = 0;
for(int i = l; i <= r; i ++) {
if(__builtin_popcount(i) % 2 == 1) {
ans += i;
}
}
cout << ans;
return 0;
}