题意:给定 n,m,让你把 1 ~ n 分成 m 部分,而且每部分和是一样大的。

析:首先先判断不能分成的,第一种是 sum (1 ~ n 的和)不能被 m 整除,或者 sum / m < n,其他的情况都有解。

这个题采用的是构造加暴力搜索的思想,首先,先成对的构造解,也就 2 * m 个,每 2 * m 组,分别放到 m 个部分,这样都每部分的贡献都是一样的(最大的和最小一组,次大和次小等等),然后剩下的部分进行搜索暴力,但是要注意的是,如果剩下的不够 m 个的话,这样是不可能搜索出解的,要再加上一个 2*m,但是加上一个 2 * m,复杂度就大了很多,会超时,所以可以对于大于 20 的再减去 m,这样就能够少搜索一部分。就是有一组神数据,那就是 20 6 (这个是我用暴力找出来的),如果特判这个数据的话 嘻嘻 就更容易了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
//#define all 1,n,1
#define FOR(i,n,x) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 20;
const int maxm = 1e6 + 10;
const LL mod = 1000000000000000LL;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x; scanf("%d", &x); return x; } int a[50][50];
int b[50]; bool dfs(int cur){
if(cur == 0) return true;
for(int i = 0; i < m; ++i) if(a[i][40] + cur <= b[i]){
a[i][40] += cur;
a[i][++a[i][0]] = cur;
if(dfs(cur-1)) return true;
a[i][40] -= cur;
--a[i][0];
}
return false;
} int main(){
int T; cin >> T;
while(T--){
scanf("%d %d", &n, &m);
vector<int> ans[20];
LL sum = (LL)n * (n+1) / 2;
LL p = sum / m;
if(sum % m || sum < (LL)n * m){ puts("NO"); continue; }
puts("YES");
int t = n % (m<<1);
if(t) t += m<<1;
t = min(t, n);
for(int i = t+1; i < n; i += (m<<1)){
for(int j = 0; j < m; ++j) ans[j].pb(i+j);
for(int j = 0; j < m; ++j) ans[j].pb(i+(m<<1)-j-1);
}
sum = t * (t+1) / 2 / m;
for(int i = 0; i < m; ++i) b[i] = sum;
if(t > 20){
for(int i = t, j = 0; j < m; ++j, --i) b[j] -= i, ans[j].pb(i);
t -= m;
}
ms(a, 0);
dfs(t);
for(int i = 0; i < m; ++i){
printf("%d", ans[i].sz + a[i][0]);
for(int j = 0; j < ans[i].sz; ++j) printf(" %d", ans[i][j]);
for(int j = 1; j <= a[i][0]; ++j) printf(" %d", a[i][j]);
printf("\n");
}
}
return 0;
}

  

HDU 5355 Cake (构造 + 暴力)的更多相关文章

  1. HDU 5355 Cake

    HDU 5355 Cake 更新后的代码: 今天又一次做这道题的时候想了非常多种思路 最后最终想出了自觉得完美的思路,结果却超时 真的是感觉自己没救了 最后加了记忆化搜索,AC了 好了先说下思路吧.不 ...

  2. 多校第六场 1003 hdu 5355 Cake(贪心)

    题目链接:(数据加强后wa了) hdu 5355 题目大意: 给出一个蛋糕.切成1~n大小的n块.问是否能在不继续分割的情况下拼凑出m等份. 题目分析: 首先我们是可以知道每份蛋糕的尺寸的,利用n*( ...

  3. HDU 5355 Cake (WA后AC代码,具体解析,构造题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5355 题面: Cake Time Limit: 2000/1000 MS (Java/Others) ...

  4. 2015多校第6场 HDU 5355 Cake 贪心,暴力DFS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题意:给你n个尺寸大小分别为1,2,3,…,n的蛋糕,要求你分成m份,要求每份中所有蛋糕的大小之 ...

  5. hdu 5535 Cake 构造+记忆化搜索

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题意:给定n与m,其中1<= n <= 1e5,2 <= m <= 10;问 ...

  6. HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)

    Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Sub ...

  7. 贪心 HDOJ 5355 Cake

    好的,数据加强了,wa了 题目传送门 /* 题意:1到n分成m组,每组和相等 贪心:先判断明显不符合的情况,否则肯定有解(可能数据弱?).贪心的思路是按照当前的最大值来取 如果最大值大于所需要的数字, ...

  8. 构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination

    题目传送门 /* 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! */ /************************************** ...

  9. hdoj 5355 Cake(分析+二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 分蛋糕的题目,有1-n大小的n个蛋糕,要求平均分成m份,不能切开蛋糕 #include<s ...

随机推荐

  1. Bootstrap中的data-toggle,data-target

    data-toggle指以什么事件触发常用的如collapse,modal,popover,tooltips等:data-target指事件的目标, 一起使用就是代表data-target所指的元素以 ...

  2. python 练习题(1-15)

    1.给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 2.生成双色球 3.逻辑运算(运算符优先级) 4.输入一个整数,判断这个数是几位数 5.用while循环计算 1-2+3-4...-9 ...

  3. Game Engine Architecture 5

    [Game Engine Architecture 5] 1.Memory Ordering Semantics These mysterious and vexing problems can on ...

  4. 101210-450789-147200(可以激活Xshell5,而且可以升级) 亲测可用 只能用于xshell5

    101210-450789-147200(可以激活Xshell5,而且可以升级) 亲测可用 只能用于xshell5

  5. 提高GitHub下载速度

    修改/etc/hosts 加上 151.101.72.249 GitHub.global.ssl.fastly.net 192.30.253.112 github.com

  6. Docker容器的基本了解和命令

    一.docker和虚拟机的对比 特性 容器 虚拟机 启动 秒级 分钟级 硬盘使用 一般为MB 一般为GB 性能 接近原生 弱于 系统支持量 单机支持上千个容器 一般几十个 更高效的利用系统资源 更快速 ...

  7. python多线程的学习

    0x00.前言 学了一下python的多线程,threading模块 感觉挺有意思的,随便练手写了一个很粗陋的windows下多线程扫在线ip的脚本 脚本没什么技术含量,纯粹练手,扫一趟192的局域网 ...

  8. JVM性能优化读后笔记

    java性能优化权威指南读后笔记 三重境界 1.花似雾中看:对于遇到的额问题还看不清,不知道真真假假,是是非非. 2.悠然见南山:虽然刚开始对这个领域还不清楚,但随着时间推移,你对它有许多自己的见解, ...

  9. Entity Framework连接postgresql,code first

    官方介绍地址 http://www.npgsql.org/ef6/index.html 首先创建一个控制台应用程序 ,本例居于.NETFramework,Version=v4.6.1 安装包 Enti ...

  10. centos7 安装mysql5.7.20(yum方式)

    windows下安装mysql请访问:windows下mysql5.7.20安装(zip包方式) 一.安装过程 1.安装wget yum install wget 2.查找yum源名称 在mysql官 ...