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. excel-填充

    问题[1]:需要将一列元素的空全部填充为NULL 选定列->F5定位(推荐先定位行总数)->再次F5定位(选空值)->在选定后的一个框内输入NULL->ctrl+enter 完 ...

  2. 常哥带你认识NoSQL和Redis的强大

    各位朋友,这篇文章是针对Redis快速了解的内容,为了学好Redis在这里首先跟大家聊聊NoSQL相关内容,有了概念和方向后,我们再学习Redis大家会感觉得心应手. [公众号dotNet工控上位机: ...

  3. lombok的基本使用方法

    在java刚开始学习的时候,首先就会学习封装.继承和多态,就拿封装来说,封装就是为了保护数据安全而将实体类内部数据保持为私有状态,如果外部程序想要访问里面的数据就必须调用此实体类提供的相关数据接口,这 ...

  4. 06 大数据CentOS6.5mini安装与网络配置

    1. CentOS6.5mini安装 文件>>新建虚拟机 选择自定义,下一步 默认,下一步 选择稍后安装操作系统,下一步 选择CentOS版本,下一步 给虚拟机命名,这个是在VMWare中 ...

  5. JS学习第八天

    DOM访问列表框.下拉菜单的常用属性: form返回列表框.下拉菜单所在的表单对象; length返回列表框.下拉菜单的选项个数; options返回列表框.下拉菜单里所有选项组成的数组; defau ...

  6. QT QMdiArea 添加背景或添加背景图片失效问题

    说起QMdirArea 这个控件与其他控件真所不同.... 这里记一下 我踩过的坑之一,,,,, QMdiArea 默认的背景 不符合我要求,,当时我就理所当然就想往常一样给它设置颜色 万万没想到.. ...

  7. 2020-07-16:如何获得一个链表的倒数第n个元素?

    福哥答案2020-07-16: 1.快慢指针.快指针先走n步,然后快慢指针同时走,直到快指针走到尾.2.两次遍历.第一次遍历获取链表长度,然后计算出序号,然后遍历获取序号下的元素.3.数组保存.遍历一 ...

  8. 编写高质量代码的50条黄金守则-Day 02(首选readonly而不是const)

    编写高质量代码的50条黄金守则-Day 02(首选readonly而不是const),本文由比特飞原创发布,转载务必在文章开头附带链接:https://www.byteflying.com/archi ...

  9. c cpp编程用到的系统边角与其拾遗

    拾遗 Q:unix编程怎么查一个函数在哪个头文件中 A: 可以用诸如 man 3 printf Q: man后面接个数字什么意思,如man 3 printf A:如下 man man中的引用 下表显示 ...

  10. 2020.5.24 第四篇 Scrum冲刺博客

    Team:银河超级无敌舰队 Project:招新通 项目冲刺集合贴:链接 目录 一.每日站立会议 1.1 会议照片 1.2 项目完成情况 二.项目燃尽图 三.签入记录 3.1 代码/文档签入记录 3. ...