C. No Prime Differences

You are given integers nn and mm. Fill an nn by mm grid with the integers 11 through n⋅mn⋅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.

Input

The first line of the input contains a single integer tt (1≤t≤10001≤t≤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 nn and mm (4≤n,m≤10004≤n,m≤1000) — the dimensions of the grid.

It is guaranteed that the sum of n⋅mn⋅m over all test cases does not exceed 106106.

Output

For each test case, output nn lines of mm integers each, representing the final grid. Every number from 11 to n⋅mn⋅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.

 如果将所有数挨着放,那么左右间距永远是1,上下间距为m,如果m是合数,那么刚刚好,如果是素数呢?

对于挨着放的每一行是满足要求的,假如某行某列的一个数是x, 他的下一行此列为x + m,差值为m,他的下两行为x + 2 * m,差值为2 * m,这个2 * m一定是个合数,由此想到可以按照一定规则重新排列每个行

#include <iostream>

using namespace std;

int main() 
{

    int t; 
	cin >> t;
    while(t --)
	{

        int n, m; 
		cin >> n >> m;

        for(int i = 0; i < n; i ++) 
		{
            for(int j = 0; j < m; j ++) 
			{
                if(i % 2 == 0) 
				cout << (n / 2 + i / 2) * m + j + 1 << ' ';
                else 
				cout << (i / 2) * m + j + 1 << ' ';
            }
            cout << '\n';
        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值