题解
2025-11-28 07:16:16
发布于:湖南
12阅读
0回复
0点赞
#include <iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); // 加速输入(应对q=1e5的情况)
unsigned int n; // 用unsigned避免符号位问题,n<2^32
int q;
cin >> n >> q;
long long total = 0; // 存储x的总和
while (q--) {
int k;
cin >> k;
int bit_pos = k - 1; // 从右往左第k位,对应二进制的bit_pos位(0开始)
unsigned int mask = 1U << bit_pos; // 构造第bit_pos位为1的掩码
if ((n & mask) == 0) { // 若第k位当前是0
// 最小x = (mask) - (n & (mask - 1))
unsigned int x = mask - (n & (mask - 1));
total += x;
n += x;
}
// 若第k位已经是1,x=0,无需操作
}
cout << total << endl;
return 0;
}
这里空空如也




有帮助,赞一个