Stringsobits
[Description
Consider sorting out the n (n <= 31) bits.
You will find it interesting. Because they are arranged and contain all the numbers that may be n and contain 1 less than or equal to L (L <= N.
Your task outputs a large number of I (1 <= I <= the number of binary numbers whose length is N) and the length is N, and the binary number containing 1 is less than or equal to L.
Format
Program name: Kimbits
Input Format:
(File kimbits. In)
A total of three integers, n, l, And I, separated by spaces.
Output Format:
(File kimbits. out)
Output the I-largest binary number that meets the condition in a row.
Sample Input
5 3 19
Sample output
10011 analysis: this is a classic DP + structure ~~~ At first, I thought about brute force judgment O (logn * n), and enumerated N * to determine the number of logns in each number 1. This apparently timed-out algorithm should not think about it next time = !... (Calculate the complexity before thinking about it !) Then consider DP ~~~Calculate with dynamic planning: the 01 string with the length of I, the number of 1 is not greater than the number of J
Its status is f [I, j]
Equation: F [I, j] = f [I-1, J] + F [I-1, J-1]; // two conditions for adding 0 and 1 to the current BIT, respectively
Border: F [I, 0] = 1, F [0, I] = 1;
Then it is constructed with a string (Note: K here is the I in the original question)
For the first time, look for the highest position of 1 and start scanning from the second High Position (I: = n-1 downto 1) to see if the number of 1 from the current bit to the last has exceeded L, compare the sizes of F [I, L] and K. If f [I, L]> = K is not 1 (because
Parts of the first K are composed of the rear I-1 01 string), to S to add a '0' complement high; otherwise, find the first f [I, l] <K (that is, only the current I-bit cannot constitute at least K 01 strings meeting the conditions), which is
I + 1 must add a '1', then Dec (K, F [I, L]), Dec (l)
Then, construct the following '1' according to the same method. All the deficiencies are supplemented by '0 '.
PS: This algorithm standardization should beKanto expand~~~ O. O...
Code:
/* ID: 138_3531lang: C ++ task: kimbits */# include <fstream> # include <iostream> # include <cstdio> # include <cstdlib> # include <cmath> # include <iomanip> # include <climits> # include <vector> # include <set> # include <bitset> # include <map> # include <algorithm> # include <string> # include <cstring> using namespace STD; int max (int A, int B) {return A> B? A: B;} int min (int A, int B) {return A> B? B: A;} ifstream fin ("kimbits. in "); ofstream fout (" kimbits. out "); int A [33]; long f [33] [33]; int main () {long n, l, I; fin> N> L> I; memset (F, 0, sizeof (f); For (INT I = 0; I <= N; I ++) // initialize array f {f [0] [I] = 1; F [I] [0] = 1;} For (INT I = 1; I <= N; I ++) // preprocess array F for (Int J = 1; j <= L; j ++) {f [I] [J] = f [I-1] [J] + F [I-1] [J-1]; // cout <I <''<j <'' <F [I] [J] <Endl;} int K = L; memset (, 0, sizeof (a); For (INT I = n-1; I> = 0; I --) {If (F [I] [k]> = I) A [I + 1] = 0; else {A [I + 1] = 1; I-= f [I] [k]; k --;}} for (INT I = N; I> = 1; I --) fout <A [I]; fout <Endl; return 0 ;}