Root M Leaper

AtCoder
IDabc272_d
Time2000ms
Memory256MB
Difficulty
There is a grid with $N \times N$ squares. We denote by $(i, j)$ the square at the $i$\-th row from the top and $j$\-th column from the left. Initially, a piece is placed on $(1, 1)$. You may repeat the following operation any number of times: * Let $(i, j)$ be the square the piece is currently on. Move the piece to the square whose distance from $(i, j)$ is exactly $\sqrt{M}$. Here, we define the distance between square $(i, j)$ and square $(k, l)$ as $\sqrt{(i-k)^2+(j-l)^2}$. For all squares $(i, j)$, determine if the piece can reach $(i, j)$. If it can, find the minimum number of operations required to do so. ## Constraints * $1 \le N \le 400$ * $1 \le M \le 10^6$ * All values in the input are integers. ## Input The input is given from Standard Input in the following format: $N$ $M$ [samples]
Samples
Input #1
3 1
Output #1
0 1 2
1 2 3
2 3 4

You can move the piece to four adjacent squares.
For example, we can move the piece to $(2,2)$ with two operations as follows.

*   The piece is now on $(1,1)$. The distance between $(1,1)$ and $(1,2)$ is exactly $\sqrt{1}$, so move the piece to $(1,2)$.
*   The piece is now on $(1,2)$. The distance between $(1,2)$ and $(2,2)$ is exactly $\sqrt{1}$, so move the piece to $(2,2)$.
Input #2
10 5
Output #2
0 3 2 3 2 3 4 5 4 5
3 4 1 2 3 4 3 4 5 6
2 1 4 3 2 3 4 5 4 5
3 2 3 2 3 4 3 4 5 6
2 3 2 3 4 3 4 5 4 5
3 4 3 4 3 4 5 4 5 6
4 3 4 3 4 5 4 5 6 5
5 4 5 4 5 4 5 6 5 6
4 5 4 5 4 5 6 5 6 7
5 6 5 6 5 6 5 6 7 6
API Response (JSON)
{
  "problem": {
    "name": "Root M Leaper",
    "description": {
      "content": "There is a grid with $N \\times N$ squares. We denote by $(i, j)$ the square at the $i$\\-th row from the top and $j$\\-th column from the left. Initially, a piece is placed on $(1, 1)$. You may repeat t",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc272_d"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "There is a grid with $N \\times N$ squares. We denote by $(i, j)$ the square at the $i$\\-th row from the top and $j$\\-th column from the left.\nInitially, a piece is placed on $(1, 1)$. You may repeat t...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments