3867: Cow IDs
Total Submit: 41 Accepted:13
Description
Being a secret computer geek, Farmer John labels all of his cows with binary numbers. However, he is a bit superstitious, and only labels cows with binary numbers that have exactly K "1" bits (1 <= K <= 10). The leading bit of each label is always a "1" bit, of course. FJ assigns labels in increasing numeric order, starting from the smallest possible valid label -- a K-bit number consisting of all "1" bits. Unfortunately, he loses track of his labeling and needs your help: please determine the Nth label he should assign (1 <= N <= 10^7).
Input
* Line 1: Two space-separated integers, N and K.
Output
*Line 1: One number in binary form.
Sample Input
7 3
Sample Output
10110
Hint
INPUT DETAILS:
Among all binary numbers containing exactly 3 "1" bits, FJ wants to output the 7th in increasing sorted order.
Source
分析:用s[i][j]表示用i + 1个1和不超过j个0最多产生多少个数,那么lower_bound(s[k - 1], s[k - 1] + len, n)就是第n个数含有的0的个数,再算出剩下部分(从第2个1开始的部分)有多少个0,它们的差就是前两个1之间0的个数,然后递归剩下以1开头的部分。
#include
#include
#include
#include
#include
#include