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. DCGAN实现

    DCGAN实现 代码 dcgan.py #!/usr/bin/env python # -*- coding: utf-8 -*- import os import math import argpa ...

  2. WebApi的创建,部署,Oauth身份认证(一)

    1.首先创建一个项目 2.选择Web API 3.创建一个空的控制器 4.控制器名称为MyApiController using System; using System.Collections.Ge ...

  3. find the lowest number location

    before #设定路径列表Path def find_path2(heightmap, x, y, water_level=557,path=[]): #global path #设定坐标 右0 左 ...

  4. 2020-03-25:快排、堆排和归并都是O(nlog n)的算法,为何JDK选择快速排序?

    福哥答案2020-03-26: 口诀如下:冒选插希快 堆归计桶基(冒泡,选择,插入,希尔,快速,堆,归并,计数,桶,基数)冒线 平平 稳常小选平 平平 不常小插线 平平 稳常序希线 四组 不常组快四 ...

  5. DRF内置过滤组件与排序组件结合使用

    DRF内置过滤组件Filtering DRF提供了内置过滤组件Filtering,可以结合url路径的改变获取想要的数据,当然用户不可能在url访问路径中自己设置过滤条件,肯定是后端开发人员将前端页面 ...

  6. Oracle数据库安装教程

    一.准备文件 Oracle安装程序(64位)下载地址: http://download.oracle.com/otn/nt/oracle11g/112010/win64_11gR2_database_ ...

  7. 计算机网络-网络层(3)DHCP协议

    主机获得IP地址,除了可以通过静态配置,还可以通过动态主机配置协议DHCP: Dynamic Host Configuration Protocol从服务器动态获取IP地址.子网掩码.默认网关地址.D ...

  8. python numpy常用的数学和统计函数

    numpy模块的核心就是基于数组的运算,相比于列表和其他数据结构,数组的运算效率是最高的.在统计分析和挖掘过程中,经常会使用到numpy模块的函数,以下是常用的数学函数和统计函数: 常数p就是圆周率  ...

  9. 速记OSI七层协议模型

    OSI七层协议模型 第一层:物理层(Physical) 第二层:数据链路层(Data-Link) 第三层:网络层(NetWork) 第四层:传输层(Transport) 第五层:会话层(Session ...

  10. Mac上如果看不到.git目录的解决方法

    Mac OS X上,如果需要查看.git目录下的隐藏文件,操作很简单: 做法是:打开一个Terminal终端窗口,输入: defaults write com.apple.finder AppleSh ...