二进制运算性质
wanghanshi_1999
·
·
个人记录
异或与二进制运算性质总结
一、异或 XOR 的基本性质
1. 基本运算规律
交换律、结合律:
a\oplus b=b\oplus a
(a\oplus b)\oplus c=a\oplus(b\oplus c)
自反性、单位元、消去律:
a\oplus a=0,\qquad a\oplus0=a
a\oplus b=a\oplus c\iff b=c
a\oplus b=c\iff a\oplus c=b
推论: 一个数字出现偶数次,在整体异或中贡献为 0;出现奇数次,贡献为它本身。
2. 前缀异或
定义:
s_i=\bigoplus_{j=1}^{i}a_j,\qquad s_0=0
则区间异或为:
\boxed{\bigoplus_{i=l}^{r}a_i=s_r\oplus s_{l-1}}
若区间异或为 k:
s_r\oplus s_{l-1}=k
\iff s_{l-1}=s_r\oplus k
可以使用哈希表统计异或为 k 的子数组数量。
3. 异或与奇偶性
\boxed{a\oplus b\equiv a+b\pmod2}
推广到多个数字:
\boxed{
\bigoplus_{i=1}^{n}a_i
\equiv\sum_{i=1}^{n}a_i\pmod2
}
原因是异或的最低位等于所有最低位之和模 2。
二、AND、OR、XOR 与加法
以下讨论均针对非负整数,且假设运算过程中不发生整数溢出。
4. 加法与位运算的恒等式
最重要的两个公式:
\boxed{a+b=(a\mid b)+(a\mathbin{\&}b)}
\boxed{a+b=(a\oplus b)+2(a\mathbin{\&}b)}
由此得到:
\boxed{a\oplus b=(a\mid b)-(a\mathbin{\&}b)}
\boxed{a\oplus b=2(a\mid b)-(a+b)}
逐位分析:
| a_i |
b_i |
和 |
OR |
AND |
XOR |
| 0 |
0 |
0 |
0 |
0 |
0 |
| 0 |
1 |
1 |
1 |
0 |
1 |
| 1 |
0 |
1 |
1 |
0 |
1 |
| 1 |
1 |
2 |
1 |
1 |
0 |
因此:
\boxed{a+b=a\oplus b\iff(a\mathbin{\&}b)=0}
即两个数相加没有进位,当且仅当它们的二进制 1 位互不重叠。
5. OR、XOR 与加法的大小关系
\boxed{a\oplus b\le a\mid b\le a+b}
其中:
a\mid b=a+b
\iff a\mathbin{\&}b=0
a\oplus b=a+b
\iff a\mathbin{\&}b=0
并且:
a\oplus b=a\mid b
\iff a\mathbin{\&}b=0
应用: 二进制贪心、最小化求和、无进位构造。
6. 已知和与异或,构造两个数
已知:
a+b=s,\qquad a\oplus b=x
由恒等式:
a+b=(a\oplus b)+2(a\mathbin{\&}b)
可得:
\boxed{a\mathbin{\&}b=\frac{s-x}{2}}
令:
t=\frac{s-x}{2}
存在非负整数解的充要条件为:
\boxed{
\begin{cases}
s\ge x\\
(s-x)\bmod2=0\\
t\mathbin{\&}x=0
\end{cases}
}
其中:
设:
k=\operatorname{popcount}(x)
则非负整数有序解的数量为:
\boxed{2^k}
如果要求 a,b>0,还需要排除包含 0 的解。
三、多个数字的 OR、XOR 与求和
7. XOR 与普通求和
设:
X=\bigoplus_{i=1}^{n}a_i,\qquad
S=\sum_{i=1}^{n}a_i
则:
\boxed{X\le S}
\boxed{X\equiv S\pmod2}
注意:多个数相加时可能产生连续进位,因此不能直接将两个数的 AND 恒等式推广到多个数。
8. 多个数的 OR 与求和
\boxed{
\bigvee_{i=1}^{n}a_i\le\sum_{i=1}^{n}a_i
}
等号成立当且仅当任意二进制位最多出现在一个数中,即:
\boxed{
a_i\mathbin{\&}a_j=0\quad(\forall i\ne j)
}
此时:
\boxed{
\sum_{i=1}^{n}a_i
=
\bigvee_{i=1}^{n}a_i
=
\bigoplus_{i=1}^{n}a_i
}
应用: 二进制拆分、贪心构造、最小和问题。
9. 多个数的 XOR 与 OR 相等
\bigoplus_{i=1}^{n}a_i=\bigvee_{i=1}^{n}a_i
当且仅当每个二进制位的出现次数为 0 或奇数。
原因:
- XOR 判断该位出现次数的奇偶性。
- OR 判断该位是否至少出现一次。
注意:多个数的 XOR 等于 OR,不代表任意两个数的 AND 都为 0。
例如:
1\oplus1\oplus1=1\mid1\mid1=1
四、二进制位的特殊性质
10. 最高不同位决定大小
设:
k=\operatorname{msb}(a\oplus b)
其中 \operatorname{msb} 表示最高置位的位置,且 a\ne b。
则 a,b 在所有高于 k 的位置完全相同,第 k 位不同。
因此:
\boxed{
a>b\iff \text{$a$ 的第 $k$ 位为 $1$}
}
应用: 从高位到低位贪心、二进制 Trie、异或最优化。
11. 低位掩码
\boxed{
2^k-1=\underbrace{11\cdots11}_{k\text{ 个 }1}
}
因此:
\boxed{x\mathbin{\&}(2^k-1)=x\bmod2^k}
\boxed{x\gg k=\left\lfloor\frac{x}{2^k}\right\rfloor}
适用于提取低位、分离高低位、按位分治。
12. lowbit 与最低位的 1
对于正整数 x:
\boxed{\operatorname{lowbit}(x)=x\mathbin{\&}(-x)}
同时:
\boxed{x\mathbin{\&}(x-1)}
会删除 x 最低的一个 1。
因此:
\boxed{
x>0,\quad x\mathbin{\&}(x-1)=0
}
当且仅当 x 是 2 的整数次幂。
13. 连续整数异或
定义:
F(n)=0\oplus1\oplus\cdots\oplus n
则:
\boxed{
F(n)=
\begin{cases}
n,&n\bmod4=0\\
1,&n\bmod4=1\\
n+1,&n\bmod4=2\\
0,&n\bmod4=3
\end{cases}
}
因此:
\boxed{
\bigoplus_{i=l}^{r}i=F(r)\oplus F(l-1)
}
其中规定 F(-1)=0。
五、异或线性空间与线性基
14. XOR 的线性性质
二进制数可以视为 \mathbb F_2 上的向量,XOR 相当于向量加法。
因此可以使用线性基维护一组数字能够异或出的所有值。
设线性基的秩为 r,则不同的可表示异或值数量为:
\boxed{2^r}
对于 n 个数字,异或结果为 0 的子集数量为:
\boxed{2^{n-r}}
其中包含空集,且子集按下标区分。
若某个值 x 可以被线性基表示,则异或结果为 x 的子集数量同样为:
\boxed{2^{n-r}}
否则为 0。
15. 线性基的常见应用
- 最大子集 XOR。
- 第 k 小不同 XOR 值。
- 判断指定 XOR 值能否表示。
- 统计 XOR 为指定值的子集数量。
- 图上路径 XOR 与环空间。
图上路径 XOR:
先通过 DFS 求出根到每个点的树路径异或值,再将非树边产生的环异或值插入线性基。
对于无向图中两个连通点 u,v,任意一条路径的基础异或值可以写成:
dis[u]\oplus dis[v]
再利用环异或线性基优化路径 XOR。
六、补充:常见位运算恒等式
16. 位运算的分配律
AND 对 OR、XOR 满足分配律:
\boxed{
a\mathbin{\&}(b\mid c)
=
(a\mathbin{\&}b)\mid(a\mathbin{\&}c)
}
\boxed{
a\mathbin{\&}(b\oplus c)
=
(a\mathbin{\&}b)\oplus(a\mathbin{\&}c)
}
OR 对 AND 满足分配律:
\boxed{
a\mid(b\mathbin{\&}c)
=
(a\mid b)\mathbin{\&}(a\mid c)
}
但 OR 对 XOR 一般不满足分配律。
17. AND、OR 的吸收律
\boxed{a\mathbin{\&}(a\mid b)=a}
\boxed{a\mid(a\mathbin{\&}b)=a}
18. AND、OR、XOR 的幂等性
\boxed{a\mathbin{\&}a=a}
\boxed{a\mid a=a}
\boxed{a\oplus a=0}
其中 XOR 不满足幂等律,而是具有自反消去性质。
19. XOR 与位取反
在固定 w 位二进制下,设:
M=2^w-1
则:
\boxed{\neg_w a=M\oplus a}
其中 \neg_w a 表示只对低 w 位取反。
由此:
\boxed{a\oplus M=\neg_w a}
应用: 固定位宽下的补集、掩码构造、二进制贪心。
20. 子集位掩码判定
若把非负整数视为二进制集合,则:
\boxed{
(a\mathbin{\&}b)=a
\iff
\text{$a$ 的所有置位均包含于 $b$}
}
等价于:
\boxed{a\mid b=b}
应用: 状压 DP、SOS DP、子集枚举。
七、对应例题
1. CF1556D Take a Guess
题目:https://codeforces.com/problemset/problem/1556/D
2. CF2211D AND-array
题目:https://codeforces.com/problemset/problem/2211/D
3. CF2077B Finding OR Sum
题目:https://codeforces.com/problemset/problem/2077/B
4. CF2165C Binary Wine
题目:https://codeforces.com/problemset/problem/2165/C
5. CF1088D Ehab and another another xor problem
题目:https://codeforces.com/problemset/problem/1088/D
6. CF2247D2 XOR Sorting (Hard Version)
题目:https://codeforces.com/problemset/problem/2247/D2
7. CF2146D2 Max Sum OR (Hard Version)
题目:https://codeforces.com/problemset/problem/2146/D2
8. CF1392E Omkar and Duck
题目:https://codeforces.com/problemset/problem/1392/E
9. CF1658D2 388535 (Hard Version)
题目:https://codeforces.com/problemset/problem/1658/D2
10. CF1625D Binary Spiders
题目:https://codeforces.com/problemset/problem/1625/D
八、核心公式速查
XOR 基本性质
a\oplus a=0
a\oplus b=c\iff a\oplus c=b
AND、OR、XOR 与加法
\boxed{a+b=(a\oplus b)+2(a\mathbin{\&}b)}
\boxed{a+b=(a\mid b)+(a\mathbin{\&}b)}
\boxed{a\oplus b\le a\mid b\le a+b}
无进位条件
\boxed{a+b=a\oplus b\iff(a\mathbin{\&}b)=0}
多个数的无进位条件
\boxed{
\bigvee_{i=1}^{n}a_i=\sum_{i=1}^{n}a_i
\iff
\forall i\ne j,\ a_i\mathbin{\&}a_j=0
}
前缀异或
\boxed{
\bigoplus_{i=l}^{r}a_i=s_r\oplus s_{l-1}
}
最低置位
\boxed{\operatorname{lowbit}(x)=x\mathbin{\&}(-x)}
二进制子集关系
\boxed{
(a\mathbin{\&}b)=a\iff(a\mid b)=b
}
线性基
\boxed{
\text{不同异或值数量}=2^{\operatorname{rank}}
}
九、解题思路总结
二进制题目通常可以从以下几个方向思考:
- 逐位独立: 将整体限制拆分为每一位的限制。
- 最高位贪心: 数字大小由最高不同位决定。
- 加法与进位: 使用 AND、OR、XOR 与加法的恒等式。
- 异或消去: 利用 a\oplus a=0 消除重复贡献。
- 二进制 Trie: 按最高位分类,处理 XOR 最优化。
- 线性基: 处理任意子集异或及路径异或。
- 位运算构造: 利用不同二进制位之间的独立性编码信息。
其中最重要的是:
\boxed{
a+b=(a\oplus b)+2(a\mathbin{\&}b)
}
\boxed{
a+b=(a\mid b)+(a\mathbin{\&}b)
}