#P8764. Counting Numbers with Exactly K Ones in Binary
Counting Numbers with Exactly K Ones in Binary
Counting Numbers with Exactly K Ones in Binary
Little Blue is learning binary numbers. He is curious about how many numbers between \(1\) and \(N\) inclusive have exactly \(K\) ones in their binary representation. Can you help him?
Task: Given two integers \(N\) and \(K\), count the numbers from \(1\) to \(N\) whose binary representation contains exactly \(K\) ones.
Note: If a number's binary representation includes leading zeros, they are not counted. For example, the binary representation of \(3\) is 11
which has two ones, so if \(K = 2\), \(3\) should be counted.
inputFormat
The input consists of a single line containing two space-separated integers \(N\) and \(K\). For example:10 2
outputFormat
Output a single integer which is the count of numbers between \(1\) and \(N\) (inclusive) that have exactly \(K\) ones in their binary representation.
sample
10 2
5