Problem Description
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?
 
Input
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.
 
Output
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.
 
Sample Input
1
4 2
 
Sample Output
yes
1 4
2 3
 
Source
 
Recommend
chendu   |   We have carefully selected several similar problems for you:  6623 6622 6621 6620 6619 
这个题先用求和公式对(1——n)求和 然后判断是否能进行整分,不行就输出no 可以就输出yes 
对于打印的时候怎么构造是要分情况讨论的若n/k是偶数 则按列进行蛇皮填 若n/k是奇数 要单独处理前两列后面进行蛇皮排 但也是前两列是这个题的难点
 前两列 你要实现把 1----2k个数都用上 并且保证每行之和能够递增+1 这时候我们可以通过等差数列的性质 然后求出第一行的值然后赋值为 1 和 另一个可以求出来的数 然后前者+(k/2+1) -(k/2)...... 后者-(k/2)+(k/2+1).....就把这个题的构造实现了

代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+; typedef long long ll;
using namespace std;
vector<int>vec[maxn];
int vis[maxn];
int main()
{
int T;
cin>>T;
ll n,k;
while(T--)
{
scanf("%lld%lld",&n,&k);
ll sum=(n*(n+)/);
for(int t=; t<=k; t++)
{
vec[t].clear();
}
memset(vis,,sizeof(vis));
if(sum%k!=)
{
puts("no");
}
else
{
if((n/k)%==)
{
puts("yes");
int cnt=;
for(int j=; j<=n/k; j++)
{
if(j%==)
{
for(int t=; t<=k; t++)
{
vec[t].push_back(cnt++);
}
}
else
{
for(int t=k; t>=; t--)
{
vec[t].push_back(cnt++);
}
}
}
for(int t=; t<=k; t++)
{
for(int j=; j<vec[t].size(); j++)
{
if(j!=vec[t].size()-)
printf("%d ",vec[t][j]);
else
{
printf("%d",vec[t][j]);
}
}
printf("\n");
}
}
else
{
puts("yes");
if(n==)
{
puts("");
continue;
}
ll kk=(*k+)/;
ll ss=k/+; int cnt=;
vec[].push_back(cnt);
vec[].push_back(kk-cnt);
int cnt1=cnt;
int cnt2=kk-cnt;
for(int t=;t<=k;t++)
{
if(t%==)
{
int temp1=cnt1+ss,temp2=cnt2-abs(k-ss);
vec[t].push_back(temp1);
vec[t].push_back(temp2);
cnt1=temp1;
cnt2=temp2;
}
else
{
int temp1=cnt1-abs(k-ss),temp2=cnt2+ss;
vec[t].push_back(temp1);
vec[t].push_back(temp2);
cnt1=temp1;
cnt2=temp2;
} }
cnt=*k+;
for(int j=; j<=n/k-; j++)
{
if(j%==)
{
for(int t=; t<=k; t++)
{
vec[t].push_back(cnt++);
}
}
else
{
for(int t=k; t>=; t--)
{
vec[t].push_back(cnt++);
}
}
}
for(int t=; t<=k; t++)
{
for(int j=; j<vec[t].size(); j++)
{
if(j!=vec[t].size()-)
printf("%d ",vec[t][j]);
else
{
printf("%d",vec[t][j]);
}
}
printf("\n");
}
}
}
}
return ;
}

HDU多校1003-Divide the Stones(构造)的更多相关文章

  1. 2018 HDU多校第四场赛后补题

    2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...

  2. 2018 HDU多校第三场赛后补题

    2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...

  3. 2015 HDU 多校联赛 5363 Key Set

    2015 HDU 多校联赛 5363 Key Set 题目: http://acm.hdu.edu.cn/showproblem.php? pid=5363 依据前面给出的样例,得出求解公式 fn = ...

  4. 2015 HDU 多校联赛 5317 RGCDQ 筛法求解

    2015 HDU 多校联赛 5317 RGCDQ 筛法求解 题目  http://acm.hdu.edu.cn/showproblem.php? pid=5317 本题的数据量非常大,測试样例多.数据 ...

  5. [HDU多校]Ridiculous Netizens

    [HDU多校]Ridiculous Netizens 点分治 分成两个部分:对某一点P,连通块经过P或不经过P. 经过P采用树形依赖背包 不经过P的部分递归计算 树型依赖背包 v点必须由其父亲u点转移 ...

  6. hdu多校第四场 1003 (hdu6616) Divide the Stones 机智题

    题意: 给你重量分别为1到n的n个石头,让你分成重量相等,数量也相等的k组,保证k是n的约数.问你能不能分配,如果能,输出具体的分配方案. 题解: 首先,如果1到n之和不能整除k,那么一定不能如题意分 ...

  7. [2019杭电多校第四场][hdu6616]Divide the Stones

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6616 题意是说n个数分别为1-n,将n个数分成k堆,能否满足每堆个数相等,数值之和相等.保证n%k=0 ...

  8. HDU - 6400 多校8 Parentheses Matrix(构造)

    Parentheses Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  9. 【HDOJ6616】Divide the Stones(构造)

    题意:给定n堆石子,第i堆的个数为i,要求构造出一种方案将其分成k堆,使得这k堆每堆数量之和相等且堆数相等 保证k是n的一个约数 n<=1e5 思路:先把非法的情况判掉 n/k为偶数的方法及其简 ...

随机推荐

  1. ios 版本更新提示-硬更新/软更新

    实现: 强制更新:每次弹框 非强制更新:一天提示一次 代码如下: 步骤一: 将检测更新写到APPDelegate的applicationDidBecomeActive中 步骤二: 检测是否需要更新 步 ...

  2. 初步理解@Transactional注解

    在SSM项目中,经常在业务层的类或者方法上看到@Transactional注解,只是知道这个注解的作用是进行事务管理,但是具体有哪些属性,在什么情况下进行回滚,确是不那么清楚.所以在网上看了一些视频和 ...

  3. CNN 小结

    CNN 小结 目录 CNN特征提取过程(卷积核描述的是特征信息, 此特征可能就是原图像中的某些像素, 但是卷积核并不找相似的地方在原始图像的哪里, 所以需要将卷积核不断地滑动, 得到的feature ...

  4. k8s使用需认证的私服仓库

    本文内容 在K8s中使用需认证的私服仓库需要导入认证信息到集群中,常规导入方式有两种: 使用Docker已登录的仓库密文导入 使用命令行创建Secret对象导入 本文介绍的就是以上两种方法. 使用Do ...

  5. MongoDB学习2:MongoDB的基本操作

    以下都是基于MongoShell进行操作 1.使用insert进行插入操作  示例: db.<集合>.insertOne(<JSON对象>) db.<集合>.ins ...

  6. SpringMVC常见问题Error configuring application listener of class org.springframework.web.context.ContextLoaderListenejava.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    六月 20, 2018 9:43:34 下午 org.apache.catalina.core.StandardContext listenerStart 严重: Error configuring ...

  7. C++指针变量的基本写法

    指针变量与应用——动态数组 在C++中,有一种神奇的变量,它不可以表示一个值,但是可以表示某个元素的地址,通过地址来访问这个元素. 打个比方:你有一张地图和一个坐标,你就可以通过访问这个坐标来达到你访 ...

  8. 记录使用Python登录浙江大学统一身份认证

    背景 现在每天要进行健康情况上报,但是因为经常睡过头忘记打卡,于是想着写一个程序来自动打卡. 统一身份认证 访问健康情况上报页面(https://healthreport.zju.edu.cn/nco ...

  9. mycat数据库集群系列之mysql主从同步设置

    最近在梳理数据库集群的相关操作,现在花点时间整理一下关于mysql数据库集群的操作总结,恰好你又在看这一块,供一份参考.本次系列终结大概包括以下内容:多数据库安装.mycat部署安装.数据库之读写分离 ...

  10. 基于go+etcd实现分布式锁

    原文链接:https://www.yii-china.com/topic/detail/113 package main import ( "context" "fmt& ...