Super Brain is a famous scientific reality and talent show aiming to find people with exceptional brainpower.
In one of the challenges called
Link Game of Prime Factors, at first, a n × m matrix M composed with different numbers is given to the contestant (as the leftmost picture). After a couple of operations, the contestant will obtain a matrix within lots of spaces (as the rightmost picture).
In the original Link Game (Lian Lian Kan in Chinese), if you can draw at most three horizontal or vertical head-and-tail-connected lines over the empty grids (the lines can be out of the whole area) to connect two non-empty grids with the same symbol or the two non-empty grids with the same symbol are adjacent, then you can change these two grids into empty.
Furthermore, in the
Prime Factors Link Game, if you can draw at most three lines over the empty grids with the same prime factor or the two non-empty grids with the same prime factor are adjacent, then you can change these grids with the following regulations, Here shows the picture:
- Initially, it's defined that number 0 represents the empty grid.
- If the number of a grid is equal to 1, it'll change into an empty grid 0 automatically.
- If both two linked grids share the same number, you can change these two grids equal to 1, and then change into empty. For instance, such as two linked numbers 11 and 11 shown in the picture, after linking you can change both of them into empty grids.
- If the number of two linked grids is not the same, but share the same Greatest Common Divisor, you can change these two grids both divided by the Greatest Common Divisor at the same time. For instance, such as two linked numbers 9 and 3 shown in the picture, after linking you can change them into 3 and 1, respectively. And then the number 1 will change into an empty grid.
- Otherwise, you cannot change any pair of two linked grids any more, for instance, such as two linked numbers 8 and 7 shown in the picture, because number 8 number and 7 are composed to a coprime pair.
With the ongoing process of the game, it seems more challenging to accomplish the task successfully. More seriously, if some specific grids are wrongly removed in advance by mistake, it is more likely that the entire matrix will eventually become unsolvable.
Because of the time for the contestant to complete the task is very limited, for each state of the whole matrix M, the contestant is eager to know whether the matrix can be solved after lots of operation steps eventually.
Now you are asked to write a program to help them tackle this problem. Given a n × m matrix M composed with different numbers, please help them judge whether all of the non-empty grids that can be removed altogether.