mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-04 15:01:40 +08:00
105 lines
3.6 KiB
HTML
105 lines
3.6 KiB
HTML
<p>You are given two integers <code>n</code> and <code>k</code>.</p>
|
|
|
|
<p>For any positive integer <code>x</code>, define the following sequence:</p>
|
|
|
|
<ul>
|
|
<li><code>p<sub>0</sub> = x</code></li>
|
|
<li><code>p<sub>i+1</sub> = popcount(p<sub>i</sub>)</code> for all <code>i >= 0</code>, where <code>popcount(y)</code> is the number of set bits (1's) in the binary representation of <code>y</code>.</li>
|
|
</ul>
|
|
|
|
<p>This sequence will eventually reach the value 1.</p>
|
|
|
|
<p>The <strong>popcount-depth</strong> of <code>x</code> is defined as the <strong>smallest</strong> integer <code>d >= 0</code> such that <code>p<sub>d</sub> = 1</code>.</p>
|
|
|
|
<p>For example, if <code>x = 7</code> (binary representation <code>"111"</code>). Then, the sequence is: <code>7 → 3 → 2 → 1</code>, so the popcount-depth of 7 is 3.</p>
|
|
|
|
<p>Your task is to determine the number of integers in the range <code>[1, n]</code> whose popcount-depth is <strong>exactly</strong> equal to <code>k</code>.</p>
|
|
|
|
<p>Return the number of such integers.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">n = 4, k = 1</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<p>The following integers in the range <code>[1, 4]</code> have popcount-depth exactly equal to 1:</p>
|
|
|
|
<table style="border: 1px solid black;">
|
|
<thead>
|
|
<tr>
|
|
<th align="center" style="border: 1px solid black;">x</th>
|
|
<th align="center" style="border: 1px solid black;">Binary</th>
|
|
<th align="left" style="border: 1px solid black;">Sequence</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td align="center" style="border: 1px solid black;">2</td>
|
|
<td align="center" style="border: 1px solid black;"><code>"10"</code></td>
|
|
<td align="left" style="border: 1px solid black;"><code>2 → 1</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td align="center" style="border: 1px solid black;">4</td>
|
|
<td align="center" style="border: 1px solid black;"><code>"100"</code></td>
|
|
<td align="left" style="border: 1px solid black;"><code>4 → 1</code></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<p>Thus, the answer is 2.</p>
|
|
</div>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<div class="example-block">
|
|
<p><strong>Input:</strong> <span class="example-io">n = 7, k = 2</span></p>
|
|
|
|
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
|
|
|
<p><strong>Explanation:</strong></p>
|
|
|
|
<p>The following integers in the range <code>[1, 7]</code> have popcount-depth exactly equal to 2:</p>
|
|
|
|
<table style="border: 1px solid black;">
|
|
<thead>
|
|
<tr>
|
|
<th style="border: 1px solid black;">x</th>
|
|
<th style="border: 1px solid black;">Binary</th>
|
|
<th style="border: 1px solid black;">Sequence</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr>
|
|
<td style="border: 1px solid black;">3</td>
|
|
<td style="border: 1px solid black;"><code>"11"</code></td>
|
|
<td style="border: 1px solid black;"><code>3 → 2 → 1</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td style="border: 1px solid black;">5</td>
|
|
<td style="border: 1px solid black;"><code>"101"</code></td>
|
|
<td style="border: 1px solid black;"><code>5 → 2 → 1</code></td>
|
|
</tr>
|
|
<tr>
|
|
<td style="border: 1px solid black;">6</td>
|
|
<td style="border: 1px solid black;"><code>"110"</code></td>
|
|
<td style="border: 1px solid black;"><code>6 → 2 → 1</code></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
|
|
<p>Thus, the answer is 3.</p>
|
|
</div>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= n <= 10<sup>15</sup></code></li>
|
|
<li><code>0 <= k <= 5</code></li>
|
|
</ul>
|