TimeLimit: 10000 msCaseTimeLimit: 1000 msMemoryLimit: 256 bytes {} only. Allstringsinthesethavethesamenumberof0sand1s. WriteaprogramtofindandoutputtheK
Time Limit: 10000 ms Case Time Limit: 1000 ms Memory Limit: 256 MB Description Consider a string set that each of them consists of {0, 1} only. all strings in the set have the same number of 0 s and 1 s. write a program to find and output the K
Time Limit: 10000 ms
Case Time Limit: 1000 ms
Memory Limit: 256 MB
Description
Consider a string set that each of them consists of {0, 1} only. all strings in the set have the same number of 0 s and 1 s. write a program to find and output the K-th string according to the dictionary order. if s? Uch a string doesn' t exist, or the input is not valid, please output "Impossible ". for example, if we have two '0' s and two '1' s, we will have a set with 6 different strings, {0011,010 1, 0110,100 1, 1010,110 0 }, the 4th string is 1001.
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10000), the number of test cases, followed by the input data for each test case.
Each test case is 3 integers separated by blank space: N, M (2 <= N + M <= 33 and N, M> = 0 ), K (1 <= K <= 1000000000 ). N stands for the number of '0' s, M stands for the number of '1' s, and K stands for the K-th of string in the set that needs to be printed as output.
Output
For each case, print exactly one line. If the string exists, please print it, otherwise print "Impossible ".
Sample In
3
2 2 2
2 2 7
4 7 47
Sample Out
0101
Impossible
01010111011
Question Analysis:
The meaning of the question is that all n 0 and m 1 are arranged in a combination and arranged in ascending order, and the k binary representation is found and output.
Well, we can combine them and arrange them in the ascending decimal order. Then we can find the k Data and convert it to binary output. However, the workload is quite large, and we also need to sort them in binary format when combining and arranging them. So how do we arrange them? First, try the examples in the question to see if you can find any rule. (Next we will see that mathematics is a wonderful thing. The Code will be very simple after the analysis is complete)
The permutation and combination are written as CEN. For two 0 s and two 1 s: 0011,010 1, 0110,100 1, 1010,110 0.
Category 1: the first data is 0011, minimum, corresponding to n zeros + m 1;
Category 2: when the second and third data are used, the value of 1 increases and the value of 0 decreases. At this time, a 0 and 1 have two combinations (c21)
Category 3: the fourth to sixth data, the highest bit 1 raises the first bit to the highest bit of the binary, then the last two 0 and one 1 have three combinations (c32)
Similarly, we can combine two 0 s and three 1 s into ten numbers: 00111,01011, 01101,01110, 10011,10101, 10110,11001, 11010,11100
Category 1: n zero + m 1--00111
Class 2: The highest digit 1 enters one digit, followed by 1 and 1 0 on the other -- 01011,01101, 01110 (c31)
Category 3: The highest digit 1 goes one more bit, followed by 1 and 2 0 s in full order-10011,10101, 10110,11001, 11010,11100 (c42)
Through these two examples, we can see the following rule:
1 + c M-1 + 1) 1 + c (S-1 + 2) 1 +... + M-1 + I) I +... + c (S-1 + n) n
That is, the maximum bit 1 is constantly increasing, the number of 0 is constantly increasing, and the number of combinations is naturally increasing.
By expanding the number of combinations, the relationship between the I-c (S-1 + I) I and c (S-1 + i-1) (I + 1) is: c (S-1 + I + 1) (I + 1) = c (m-1 + I) I * (m-1 + I + 1)/(I + 1 );
Through the above analysis, it is not difficult to see that the recursive form can be used to make 1 continuously improve until the number of all combinations exceeds k, k must be in c (S-1 + I) if n-I 0 appears in I, it can be determined that n-I 0 is at the beginning, followed by 1, followed by a combination of I and M-1! Then recursively determine the relationship between the remaining 0 and 1! Note that if 1 +... + c (S-1 + I) I = k; then we can determine that I is at the end, and m-1 is at the front.
The Code is as follows:
# Include
# Include
Int FindKthString (int arr [], int n, int m, int k); int main (void) {int n, m, k; while (1) {printf ("\ nplease input n, m, k:"); scanf ("% d", & n, & m, & k ); int * arr = (int *) malloc (n + m) * sizeof (int); int result = FindKthString (arr, n, m, k); if (! Result) printf ("impossible"); else {for (int I = 0; I <n + m; ++ I) printf ("% d ", arr [I]);} free (arr);} return 0;} int FindKthString (int arr [], int n, int m, int k) {int temp = 1, sum = 1; int I; if (k <= 0) // The input k is invalid. return 0; if (k = 1) {// when k is the first value, return the result: All 1 is at the end of the for (int I = 0; I <n + m; ++ I) {// n zeros, m one if (I
= K) // you must add an equal sign to verify that I 0 is successfully searched at the end. Break; sum + = temp;} if (I> n) return 0; // -- I; this sentence cannot be, the for (int j = 0; j <n + m; ++ j) can be matched only in the I zeros. {// divide the data into n-I zeros, followed by 1, then I have zero M-1 j <n-I) arr [j] = 0; else if (j = n-I) arr [j] = 1; else if (j <n + 1) arr [j] = 0; else arr [j] = 1 ;} arr = arr + n-I + 1; // adjust the pointer, pointing to the place after the highest bit 1 if (sum + temp = k) {// if it is k, then the last I bit is all 0 for (int j = 0; j <m + I-1; ++ j) {if (j <s-1) arr [j] = 1; else arr [j] = 0;} return 1;} int result = FindKthString (arr, I m-1, k-sum); // recursively solve if (! Result) return 0; return 1 ;}
Conclusion: we can see from this question how wonderful mathematics is and how complicated the problem is to be solved through an expression. The Code is also very simple! If any problem occurs, try to export the expression.