Class Solution
Hard
You are given two integers n and k.
For any positive integer x, define the following sequence:
p0 = xpi+1 = popcount(pi)for alli >= 0, wherepopcount(y)is the number of set bits (1’s) in the binary representation ofy.
This sequence will eventually reach the value 1.
The popcount-depth of x is defined as the smallest integer d >= 0 such that pd = 1.
For example, if x = 7 (binary representation "111"). Then, the sequence is: 7 \u2192 3 \u2192 2 \u2192 1, so the popcount-depth of 7 is 3.
Your task is to determine the number of integers in the range [1, n] whose popcount-depth is exactly equal to k.
Return the number of such integers.
Example 1:
Input: n = 4, k = 1
Output: 2
Explanation:
The following integers in the range [1, 4] have popcount-depth exactly equal to 1:
| x | Binary | Sequence |
|---|---|---|
| 2 | "10" | 2 \u2192 1 |
| 4 | "100" | 4 \u2192 1 |
Thus, the answer is 2.
Example 2:
Input: n = 7, k = 2
Output: 3
Explanation:
The following integers in the range [1, 7] have popcount-depth exactly equal to 2:
| x | Binary | Sequence |
|---|---|---|
| 3 | "11" | 3 \u2192 2 \u2192 1 |
| 5 | "101" | 5 \u2192 2 \u2192 1 |
| 6 | "110" | 6 \u2192 2 \u2192 1 |
Thus, the answer is 3.
Constraints:
1 <= n <= 10150 <= k <= 5
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
popcountDepth
public long popcountDepth(long tillNumber, int depthQuery)
-