Card Flipping Game
Time limit1sMemory limit1024 MB
Given an N by N target pattern of O and X and a modulus M, decide whether repeated flips of every M-th cell in a chosen row or column can produce the pattern from an all-X grid.
- Level
Medium7 of 10
- Topics
- Math, Implementation, Matrix, Number theory
- Solved
- No attempts yet
Problem
The card flipping game is a solitaire card game that uses two types of cards, A and B. Card A has the rules of the game written on it. Specifically, as shown in Figure 1, it has two integers and , and a pattern consisting of the characters 'O' and 'X' arranged in an grid.

Figure 1
Card B has the character 'O' on its front and the character 'X' on its back. One card B is used to represent one character of the pattern written on card A, and enough cards B are prepared for this purpose.
Let us start the game. First, choose one card A, and according to the value of written on it, place cards B in an grid. All cards placed initially must be placed so that 'X' is visible. Each placed card is identified by its row and column numbers as in Figure 2.

Figure 2
Once the initial placement of the cards is finished, the player repeats a 'flip', described below, as needed. One 'flip' consists of two steps.
- Step 1: In the grid where the cards are placed, choose any one row or one column. Also, according to the integer written on card A, choose any integer .
- Step 2: If the choice in step 1 is row , then for all with , flip all cards at positions on the grid. Similarly, if the choice in step 1 is column , then for all with , flip all cards at positions on the grid.
The player must repeat 'flips' to make the pattern of the cards on the grid match the pattern drawn on card A. Determine whether this is actually possible.
Constraints
- Every character in is either '
O' or 'X'.