题解 \(by\;zj\varphi\)
每次直接来回一条龙选,如果 \(\frac{n}{k}\) 是奇数,那么将连续的三行取出来,凑一下。
具体实现就是对其中两行凑出来公差为 \(1\) 的等差数列,可以先算出来中位数,再两两配对。
记得特判无解,但 \(n=1,k=1\) 时是有解的。
Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
#define debug1(x) std::cerr << #x"=" << x << ' '
#define debug2(x) std::cerr << #x"=" << x << std::endl
#define Debug(x) assert(x)
struct nanfeng_stream{
template<typename T>inline nanfeng_stream &operator>>(T &x) {
bool f=false;x=0;char ch=gc();
while(!isdigit(ch)) f|=ch=='-',ch=gc();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
return x=f?-x:x,*this;
}
}cin;
}
using IO::cin;
namespace nanfeng{
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
using ll=long long;
static const int N=1e6+7;
int st[N][2],T,n,k;
inline int main() {
FI=freopen("subset.in","r",stdin);
FO=freopen("subset.out","w",stdout);
cin >> T;
for (ri z(1);z<=T;pd(z)) {
cin >> n >> k;
if (n==1) {printf("Yes\n1\n");continue;}
ll al=1ll*n*(n+1)>>1;
if (al%k) {printf("No\n");continue;}
int nm=n/k,cnt=0;
if (nm&1) {
if (nm==1) {printf("No\n");continue;}
printf("Yes\n");
const int bs=k<<1;
int hf=n+n-bs+1+(k>>1),lim=n-bs,bg=0;
for (ri i(k);i>0;i-=2) {
st[++bg][0]=lim+i;
st[bg][1]=hf-st[bg][0];
--hf;
}
for (ri i(k-1);i>0;i-=2) {
st[++bg][0]=lim+i;
st[bg][1]=hf-st[bg][0];
--hf;
}
for (ri i(1);i<=k;pd(i)) {
const int tmp=bs-(i<<1)+1;
for (ri j(i);j<=lim-k;j+=bs) printf("%d %d ",j,j+tmp);
printf("%d %d %d\n",lim-k+i,st[i][0],st[i][1]);
}
} else {
printf("Yes\n");
const int bs=k<<1;
for (ri i(1);i<=k;pd(i)) {
const int tmp=bs-(i<<1)+1;
for (ri j(i);j<=n;j+=bs) printf("%d %d ",j,j+tmp);
printf("\n");
}
}
}
return 0;
}
}
int main() {return nanfeng::main();}