Description
Description
Editorial
Editorial
Solutions
Solutions
Submissions
Submissions
Medium

You are given an integer k and an integer x. The price of a number num is calculated by the count of at positions x, 2x, 3x, etc., in its binary representation, starting from the least significant bit. The following table contains examples of how price is calculated.

xnumBinary RepresentationPrice
1130000011013
2130000011011
22330111010013
3130000011011
33621011010102

The accumulated price of num is the total price of numbers from 1 to num. num is considered cheap if its accumulated price is less than or equal to k.

Return the greatest cheap number.

 

Example 1:

Input: k = 9, x = 1

Output: 6

Explanation:

As shown in the table below, 6 is the greatest cheap number.

xnumBinary RepresentationPriceAccumulated Price
1100111
1201012
1301124
1410015
1510127
1611029
17111312

Example 2:

Input: k = 7, x = 2

Output: 9

Explanation:

As shown in the table below, 9 is the greatest cheap number.

xnumBinary RepresentationPriceAccumulated Price
21000100
22001011
23001112
24010002
25010102
26011013
27011114
28100015
29100116
210101028

 

Constraints:

  • 1 <= k <= 1015
  • 1 <= x <= 8
 
Code
Code
Testcase
Testcase
Test Result
Test Result