Codeforces Round 877 div2 C No Prime Differences

目录

一、题目

二、题目分析

三、 code


一、题目

传送门

C. No Prime Differences

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given integers n and m. Fill an n by m grid with the integers 11 through n⋅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 t (1≤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 n and m (4≤n,m≤1000) — the dimensions of the grid.

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

Output

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

Example

input

3

4 4

5 7

6 4

output

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

Note

The first sample case corresponds to the picture above. The only absolute differences between adjacent elements in this grid are 11, 44, 66, 88, and 99, none of which are prime.


二、题目分析

题目n*m表格要求每一个数的上下左右之差均不为素数,画出两个图之后我们发现,如果按照正常顺序写的话,这个数的左右之差为1符合题意, 上下之差为m,这样的话上下之差可能符合也可能不符合;所以我们按照m是否是素数来讨论:

1.m不为素数,直接按原顺序输出即可

2.m为素数,我们知道素数的倍数一定不是素数,所以我们考虑将原顺序中的行顺序都换一下,这样左右之差还是1,上下之差为素数的倍数。

至于怎么样的换,我们不妨先输出奇数行,再输出偶数行,但是有一个点我们需要注意,当n等于4的时候,这样我们输出顺序就变为了 行顺序:1 3 2 4,这样 3 2之差还是素数,所以我们还是先输出奇数再输出偶数,但是先从大的开始输出,也即行顺序: 3 1 4 2这样即可保证答案正确 


三、code 

#include<bits/stdc++.h>
using namespace std;
bool isprime(int x)
{
    if(x<2)return false;
    for(int i=2;i<=x/i;i++)
    {
        if(x%i==0)return false;
    }
    return true;
}
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int x,y;cin>>x>>y;
        if(isprime(y))
        {
            for(int i=x-1;i>=1;i-=2)
            {
                
               for(int j=1;j<=y;j++)
               {
                   cout<<y*(i-1)+j<<" ";
               }cout<<"\n";
            }
            for(int i=x;i>=1;i-=2)
            {
            
               for(int j=1;j<=y;j++)
               {
                   cout<<y*(i-1)+j<<" ";
               }cout<<"\n";
            }
        }
        else
        {
            for(int i=1;i<=x*y;i++)
            {
                cout<<i<<" ";
                if(i%y==0)cout<<"\n";
            }
        }
        cout<<endl;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值