No.3567 Modulo Grid
Problem Statement
You are going to write every integer from $1$ to $M=H\times W$, inclusive, $1$ time each into the cells of a grid with $H$ rows and $W$ columns.
Find $1$ way of writing them that satisfies the following condition.
- For any $2$ cells adjacent in one of the $4$ directions (up, down, left, right), the integers $a,b$ written in them form a good pair
Here, integers $a,b$ form a good pair if at least one of the following conditions holds.
- There exists an integer $x$ such that $ax\equiv b \bmod M$
- There exists an integer $x$ such that $a\equiv bx \bmod M$
It can be proved that a way of writing satisfying the condition always exists under the constraints of this problem.
Constraints
- All inputs are positive integers
- $1\le H\times W\le 2\times 10^5$
Input
$H$ $W$
Output
Output $1$ way of writing that satisfies the condition, in the following format. Here, $A_{i,j}$ is the number written in row $i$, column $j$.
$A_{1,1}$ $A_{1,2}$ $\dots$ $A_{1,W}$
$A_{2,1}$ $A_{2,2}$ $\dots$ $A_{2,W}$
$\vdots$
$A_{H,1}$ $A_{H,2}$ $\dots$ $A_{H,W}$
Sample
Sample 1
Input
2 3
Output
1 2 4 3 5 6
In this grid, every positive integer from $1$ to $2\times 3=6$ is written $1$ time each, and the $2$ positive integers written in any $2$ adjacent cells form a good pair.
With the following way of writing, there exists neither an $x$ satisfying $2x\equiv 3\bmod 6$ nor an $x$ satisfying $2\equiv 3x\bmod 6$, so $2,3$ is not a good pair.
1 2 3 4 5 6
Sample 2
Input
1 1
Output
1
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
hamamu