1. 程式人生 > 實用技巧 >2020牛客暑期多校-6G-Grid Coloring (構造)

2020牛客暑期多校-6G-Grid Coloring (構造)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
using namespace std;
const int mod = 1e9+7;/// 998244353;
const int mxn = 2e7 +7;
ll _,m,n,t,k,ans;
void solve()
{
    for(cin>>t;t;t--)
    {
        cin>>n>>k;
        if(2*n*(n+1)%k!=0 || k==1 || n==1){
            cout
<<-1<<endl; continue; } if(n%k!=0) { ans = 1 ; for(int i=1;i<=2*n+2;i++){ for(int j=1;j<=n;j++){ cout<<ans<<(j==n?'\n':' '); ans%=k; ans++; /// 防止出現零 } } }
else { ll col = 1 ; for(int i=1;i<=2*n+2;i++){ for(int j=1;j<=n;j++){ cout<<( (col+j-1)%k+1 )<<(j==n?'\n':' '); } col++; col%=k; } } } } int main() { ios::sync_with_stdio(
false); cin.tie(0); cout.tie(0); solve(); }
View Code