题意:给定 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. 在Ubuntu上升级SQLite,并让Python使用新版SQLite

    (本文适用于Debian系的Linux,如Ubuntu.Raspbian等等.) 在Linux上,Python的sqlite3模块使用系统自带的SQLite引擎,然而系统自带的SQLite可能版本太老 ...

  2. 基于maven来Spring MVC的环境搭建遇到“坑”

    1.注解配置路径问题: 在web.xml中配置spring mvc 路径时, 应该配置如下:classpath:classpath:spring-* 2.jdk版本和Spring MVC版本不一致问题 ...

  3. [原创] debian 9.3 搭建Jira+Confluence+Bitbucket项目管理工具(三) -- 安装confluence 6.6.1

    [原创] debian 9.3 搭建Jira+Confluence+Bitbucket项目管理工具(三) -- 安装confluence 6.6.1 有了安装Jira的经验, 这次再安装conflue ...

  4. spring、mybatis事务配置和控制

    springmybatis.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi= ...

  5. MySQL 5.7自定义安装图文详解

    本文教程为大家分享了mysql5.7安装配置方法,供大家参考,具体内容如下 mysql-installer-community-5.7.9.1各版本相关说明: mysql-installer-web- ...

  6. loading data into a table(亲测有效)

    一.实验要求 导入数据到数据库的表里    表内容如下: name owner species sex birth death Fluffy Harold cat f 1993-02-04   Cla ...

  7. SpringCloud-day03-服务注册与发现组件Eureka

    5.服务注册与发现组件Eureka 5.1Eureka简介: Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中 ...

  8. X1226 和 AT24C16 地址冲突问题

    这几天开发eeprom功能的时候,利用单片机的I2C接口访问AT24C16.遇到一个问题: 发现AT24C16的最后一页写不进去.表现为写任何值后,再读取都是0. 经过几天的排查,发现是应为在I2C总 ...

  9. 【Spring】SpringMVCの環境構築(簡)(Version3.1)

    ■Mavenでプロジェクトの新規 ■プロジェクトのイメージ ■必要なラブリア ■ソース ①pom.xml <?xml version="1.0" encoding=" ...

  10. Servlet第三篇(response;request)

    response对象 Tomcat收到客户端的http请求,会针对每一次请求,分别创建一个代表请求的request对象.和代表响应的response对象 那么我们获取浏览器提交过来的数据,找reque ...