3.8课程题目解析+总结
xiangrui0305 · · 个人记录
3.8课程题目解析+总结
知识普及:位运算
1.自然溢出
unsigned是无符号整数变量,自带一个取模
unsigned int相当于给一个数模unsigned long long相当于给一个数模
未定义行为:Underfined Behavior,简称UB
2.获取x 变成二进制之中1 的数量
短除法:时间复杂度为
int cnt = 0;
while(n){
cnt += n % 2;
n /= 2;
}
cout<<cnt<<endl;
系统自带函数:时间复杂度接近
int cnt = __builtin_popcount(n); //int范围取值
long long ans = __builtin_popcountll(n); //longlong范围取值
cout<<cnt<<endl;
3.获取x 的二进制最低位
lowbit(x) = x & (-x)
4.位运算的基础操作
前置知识:获取二进制的第
j = (x >> i) & 1
操作1:让
x = (~(1 << i)) & x
操作2:让
x = (1 << i) | x
操作3:让
x = (1 << i) ^ x
题目考查知识点汇总
| 题号 | T739864 | T739863 | B3919 | P9451 | T739897 |
|---|---|---|---|---|---|
| 题目名 | 位运算基础试题1 | 位运算基础试题2 | 二进制与一 | 新概念报数 | 位运算进阶1 |
| 题目难度 | |||||
| 题目考察知识点 | 位运算 | 位运算 | 位运算+循环结构 | 位运算 | 位运算 |
题目解析+总结
T739864 位运算基础试题1
题目解析
我们要找出第一个出现的1,就可以让cnt不断地枚举1 << cnt,位运算的情况下记得打括号)。并没必要写快速幂(
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin>>n;
int cnt = 0;
for(int i=1;i<=n;i*=2){
if((n >> cnt) & 1){
cout<<(1<<cnt)<<endl;
return 0;
}
cnt++;
}
return 0;
}
T739863 位运算基础试题2
题目解析
这道题是知识普及的第四点,只要根据题目要求来套代码即可。
操作1:让
(~(1 << i)) & x
操作2:让
(1 << i) | x
操作3:让
(1 << i) ^ x
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int x,t;
cin>>x>>t;
while(t--){
int op,pos;
cin>>op>>pos;
int tmp = (x >> pos) & 1;
if(op == 1){
if(tmp == 1) x &= (~(1ll<<pos));
}
else if(op == 2){
if(tmp == 0) x |= (1ll<<pos);
}
else x ^= (1ll << pos);
cout<<x<<endl;
}
return 0;
}
B3919 [语言月赛 202401] 二进制与一
题目解析
若要让答案最小,最好情况下就是在满足要求的情况下,第
我们先制造新数
此时,原数
将每次询问的值加起来,就是本题答案。
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,q,ans = 0;
cin>>n>>q;
while(q--){
int k; cin>>k;
if((n >> (k - 1ll)) & 1ll) continue;
int Y = (n >> (k - 1ll));
int X = (Y + 1) << (k - 1ll);
int x = X - n;
n = X,ans += x;
}
cout<<ans<<endl;
return 0;
}
P9451 [ZSHOI-R1] 新概念报数
题目解析
我们可以直接使用知识普及的第二点,直接判断这个数是否满足条件。
我们对于满足条件的情况只需要分成两个点,构造出满足条件的下一个数字:
-
1$的数量为$1$个:很简单,直接在后面加个$1 -
注:数字可能会达到unsined long long,ull才能把unsined long long类型!
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t;
cin>>t;
while(t--){
int a;
cin>>a;
int cnt = __builtin_popcountll(a);
if(cnt >= 3) cout<<"No,Commander\n";
else if(cnt == 2){
int b = a & (-a);
cout<<a + b<<endl;
}
else cout<<a + 1ull<<endl;
}
return 0;
}
T739897 位运算进阶1
题目解析
我们可以拆分这个公式:
我们可以发现:
- 若
i = 1 ,则自己包围的出现次数为3 ,被其他情况包围了0 次。 - 若
i = 2 ,则自己包围的出现次数为2 ,而被i = 1 的情况包围了2 次。 - 若
i = 3 ,则自己包围的出现次数为1 ,而被i = 1 、i = 2 的情况包围了2 - 第
i 个数自己包围的出现次数为r-l+1-i ,而被上面的情况包围了(i-l) \times (r-l+1-i) 次。
我们可以推导出公式:
注:
则第
我们直接从
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[5005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,q;
cin>>n>>q;
for(int i=1;i<=n;++i) cin>>a[i];
while(q--){
int op,x,y;
cin>>op>>x>>y;
if(op == 1) a[x] = y;
else{
int ans = 0;
for(int i=x;i<=y;++i){
int tmp = (i - x + 1) * (y - i + 1);
if(tmp % 2) ans ^= a[i];
}
cout<<ans<<endl;
}
}
return 0;
}