This page is still under construction.

Parts of this page are still being built. What you see may change.

Card Flipping Game

Time limit1sMemory limit1024 MB

Summary
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 NN and M(≤N)M(\le N), and a pattern PP consisting of the characters 'O' and 'X' arranged in an N×NN \times N 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 NN written on it, place cards B in an N×NN \times N 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 N×NN \times N grid where the cards are placed, choose any one row or one column. Also, according to the integer MM written on card A, choose any integer k(0≤k<M)k(0 \le k < M).
  • Step 2: If the choice in step 1 is row ii, then for all jj with j≡k(modM)j \equiv k \pmod{M}, flip all cards at positions (i,j)(i,j) on the grid. Similarly, if the choice in step 1 is column jj, then for all ii with i≡k(modM)i \equiv k \pmod{M}, flip all cards at positions (i,j)(i,j) on the grid.

The player must repeat 'flips' to make the pattern of the cards on the grid match the pattern PP drawn on card A. Determine whether this is actually possible.

Constraints

  • 1≤M≤N≤1 0001 \le M \le N \le 1\,000
  • Every character in PP is either 'O' or 'X'.

Examples1

  1. Example 1

    Input
    1 1
    O
    
    Expected output
    YES