来追梦2026春季周五位运算专题进阶总结
题目总结
今日的位运算专题,做的不太理想满分500分,只打了96分,可能是考试时状态不太好导致的。其实前三题都比较水。接下来总结一下每道题的思路及代码。
题单:
P7071 [CSP-J 2020] 优秀的拆分
P1461 [USACO2.1] 海明码 Hamming Codes
P13591 [NWRRC 2023] Kitchen Timer
P11485 「Cfz Round 5」Non-breath Oblige
P10307 「Cfz Round 2」Binary
总结:
P7071 [CSP-J 2020] 优秀的拆分
错因:
★★★
思路:
代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, b, d, cnt = 1, idx[70];
int hm(int x, int y){
int cc1 = 0;
for(int i = 60; i >= 0; i--){
int p = x >> i & 1;
int q = y >> i & 1;
cc1 += (p != q);
}
return cc1;
}
signed main(){
cin >> n >> b >> d;
cout << 0 << ' ';
for(int i = 1; ; i++){
bool flag = true;
for(int j = 1; j <= cnt; j++)
if(hm(idx[j], i) < d) flag = false;
if(flag){
idx[++cnt] = i;
cout << idx[cnt] << ' ';
if(cnt % 10 == 0) cout << '\n';
if(cnt == n) return 0;
}
}
return 0;
}
P13591 [NWRRC 2023] Kitchen Timer
错因:
没有抓住做题的关键。
思路:
如果要让按暂停键的次数最少,那么就要让连续按按钮的次数最多。如果连续按了(1ll << 63) - 1开始遍历,若
代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int t, x;
void solve(){
cin >> x;
int k = (1ll << 63) - 1, ans = 0;
while(x != 0){
while(x >= k){
x -= k;
ans++;
}
k /= 2;
}
cout << ans - 1 << '\n';
}
signed main(){
cin >> t;
while(t--)
solve();
return 0;
}
P11485 「Cfz Round 5」Non-breath Oblige
错因:
没想到思路。
思路:
首先,如果一开始
然后排除特殊情况,也就是
(s ^ ((1 << n) - 1)) + (t ^ ((1 << n) - 1))
证明:
如果
代码:
#include <bits/stdc++.h>
using namespace std;
int T, n, s, t;
void solve(){
cin >> n >> s >> t;
if(s == t) cout << "0\n";
else cout << (s ^ ((1 << n) - 1)) + (t ^ ((1 << n) - 1)) << '\n';
}
int main(){
cin >> T;
while(T--)
solve();
return 0;
}