Slides! (Large)
Time limit5sMemory limit512 MB
Decide whether a building slide graph can have exactly M paths from building 1 to B, then print the fixed canonical matrix when possible.
- Level
Medium7 of 10
- Topics
- Combinatorics, Bit manipulation, Implementation
- Solved
- No attempts yet
Problem
Gooli is a huge company that owns buildings in a hilly area. The buildings are numbered from 1 to .
The CEO wants to build a set of slides between buildings that she can use to travel from her office in building 1 to her favorite cafe in building . Slides are one-way only, but the buildings are tall and have elevators, so a slide can start in any building and end in any other building, and can go in either direction. Specifically, for any two buildings and , you can build either zero or one slides from to , and you can build either zero or one slides from to . The exception is that no slides are allowed to originate in building , since once the CEO reaches that building, she has no need to slide any further.
In honor of Gooli becoming exactly milliseconds old, the design must give the CEO exactly different ways to travel from building 1 to building using the new slides. A way is a sequence of buildings that starts with building 1, ends with building , and has the property that for each pair of consecutive buildings and in the sequence, a slide exists from to . The CEO does not require that every building be reachable from every other building via slides.
Build a set of one or more slides that satisfies the CEO's requirements, or determine that it is impossible.
Input
The first line of the input gives the number of test cases, . lines follow; each consists of two integers and , as described above.
Output
For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is POSSIBLE if the CEO's requirements can be fulfilled and IMPOSSIBLE otherwise.
If it is possible, output an additional lines containing characters each, representing a matrix of slides. The -th character of the -th of these lines (with both and counting from 1) is 1 if a slide is built from building to building , and 0 otherwise.
So that the answer is unique, the matrix must be built by this rule:
- For every pair , build a slide from building to building . Build no other slides among buildings to .
- If , build a slide from building 1 to each of buildings .
- If , then for each with , build a slide from building 1 to building exactly when the bit of value in the binary representation of is 1.
- Build no other slides. In particular, the -th character of the -th line and every character of the last line are
0.
The matrix built by this rule has exactly ways from building 1 to building .
Constraints
Hint
In Case #1 (, ), and only the bit of value is set in , so building 1 gets a single slide, to building . The four ways to get from building 1 to building 5 are:
- 1, 2, 5
- 1, 2, 3, 5
- 1, 2, 4, 5
- 1, 2, 3, 4, 5
In Case #3, building slides from 1 to 2, 2 to 3, 3 to 1, and 1 to 4 would create infinitely many ways for the CEO to reach building 4 (she could go directly to 4, or go around the loop once and then go to 4, or go around the loop twice, and so on), but the CEO requested exactly 20 ways.