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.

C. No Prime Differences_第1张图片

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 

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';
        }
    }
}

你可能感兴趣的:(codeforces,算法)