题意:给定 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. windows安装tf

    https://www.cnblogs.com/lvsling/p/8672404.html

  2. JS工具类

    封装了开发中常用的操作 并添加了一些扩展方法供调用 var util = { //获取Url中的参数(不支持中文) getParams: function() { var url = location ...

  3. pandas进行条件格式化以及线性回归的预测

    条件格式化 需求1: 将三次考试的成绩小于60分的值找出来,并将字体变为红色 需求2: 将每次考试的第一名找出来,将背景变为绿色 需求3: 使用背景颜色的深浅来表示数值的大小 需求4: 使用数据条的长 ...

  4. 如何用frp进行来无影去无踪

    准备工作 和 注意事项: 1.frp 下载地址  https://github.com/fatedier/frp/releases 2. 需要给有公网ip 的服务端服务器 和 本地客户端服务器 各放一 ...

  5. maven工程 添加本地jar依赖

    和第三方平台对接的时候要用到对方提供的一个jar包,jar包怎么直接添加到pom文件的依赖中呢? jar包一般都是公共的,要上传到私服仓库.我们都是直接登录私服,操作仓库. 登录私服可以在项目的pom ...

  6. vim简单命令

    保存文件:普通模式(在  :)后面 1.wq 2.q 3.!q 4.shirft+zz  直接退出vim 5.set nu  设置行号 行号移动: 1.shift+1;移动到该行末尾 2.shirft ...

  7. AD16PCB如何快速删除走线

    工具(Tools)取消布线(Un_Route)全部(AII) ad pcb画图,如果想整体去掉一条线,只要是连接在一起的,不管在哪一层,都可以采取如下方法:1.PCB画面下,按组合键Ctrl+H,会出 ...

  8. 微信小程序开发——点击防重的解决方案

    对于一些涉及后端接口请求的单击事件,不论后端是否做了请求限制,前端还是有必要进行点击防重处理的. 这样既能减少对服务器端的压力,也能有效防止因重复请求而造成一些不可预期的异常. 尤其是接口请求结果处理 ...

  9. Scrapy简单入门及实例讲解

    Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的, 也可以 ...

  10. sql的日期和时间函数–date_format

    Mysql的日期和时间函数–date_format   DATE_FORMAT(date,format)依照 format 字符串格式化 date 值.下面的修饰符可被用于 format 字符串中:修 ...