C. OKEA
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.
— Pedro Domingos
You work for a well-known department store that uses leading technologies and employs mechanistic work — that is, robots!
The department you work in sells n⋅kn⋅k items. The first item costs 11 dollar, the second item costs 22 dollars, and so on: ii-th item costs ii dollars. The items are situated on shelves. The items form a rectangular grid: there are nn shelves in total, and each shelf contains exactly kk items. We will denote by ai,jai,j the price of jj-th item (counting from the left) on the ii-th shelf, 1≤i≤n,1≤j≤k1≤i≤n,1≤j≤k.
Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items ai,l,ai,l+1,…,ai,rai,l,ai,l+1,…,ai,r for some shelf ii and indices l≤rl≤r? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.
You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array aa that:
- Every number from 11 to n⋅kn⋅k (inclusively) occurs exactly once.
- For each i,l,ri,l,r, the mean price of items from ll to rr on ii-th shelf is an integer.
Find out if such an arrangement is possible, and if it is, give any example of such arrangement.
Input
The first line contains a single integer tt (1≤t≤5001≤t≤500) — the number of test cases.
The first and only line of each test case contains two integers nn and kk (1≤n,k≤5001≤n,k≤500) — the number of shelves and length of each shelf, respectively.
It is guaranteed that the sum nn over all test cases does not exceed 500500 and the sum kk over all test cases does not exceed 500500.
Output
Print the answer for each test case.
If such an arrangement exists, print "YES" on a single line. After that, print any example on nn lines of kk numbers each, one line per shelf. Each number from 11 to n⋅kn⋅k must occur exactly once in the output.
If no good arrangement exists, print a single word "NO" on its own line.
做法:构造,模拟;
当k==1时,很容易得到对任意的n, 总能得到,这个序列,而且只需从1扫到n换行输出即可;
当k!=1时就需要进行模拟找规律了,可以发现此时当n为奇数时是没有构造方案的;
而n为偶数时是有方案的,且只要每行每列的的技术的个数等于偶数的个数即可;
ex:
n=4,k=2;
可构造:
1 3
5 7
2 4
6 8
n=4,k=4;
构造:
1 3 5 7
9 11 13 15
2 4 6 8
10 12 14 16
或者:
1 3 5 7
9 11 2 4
6 8 13 15
10 12 14 16
等等都是可以的
这里弱狗我给出了基基的代码(low)
-
#include<bits/stdc++.h> using namespace std; typedef long long ll; #pragma GCC optimize(2) const int N=1e7+10; typedef long long ll; int tt; int a[N];int n,k; int main(){ ios::sync_with_stdio(false); cin>>tt; while(tt--){ cin>>n>>k; if(k==1){ cout<<"YES"<<endl; for(int i=1;i<=n;i++) cout<<i<<endl; } else if(n%2){ cout<<"NO"<<endl; } else { cout<<"YES"<<endl; int cnt=0,c=0; for(int i=1;i<=n*k-1;i+=2) { a[++cnt]=i; } for(int i=2;i<=n*k;i+=2) { a[++cnt]=i; } for(int i=1;i<=n*k;i++) { ++c; if(c!=k) cout<<a[i]<<" "; else { cout<<a[i]<<endl; c=0; } } } } return 0; }