Flipping and cutting (Easy)
Time limit1sMemory limit1024 MB
Given a circle with circumference c and a step length sqrt(s), find the minimum number of wedge flips after which the circle is fully black again, or report that it never happens.
- Level
Medium6 of 10
- Topics
- Number theory, Math, Simulation
- Solved
- No attempts yet
Problem
On the table we have a perfect circle cut out of paper. The circle is all black from the top and all white from the bottom. The circumference of the circle is a positive integer .
We also have a pair of scissors. We will use the scissors to cut the circle. At the beginning we choose an arbitrary point on the boundary of the circle (called the active point) and make a straight cut from that point to the center of the circle.
Next, we choose a positive integer (with ): the square of our step length. Then we perform an infinite sequence of rounds. Each round consists of these steps:
- Find a new point by starting at the current active point and measuring the distance along the boundary of the circle, counter-clockwise.
- Make a straight cut from the new point to the center of the circle.
- We now have a wedge (a sector of the circle) that starts with the cut from the active point to the center and continues counter-clockwise to the cut from the new point to the center. The inside of the wedge may also contain other cuts.
- We lift the entire wedge from the table, flip it upside down and return it to its place. If the wedge already consists of multiple pieces, their order is reversed by this operation.
- The new point becomes the active point for the next round.
The figure below illustrates the first four rounds for and . Pay close attention to what happens in the fourth round.

For some pairs the circle becomes completely black again after finitely many rounds. Find all such pairs, and for each one, find the smallest number of rounds after which this happens.
A circle is completely black if each point of the top side is black or lies on one of the cuts. In other words, the color of points lying directly on a cut does not matter.
Input
The first line of the input contains an integer , the number of test cases. Each test case is preceded by a blank line.
Each test case consists of a single line with the positive integers and . You may assume , , and that is an integer.
Output
If the process never returns to a completely black circle, output a single line with the text never. Otherwise, output a single line with the smallest positive number of rounds after which the circle is again completely black.
Hint
In the first example the circumference is 360 and each wedge contains 60 units of the circumference. After six rounds every part of the circle has been flipped exactly once, and after another six rounds everything is back to where it started.
The second example matches the situation in the figure above. It takes 24 rounds to return to an all-black circle.
In the third example each round flips 5/6 of the circle. Surprisingly, the circle is completely black again after four rounds.