A16046 | No Prime Differences
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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.
It can be shown that under the given constraints, there is always a solution.
It can be shown that under the given constraints, there is always a solution.
输入格式
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.
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.
It is guaranteed that the sum of $n\cdot m$ over all test cases does not exceed $10^6$ .
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.
It is guaranteed that the sum of $n\cdot m$ over all test cases does not exceed $10^6$ .
输出格式
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.
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.
If there are multiple solutions, print any of them.
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.
If there are multiple solutions, print any of them.
输入输出样例
输入 #1
3 4 4 5 7 6 4
输出 #1
16 7 1 9 12 8 2 3 13 4 10 11 14 5 6 15 29 23 17 9 5 6 2 33 27 21 15 11 7 1 32 31 25 19 20 16 10 26 30 24 18 14 8 4 35 34 28 22 13 12 3 2 3 7 11 8 9 1 10 17 13 5 4 18 14 6 12 19 23 15 21 20 24 16 22
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.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted