Möbius Strip
Time limit2sMemory limit512 MB
Decide if repeated thirds-cuts of paper strips can make two given multisets of half-twist counts agree on every type.
- Level
Hard8 of 10
- Topics
- Math, Number theory, Greedy
- Solved
- No attempts yet
Problem
You get a Möbius strip by taking a long strip of paper, twisting it through 180 degrees (a half-twist), and joining one end to the other end of the strip. The number of half-twists does not have to be one. You can make zero, two, three, four, or more half-twists and then tape the two ends. The figure below shows the shapes that come from three, four, and five half-twists.

The type of a strip is a non-negative integer giving the number of half-twists in it. Now take a strip and draw a line along its length, one third of the width away from one edge of the strip. Cut the strip along that line with scissors, and keep cutting until the cut reaches its starting point.
One such cut turns the strip into several strips, and each of them has a type of its own. For example, if you begin with a strip of type 2, you get two strips of type 2. You may cut any of the resulting strips again and again if you wish. Some of the resulting strips may be intertwined. In that case treat them as two distinct strips, and you can cut each strip independently and separately from the other strips.
You are given two sets of strips. Decide whether you can cut some strips in the two sets so that the two sets turn into two new sets holding an equal number of strips of every type.
Input
The input holds several test cases. The first line of each test case has two space separated integers and (), the number of strips in each of the two sets. The next two lines hold and non-negative integers, the types of the strips in the first set and in the second set. Every given strip type is at most 100. The input ends with a line holding -1 -1, which you do not process.
Output
For each test case print, on a separate line, Y if you can make the required transformation and N if you cannot.