Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
EndlessCheng committed Feb 21, 2023
1 parent 141b9e4 commit 840084b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions copypasta/bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

/*
有关二进制枚举、枚举子集的子集、枚举大小固定集合等写法,见 search.go
标准库 "math/bits" 包含了位运算常用的函数,如二进制中 1 的个数、二进制表示的长度等
注意:bits.Len(0) 返回的是 0 而不是 1
bits.Len(x) 相当于 int(Log2(x)+eps)+1
Expand Down
4 changes: 4 additions & 0 deletions copypasta/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,10 @@ func _(min, max func(int, int) int) {

// 枚举 set 的全部子集
// 作为结束条件,处理完 0 之后,会有 -1&set == set
//
// 你可能会好奇,为什么 sub = (sub - 1) & set 这样写一定可以「跳到」下一个子集呢?会不会漏呢?
// 因为二进制的减法的特点是,每次会把 lowbit 那个 1 改成 0,lowbit 右边的 0 全部改成 1
// 由于下一个子集必然比 sub 小,减法的这种特点可以保证 sub-1 之后的二进制数必然包含下一个子集
loopSubset := func(n, set int) {
// 所有子集
for sub, ok := set, true; ok; ok = sub != set {
Expand Down

0 comments on commit 840084b

Please sign in to comment.