codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1838C.md (1891B)


      1 Source: https://codeforces.com/problemset/problem/1838/C
      2 
      3 C. No Prime Differences
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 You are given integers $n$ and $m$. Fill an $n$ by $m$ grid with the integers $1$ through $n\cdot m$, in such a way that for any two adjacent cells in the grid, the absolute difference of the values in those cells is not a prime number. Two cells in the grid are considered adjacent if they share a side.
     22 
     23  ![](https://espresso.codeforces.com/58a8ad463a03a7b533c80c7cc8f71f56bd745256.png) 
     24 
     25 It can be shown that under the given constraints, there is always a solution.
     26 
     27 Input
     28 
     29 The first line of the input contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The description of the test cases follows.
     30 
     31 The first and only line of each test case contains two integers $n$ and $m$ ($4 \le n, m \le 1000$) — the dimensions of the grid.
     32 
     33 It is guaranteed that the sum of $n\cdot m$ over all test cases does not exceed $10^6$.
     34 
     35 Output
     36 
     37 For each test case, output $n$ lines of $m$ integers each, representing the final grid. Every number from $1$ to $n\cdot m$ should appear exactly once in the grid.
     38 
     39 The extra spaces and blank lines in the sample output below are only present to make the output easier to read, and are not required.
     40 
     41 If there are multiple solutions, print any of them.
     42 
     43 Example
     44 
     45 Input
     46 
     47 ```
     48 3
     49 
     50 4 4
     51 
     52 5 7
     53 
     54 6 4
     55 ```
     56 
     57 Output
     58 
     59 ```
     60 16  7  1  9
     61 12  8  2  3
     62 13  4 10 11
     63 14  5  6 15
     64 
     65 29 23 17  9  5  6  2
     66 33 27 21 15 11  7  1
     67 32 31 25 19 20 16 10
     68 26 30 24 18 14  8  4
     69 35 34 28 22 13 12  3
     70 
     71  2  3  7 11
     72  8  9  1 10
     73 17 13  5  4
     74 18 14  6 12
     75 19 23 15 21
     76 20 24 16 22
     77 ```
     78 
     79 Note
     80 
     81 The first sample case corresponds to the picture above. The only absolute differences between adjacent elements in this grid are $1$, $4$, $6$, $8$, and $9$, none of which are prime.