CF1838C题解

发布时间 2023-11-05 16:32:13作者: 2021cjx

显然 \(1\) 不是质数,除二外偶数不是质数。

然后分类讨论

对于 \(m\) 为偶数,构造

\[\begin{bmatrix} 1 & 2 & 3 & \cdots & m \\ m+1 & m+2 & m+3 & \cdots & 2m \\ &&\cdot\\ &&\cdot\\ &&\cdot\\ (n-1)m+1 & (n-1)m+2 & (n-1)m+3 & \cdots & nm \\ \end{bmatrix} \]

则列增量为 \(1\),行增量为 \(m\)

对于 \(n\) 为偶数,构造

\[\begin{bmatrix} 1 & n+1 & 2n+1 & \cdots & (m-1)n+1 \\ 2 & n+2 & 2n+2 & \cdots & (m-1)n+2 \\ &&\cdot\\ &&\cdot\\ &&\cdot\\ n & 2n & 3n & \cdots & nm \\ \end{bmatrix} \]

则行增量为 \(1\),列增量为 \(n\)

对于 \(n\)\(m\) 均为奇数的情况,循环移位

\[\begin{bmatrix} 1 & 2 & 3 & \cdots & m-1 & m \\ m+2 & m+3 & m+4 & \cdots & 2m & m+1 \\ 2m+3 & 2m+4 & 2m+5 & \cdots & 2m+1 & 2m+2 \\ &&\cdot\\ &&\cdot\\ &&\cdot\\ \end{bmatrix} \]

行增量为 \(1\)\(m-1\),列增量为 \(m+1\)\(1\)

无了。

#include<bits/stdc++.h>
using namespace std;
int n,m;
int T;
signed main()
{
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        if(m % 2 == 0)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    cout<<((i-1)*m+j)<<" ";
                }
                cout<<'\n';
            }
        }
        else if(n % 2 == 0)
        {
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    cout<<(i + (j-1) * n)<< " ";
                }
                cout<<'\n';
            }
        }
        else
        {
            for(int i=1;i<=n;i++)
            {
                int op = (i-1) % m + 1;
                for(int j=op;j<=m;j++)
                {
                    cout<<((i-1) * m + j)<<" ";
                }
                for(int j=1;j<op;j++)
                {
                    cout<<((i-1) * m + j)<<" ";
                }
                cout<<'\n';
            }
        }
        cout<<'\n';
    }
}