注:CV的, 也可以自行去查找题解
按照题意模拟即可,每次取最高位可以不断用x−=lowbit(x)x-=lowbit(x)x−=lowbit(x)减去最低位,直到剩下最高位 每次操作是logxlog xlogx的,最多操作logxlogxlogx次,因为位运算常数小,所以可以通过
#include#define lowbit(x) (x & -x) using namespace std; int t, n; int main() { scanf("%d", &t); while(t --) { scanf("%d", &n); int ans = 0; while(n) { int x = n, gs = 0; int ha = lowbit(x), haa = 0; while(x) { if(x == lowbit(x)) haa = lowbit(x); x -= lowbit(x); gs ++; } if(gs & 1) n ^= 1; else n -= haa; ans ++; } printf("%dn", ans); } return 0; }



