A long time ago, there was once a very mysterious city called bruhcity, which you know can be depicted as a grid of $N$-by-$M$ cells, where $2 \le N,M \le 1000$. Let us denote the $j$-th cell on the $i$-th row as cell $(i,j)$ for integers $1 \le i \le N$ and $1 \le j \le M$.
For integers $1 \le i_1,i_2 \le N$ and $1 \le j_1,j_2 \le M$, we call cell $(i_2,j_2)$ to be a neighbour of cell $(i_1,j_1)$ if and only if exactly one of the following requirements hold:
- $|i_1-i_2|=1$ and $j_1=j_2$
- $|i_1-i_2|=N-1$ and $j_1=j_2$
- $i_1=i_2$ and $|j_1-j_2|=1$
- $i_1=i_2$ and $|j_1-j_2|=M-1$
Informally, cell $(i_1,j_1)$ and $(i_2,j_2)$ are neighbours if and only if they share an edge with each other, assuming that the grid wraps around in both dimensions.
At most one farmer can be placed in each of the $N\times M$ cells of bruhcity. However, all farmers in bruhcity have the same peculiar requirement: suppose there is a farmer at cell $(i,j)$, then exactly three of the four neighbours of cell $(i,j)$ contain farmers.
However, being a programmer in the modern era, about to compete in HKOI 2025, you no longer have access to how bruhcity looks like, or where farmers are located. Even crazier, you don't even know the values of $N$ or $M$!
Instead, what you have been given, is a parameter called farmer density , given by a fraction $0 \le \frac{p}{q} \le 1$. The farmer density states that the number cells containing farmers are precisely $\frac{p}{q}$ of the total number of cells. In other words, the number of cells with farmers is precisely $\frac{p}{q} \times N \times M$.
Recover any configuration of bruhcity and the locations of farmers, or report that such a configuration does not exist. If there are multiple possible configurations that give the required farmer density, output any one of them. Note that you are NOT required to minimise the values of $N$ or $M$.
Input
The only line of input contains two non-negative integers $p$ and $q$, denoting the farmer density of bruhcity.
Output
If it is possible to construct such a configuration of farmers giving the required farmer density, output YES
on the first line.
On the second line, output two positive integers $N$ and $M$ ($2 \le N,M \le 1000$), separated by a space, denoting the size of bruhcity.
For $i$-th of the next $N$ lines, output $M$ characters of either 0
or 1
each. Output 0
as the $j$-th character if cell $(i,j)$ does not contain a farmer, and 1
as the $j$-th character if cell $(i,j)$ contains a farmer.
Note that among the $N\times M$ characters representing the configuration of bruhcity, there must be exactly $\frac{p}{q} \times N \times M$ characters being 1
, without rounding.
If there are multiple possible configurations, output any of them. Note that you are not required to minimise the values of $N$ and/or $M$.
Otherwise, if it is not possible to construct such a configuration of farmers giving the required farmer density, output NO
on the first and only line.
Constraints
For all test cases, $0 \le p \le q \le 10$, and $q > 0$.
Subtask 1 (2 points): $q=1$.
Subtask 2 (5 points): $q=2$.
Subtask 3 (5 points): $q=3$.
Subtask 4 (14 points): $\frac{p}{q} \le \frac{1}{2}$.
Subtask 5 (11 points): $\frac{p}{q} \le \frac{2}{3}$.
Subtask 6 (6 points): $\frac{p}{q} = \frac{3}{4}$.
Subtask 7 (8 points): $\frac{p}{q} = \frac{7}{10}$.
Subtask 8 (12 points): $\frac{p}{q} \le \frac{3}{4}$.
Subtask 9 (10 points): $\frac{p}{q} = \frac{4}{5}$.
Subtask 10 (27 points): No additional constraints.
Sample Test Cases
Input | Output | |
---|---|---|
6 9 | YES 4 6 110011 110011 011110 011110 |
|
0 4 | YES 2 2 00 00 |
|
8 8 | NO |
Scoring: Per Subtask
Authored by jkjkjk
Appeared in Joint-School Pre-HKOI Mini Competition 2 and Joint-School Pre-HKOI Mini Competition 2 [Early Session]