This year, I sent a written question from Microsoft and a written question from Microsoft.

Source: Internet
Author: User

This year, I sent a written question from Microsoft and a written question from Microsoft.

Here we provide oneMicrosoft pen examThe specific questions are as follows:

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 such 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}, and 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

In fact, the meaning is very simple, that is, input N 0, M 1. Then find the K-th big number consisting of M, N 1, 0. If not, output impossible.

At the beginning, we may think that this is a problem of full arrangement, but the problem with full arrangement is that we cannot know exactly the number of each number, in addition, the time must not pass, and everyone should know the efficiency of recursion.

We may think of another solution, listing it directly, from the smallest 000... starting from 1111, it is always listed as 1111 .. 000 and then record whether there are N 0 S and M 1 s. This method is the easiest to understand, but if the number is relatively large, for example, 17, 17 0, we do not mean that such a large integer cannot be saved, it is not cost-effective at this time, although it is linear complexity, this linear number is too large...

OK. I then thought about whether the tree can be traversed and thought about whether or not I wanted to be rejected.

Finally, I came up with a way to get it through constant exchange. We think that if we change from a 1st-plus number to a 2nd-plus number, we must increase this number. How can we increase it? So that the two numbers are the closest, that is to say, we only increase by 1, rather than there are many numbers in the middle?

That is, data is scanned from left to right until 10 is met, and 1 before this 1 is exchanged with 0 of the second bit. A good idea has been completely exposed. Here is an example.

For example, if the number is 5, and the number is 5, the number is 0.

The saved format is 1111100000 (the actual number is 0000011111), which is the smallest number (the output is reversed during the output, which is mainly for ease of understanding ). The maximum number is 0000011111 !)

When we want to increase the number, when J = 5, 0 appears, and j = 4, it is 1, so that they are exchanged, switch between 1 and 0 in the low position before j = 4. If there is no 0 here, there is no need to switch. Get 1111010000 (the actual number is 0000101111 ).

When we see 0011101100 (the actual number is 0011011100), what should we do to increase the number by 1? Obviously, when J = 5 and the J-1 = 4 appeared 0 for 1 to exchange them, and j = 4 before the 1 and the first bit of 0 began to exchange, finally we will get the result: 1100011100. (the actual number is 00111000011 ).

Okay. Here we should have understood it completely and read the algorithm source code: [brainstorm, do you have a better solution?]

  1. // M: The number of 0, the number of N, 1. K outputs the nth number.
  2. Bool test (int N, int M, int K ){
  3. If (calculateTotalNum (M + N, M) <K) // if the actual number is less than k, false is returned, impossible is output.
  4. Return false;
  5. Int L = M + N, count = 1;
  6. Bitset <MaxLength> bit;
  7. Map <int, bitset <MaxLength> mapStr;
  8. For (int I = 0; I <M; I ++) // the minimum number of initial calls, that is, 0 is on the leftmost side, such as 0011.
  9. Bit [I] = 1;
  10. If (K = 1 ){
  11. Print (bit, M + N); // output
  12. Return true;
  13. }
  14. Int j = 0; // indicates that the search has been performed from the low position to the high position. If there is any 01, the exchange will continue.
  15. While (! IsLast (bit, M, N) {// the last number is not found
  16. If (j> = M + N-1)
  17. J = 0; // The highest bit has been found. In this case, you need to search from 0.
  18. If (1 = bit [j] & 0 = bit [j + 1]) {
  19. Int right = J-1, left = 0;
  20. // Search from the first element of J, and move all the previous 1 to the leftmost.
  21. While (right> = 0 & bit [right] = 1 & left <right ){
  22. Bool temp = bit [right];
  23. Bit [right] = bit [left];
  24. Bit [left] = temp;
  25. Right --, left ++;
  26. }
  27. Bit [j] = 0; // exchanges 0, 1
  28. Bit [j + 1] = 1;
  29. Count ++; // The maximum number of statistics.
  30. If (count = K ){
  31. Print (bit, M + N );
  32. Return true;
  33. }
  34. J = 0; // search again
  35. }
  36. Else
  37. J ++;
  38. }
  39. Return true;
  40. }
  41.  
  42. Int calculateTotalNum (int N, int M) {// The total number of combinations. C (M, N) = A (N, N)/(A (M, M) * A (N-M, N-M ))
  43. Int result = 1;
  44. For (int I = 1; I <= N; I ++)
  45. Result * = I;
  46. For (int I = 1; I <= M; I ++)
  47. Result/= I;
  48. For (int I = 1; I <= N-M; I ++)
  49. Result/= I;
  50. Return result;
  51. }
  52. Bool isLast (bitset <MaxLength> bit, int M, int N ){
  53. Int I = 0;
  54. While (I <N & 0 = bit [I])
  55. I ++;
  56. If (I = N)
  57. Return true;
  58. Else
  59. Return false;
  60. }
  61. Void print (bitset <MaxLength> bit, int N ){//
  62. For (int I = N-1; I> = 0; I --)
  63. Cout <bit [I];
  64. Cout <"";
  65. }

 

Attached effect:

[Do Not Disturb] Students provide a different solution, mainly by recursively determining the number of the current 0, 1 combination and comparing it with K.

(Make sure that the total number of combinations generated is greater than K when no value is reduced. Otherwise, return is returned .)

If the number of the current 0 values is reduced by one, and the total number generated is greater than K, the output is 0, and the number of 0 values is reduced by one, K, and 1.

If the number of the current 0 values is reduced by one and the total number is less than K, 1 is output, and 1 is reduced by one. The number of 0 values remains unchanged, and K = K-the current total number.

Recursive call. The final result is displayed.

The Code [do not disturb] has been posted. In the reply message!

[One problem is that yongnan and I both have overflow problems when calculating the total number of times, that is, using Long long Long also overflows, do you have any suggestions on overflow solutions when calculating factorial or combination problems?]

Link: http://www.cnblogs.com/xiaoyi115/p/3696507.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.