#P3048. Cow Labeling with K Ones
Cow Labeling with K Ones
Cow Labeling with K Ones
Farmer John assigns labels to his cows using binary numbers. Each label must have exactly \(K\) one bits (\(1 \le K \le 10\)) and the label must always start with a 1. Labels are assigned in increasing numerical order, and the first label is the smallest valid label, which is the \(K\)-bit number consisting of all ones.
Your task is to determine the \(N\)th label that Farmer John should assign. Formally, given two positive integers \(K\) and \(N\) (\(1 \le N \le 10^7\)), you need to output the binary representation of the \(N\)th number whose binary representation has exactly \(K\) ones and begins with a 1.
The problem can be solved by first determining the bit-length \(L\) of the \(N\)th valid label. For any given bit-length \(L\) (with \(L \ge K\)), the count of valid numbers is given by the binomial coefficient: \(C(L-1, K-1)\). This is because the first bit is fixed as 1, and we choose \(K-1\) positions among the remaining \(L-1\) bits to place ones. Once the correct length \(L\) is found such that the cumulative count of valid labels reaches or exceeds \(N\), you then construct the binary string of length \(L\) by deciding, for each subsequent position, whether to place a '0' or a '1' based on how many completions exist if you choose '0' at that position.
Input Constraints:
\(1 \le K \le 10\) and \(1 \le N \le 10^7\).
inputFormat
The input consists of two space-separated integers: K
and N
, where K
is the number of ones in the binary label and N
is the index of the label to output.
outputFormat
Output the binary representation of the \(N\)th valid label on a single line.
sample
1 1
1