Funny Bits
View as PDFFunny Bits
Problem Statement
Let  be the average (rounded down) of all positive integers up to 
-bits with 
 bits set.
(A bit is "set" if it equals one. For example, in the binary number 1101, there is a total of 3 bits set.)
Given  and 
, find 
.
Input
Your only line of input will contain two space-separated integers,  and 
.
Output
One line, containing only the number .
Constraints
For all test cases:
- There will always be a unique valid 
for the given
and
.
 
Sample Test Cases
Example 1
Input
4 7
Output
2
Explanation
If we let , then there are a total of 
 integers (3, 5, 6, 9, 10, 12) that can be represented with at most 
 bits with exactly 
 bits set:
3 = 0011
5 = 0101
6 = 0110
9 = 1001
10 = 1010
12 = 1100
The average of these numbers is , which rounds down to 
. Thus, 
.
Example 2
Input
3 2
Output
1
Explanation
If we let , then there are a total of 
 integers (1, 2, 4) that can be represented with at most 
 bits with exactly 
 bit set:
1 = 001
2 = 010
4 = 100
The average of these numbers is , which rounds down to 
. Thus, 
.
Example 3
Input
7 90
Output
5
Explanation
If we let , then there are a total of 
 integers that can be represented with at most 
 bits with exactly 
 bits set. These numbers sum to 
 and averages to 
, which rounds down to 
. Thus, 
.
Comments