Official

B - Enlarged Checker Board Editorial by en_translator


First of all

In this editorial, the indices of rows and columns are 0-indexed. For example, the top-left tile is Tile \((0,0)\).

Fundamental Idea

Consider the following two problems.

  1. What is the color of Tile \((i,j)\) in grid \(X\)?
  2. Which tile \((i',j')\) does the square at the \(i\)-th row from the top and \(j\)-th column from the left (let us hereinafter denote this square by Square \((i,j)\)) belong to?

If the two problems above are solved, then we can construct the grid \(X\) by letting Square \((i,j)\) white if it belongs to a white tile \((i',j')\), or letting the square black if it belongs to a black tile.

Finding the color of Tile \((i,j)\)

The colors of tiles in the \(0\)-th row are white, black, white, … from the left; those in the \(1\)-st row are black, white, black, … from the left; those in the \(2\)-nd row are white, black, white, … from the left, and so on. In general, the colors of tiles in the \(i\)-th row starts with white if \(i\) is even, or with black if \(i\) is odd, and in both case black and white tiles are aligned alternately.
In the row where the colors are white, black, white, …, the color at the \(j\)-th column is white if \(j\) is even. If the leftmost tile is black, it is the opposite.
Based on the discussion so far, here is a C++ code to find the color of each tile.

vector<string> tiles(N,string(N,'-'));
for(int i=0;i<N;i++){
	if(i%2==0){
		for(int j=0;j<N;j++){
			if(j%2==0)tiles[i][j] = '.';
			else tiles[i][j] = '#';
		}
	}
	else{
		for(int j=0;j<N;j++){
			if(j%2==0)tiles[i][j] = '#';
			else tiles[i][j] = '.';
		}
	}
}

Alternatively, we can say that Tile \((i,j)\) is white if \(i+j\) is even, or black if it is odd.

Finding Tile \((i',j')\) that square \((i,j)\) belongs to

Since each tile is a grid with \(A\) rows and \(B\) columns, Tile \((0, 0)\) for example contains the squares with row index between \(0\) (inclusive) and \(A\) (exclusive) and with column index between \(0\) (inclusive) and \(B\) (exclusive). It can be generalized as follows: Tile \((i',j')\) contains squares \((i,j)\) such that \(A \times i' \leq i \lt A \times (i'+1)\) and \(B \times j' \leq j \lt B \times (j'+1)\). Conversely, the Tile \((i',j')\) that square \((i,j)\) belongs can be found as \(i'= \lfloor \frac{i}{A} \rfloor, j'= \lfloor \frac{j}{B} \rfloor \).

As already described firsthand, once we obtained \((i',j')\) this way, we can refer to the color of Tile \((i',j')\) in order to find the color of each square of \(X\). Therefore, the problem has been solved.

Sample Code 1

#include <bits/stdc++.h>
using namespace std;

int main() {
    
	int N,A,B;
	cin>>N>>A>>B;
	
	vector<string> tiles(N,string(N,'-'));
	for(int i=0;i<N;i++){
		if(i%2==0){
			for(int j=0;j<N;j++){
				if(j%2==0)tiles[i][j] = '.';
				else tiles[i][j] = '#';
			}
		}
		else{
			for(int j=0;j<N;j++){
				if(j%2==0)tiles[i][j] = '#';
				else tiles[i][j] = '.';
			}
		}
	}
	
	vector<string> X(A*N,string(B*N,'-'));
	for(int i=0;i<A*N;i++){
		for(int j=0;j<B*N;j++){
			X[i][j] = tiles[i/A][j/B];
		}
	}
	
	for(int i=0;i<A*N;i++){
		cout<<X[i]<<endl;
	}
	
    return 0;
}

Sample Code 2 (Using the parity of \(i+j\) to determine the color of tiles)

#include <bits/stdc++.h>
using namespace std;

int main() {
    
	int N,A,B;
	cin>>N>>A>>B;
	
	vector<string> X(A*N,string(B*N,'-'));
	for(int i=0;i<A*N;i++){
		for(int j=0;j<B*N;j++){
			if(((i/A)+(j/B))%2==0)X[i][j] = '.';
			else X[i][j] = '#';
		}
	}
	
	for(int i=0;i<A*N;i++){
		cout<<X[i]<<endl;
	}
	
    return 0;
}

posted:
last update: