Jolly Jelly Jiffy
Time limit2sMemory limit256 MB
Count and lexicographically minimize total orders consistent with the final box state of an insertion-sort-like stacking process and one extra value.
- Level
Medium7 of 10
- Topics
- Stack, Topological sort, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
Chansik and his younger brother Minsik like jelly. On his way home from his friend Hongbin's house, Chansik wants to buy jelly to eat with Minsik. There are jelly shops between Chansik's house and Hongbin's house, and Chansik numbered the shops to in order of distance from his own house. Chansik can buy jelly at shop through shop in that order before he meets Hongbin, or at shop through shop in that order on the way back after he meets Hongbin. Either way he buys one jelly at each shop, and the jelly bought at shop is called jelly .
Chansik has to eat the tasty jelly quickly and then study programming, so every time he buys a jelly he tidies his jelly box this way.
- If the jelly on top is tastier than the jelly he just bought, take that jelly out.
- Repeat step 1 until the box is empty, or until the jelly he just bought is tastier than the jelly on top.
- Put the jelly he just bought into the box. Jelly goes in from the top.
- Put the jellies that were taken out back in, starting with the one taken out last.
Chansik's taste decides, for every two different jellies, which of the two is tastier. Chansik recently learned a sorting algorithm, so he knows that this method usually leaves the jellies in order of taste, the tastier jelly closer to the top, whatever order he buys them in. His taste can be contradictory as a matter of logic, though. He may like lemon more than orange, strawberry more than lemon, and orange more than strawberry. In a case like that, the order the jellies are stacked in depends on the order he buys them in.
Suppose and Chansik's taste is the following.
- Jelly 2 is tastier than jelly 1.
- Jelly 1 is tastier than jelly 3.
- Jelly 4 is tastier than jelly 1.
- Jelly 3 is tastier than jelly 2.
- Jelly 4 is tastier than jelly 2.
- Jelly 3 is tastier than jelly 4.
If Chansik buys the jelly before he meets Hongbin, the box fills up as in the figure below.

If Chansik buys the jelly after he meets Hongbin, the box fills up as in the figure below.

Chansik thought that other people could work out his taste from the possible box states alone, so he wrote a program that reads the two states and guesses his taste. Now he wonders whether a little less information is still enough. Write a program that guesses Chansik's taste from the box state for the case where he buys the jelly before he meets Hongbin, together with the jelly on top of the box for the case where he buys the jelly after he meets Hongbin.
Input
The first line contains the number of jelly shops . ()
The second line contains the numbers of the jellies in the box for the case where Chansik buys the jelly before he meets Hongbin, starting with the jelly on top.
The third line contains the number of the jelly on top of the box for the case where Chansik buys the jelly after he meets Hongbin.
Only inputs that have an answer are given.
Output
On the first line, print the number of tastes possible for Chansik, modulo 1,000,000,007.
On each of the next lines, print one taste that is possible for Chansik. The -th value on the -th line is 1 if jelly is tastier than jelly , 0 if jelly is tastier than jelly , and . if .
If several tastes are possible, print the one that comes first in lexicographic order. Two tastes are compared as strings of length , built by joining the lines from the first line to the last with each line read from left to right, and the character 0 comes before the character 1.
With no conditions at all, the number of tastes possible for Chansik is .