← 返回 sofi 的题目列表Count Same-Bit Numbers
类型:qbank
OA combinatorics problem: for `n`, count integers up to the next all-ones bitmask that have the same number of set bits as `n`, excluding `n` itself.
Requirements
For any positive integer n, define f(n) as the smallest integer greater than or equal to n in which all bits are set to 1.
Given a positive integer n, count integers other than n that are <= f(n) and have the same number of set bits as n.
Meet the stated complexity requirement.
Bitwise reasoning plus combinatorics passed the reported OA.
Notes
Convert f(n) into the all-ones mask at n's bit length.
The count is a combinations problem over bit positions, then subtract the original n.