题目描述
There are n stones numbered from 1 to n.
The weight of the i-th stone is i kilograms. We divide the stones into k groups.
Each group consists of exactly stones.
We define the weight of each group is sum of the stones’ weights in the group.
Can we divide the stones so that the weights of all groups are same?
输入
The first line of input contains an integer T (1 <= T <= 100) denoting the number of test cases.
Each test case consists of one line containing two integers n (1 ≤ n ≤ 100000), k (k is divisor of n).
It is guaranteed that the sum of n overall test cases does not exceed 500000.
输出
For each test case, if you can’t divide into k groups satisfying condition, print “no”.
Else if you can divide into k groups satisfying condition, print “yes” in one line and then print k lines.
The i-th line represent the indices of stones belonging to the i-th group.
If there are multiple solutions, you can print any of them.
样例输入
复制样例数据
1 4 2
样例输出
yes 1 4 2 3
给出n个石子,重量分别为1→n 1\to n1→n,现在要求分成k kk堆,要求每一堆的重量相同,石子个数相同,问是否可以构造。如果是,输出方案。
总重量不能整除k,不可以,k为n不可以,k为1一定可以,其他情况一定可以
如果n/k为偶数,一前一后取即可
n/k为奇数,将前3*k个分到k组,剩下的按偶数取。前3*k个,可以自己列一下,很多构造方式,但出来的结果一定是一样的。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=1e9+7;
const int modp=998244353;
const int maxn=1e5+50;
const double eps=1e-6;
#define lowbit(x) x&(-x)
#define INF 0x3f3f3f3f
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
x=x*10+ch-'0';
ch=getchar();
}
return x*f;
}
int dcmp(double x)
{
if(fabs(x)<eps)return 0;
return (x>0)?1:-1;
}
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
ll qmod(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)
{
ans=(ans*a)%mod;
}
b>>=1;
a=(a*a)%mod;
}
return ans;
}
vector<ll> vec[maxn];
int main(){
int T;
scanf("%d",&T);
while(T--){
int n,k;
scanf("%lld %lld",&n,&k);
if(k==1ll){
printf("yes\n");
for(ll i=1;i<n;i++){
printf("%lld ",i);
}
printf("%lld\n",n);
continue;
}
if(k==n){
printf("no\n");
continue;
}
ll sum=n*(n+1ll)/2;
if(sum%k){
printf("no\n");
continue;
}
if(n/k%2){
ll fir3k=((9*k)+3)/2;
for(ll i=1;i<=k;i++){
vec[i].clear();
vec[i].push_back(i);
}
ll maxx1=2*k,maxx2=2*k-k/2-1;
for(ll i=1;i<=k;i++){
if(i%2){
vec[i].push_back(maxx1);
vec[i].push_back(fir3k-maxx1-i);
maxx1--;
}
else{
vec[i].push_back(maxx2);
vec[i].push_back(fir3k-maxx2-i);
maxx2--;
}
}
ll l=3*k+1,r=n,cnt=(n/k-3)/2;
for(ll i=1;i<=k;i++){
for(ll j=1;j<=cnt;j++){
vec[i].push_back(l++);
vec[i].push_back(r--);
}
}
printf("yes\n");
ll las=vec[1].size();
for(ll i=1;i<=k;i++){
for(ll j=0;j<las-1;j++){
printf("%lld ",vec[i][j]);
}
printf("%lld\n",vec[i][las-1]);
}
}
else{
printf("yes\n");
ll l=1,r=n,cnt=n/k/2;
for(ll i=1;i<=k;i++){
for(ll j=1;j<cnt;j++){
printf("%lld %lld ",l++,r--);
}
printf("%lld %lld\n",l++,r--);
}
}
}
return 0;
}