Cake

Time Limit: 2000/1000 MS (Java/Others)    Memory
Limit: 131072/131072 K (Java/Others)

Total Submission(s): 965    Accepted Submission(s): 119

Special Judge

Problem Description
There are m soda
and today is their birthday. The 1-st
soda has prepared n cakes
with size 1, 2, \dots, n.
Now 1-st
soda wants to divide the cakes into m parts
so that the total size of each part is equal. 



Note that you cannot divide a whole cake into small pieces that is each cake must be complete in the m parts.
Each cake must belong to exact one of m parts.
 
Input
There are multiple test cases. The first line of input contains an integer T,
indicating the number of test cases. For each test case:



The first contains two integers n and m (1
\le n \le 10^5, 2 \le m \le 10),
the number of cakes and the number of soda.

It is guaranteed that the total number of soda in the input doesn’t exceed 1000000. The number of test cases in the input doesn’t exceed 1000.
 
Output
For each test case, output "YES" (without the quotes) if it is possible, otherwise output "NO" in the first line.



If it is possible, then output m lines
denoting the m parts.
The first number s_i of i-th
line is the number of cakes in i-th
part. Then s_inumbers
follow denoting the size of cakes in i-th
part. If there are multiple solutions, print any of them.
 
Sample Input
4
1 2
5 3
5 2
9 3
 
Sample Output
NO
YES
1 5
2 1 4
2 2 3
NO
YES
3 1 5 9
3 2 6 7
3 3 4 8
 
Source
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <algorithm>
#define LL long long
using namespace std;
const LL MAXN = 500000 + 10;
LL n, m;
LL ans[20][MAXN];
LL A[MAXN];
LL pos[MAXN];
LL tot, tar;
bool dfs(LL dep, LL now, LL u, LL c)
{
if(now == 0)
{
LL k = 0;
while(pos[k] != -1) k++;
pos[k] = c;
if(dfs(dep + 1, A[k], k + 1, c)) return true;
pos[k] = -1;
return false;
}
if(now == tar)
{
if(dep == tot) return true;
if(dfs(dep, 0, 0, c + 1)) return true;
}
for(LL i=u;i<tot;i++)
{
if(pos[i] == -1 && now + A[i] <= tar)
{
pos[i] = c;
if(dfs(dep + 1, now + A[i], i + 1, c)) return true;
pos[i] = -1;
}
}
return false;
}
int main()
{
LL T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &m);
for(LL i=0;i<m;i++) for(LL j=0;j<n;j++) ans[i][j] = 0;
if((n * (n + 1) / 2) % m != 0 || (2 * m - 1) > n)
{
printf("NO\n");
continue;
}
while(n >= 40)
{
for(LL i=0;i<m;i++) ans[i][++ans[i][0]] = n - i;
for(LL i=0;i<m;i++) ans[i][++ans[i][0]] = n - 2 * m + 1 + i;
n -= 2 * m;
}
tot = n;
tar = n * (n + 1) / (2 * m);
for(LL i=0;i<tot;i++) A[i] = tot - i;
for(LL i=0;i<tot;i++) pos[i] = -1;
dfs(0, 0, 0, 0);
for(LL i=0;i<tot;i++) ans[pos[i]][++ans[pos[i]][0]] = A[i];
printf("YES\n");
for(LL i=0;i<m;i++)
{
printf("%d", ans[i][0]);
for(LL j=1;j<=ans[i][0];j++) printf(" %d", ans[i][j]);
printf("\n");
}
}
return 0;
}


HDU 5355 Cake(2015多校第六场,搜索 + 剪枝)的更多相关文章

  1. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

  2. HDU 5355 Cake

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

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

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

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

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

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

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

  6. HDU 5289 Assignment(2015 多校第一场二分 + RMQ)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  7. hdu 5317 RGCDQ (2015多校第三场第2题)素数打表+前缀和相减求后缀(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5317 题意:F(x) 表示x的不同质因子的个数结果是求L,R区间中最大的gcd( F(i) , F(j ...

  8. hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...

  9. hdu 5308 (2015多校第二场第9题)脑洞模拟题,无语

    题目链接:http://acm.hdu.edu.cn/listproblem.php?vol=44 题意:给你n个n,如果能在n-1次运算之后(加减乘除)结果为24的输出n-1次运算的过程,如果不能输 ...

随机推荐

  1. [POI2005]Toy Cars

    题目大意: 有n种物品,地上有k个格子,p次操作. 每次操作要求将某一个指定的物品移动到任意一个格子中,同时你可以选择是否将格子中的某一个物品收起来,并消耗1的代价. 如果下达指令时,这个物品刚好在格 ...

  2. 1.4(Spring MVC学习笔记)JSON数据交互与RESTful支持

    一.JSON数据交互 1.1JSON简介 JSON(JavaScript Object Notation)是一种数据交换格式. 1.2JSON对象结构 {}代表一个对象,{}中写入数据信息,通常为ke ...

  3. iOS:扩展UIColor,支持十六进制颜色设置

    来自转载:http://my.oschina.net/leejan97/blog/307491 摘要: 可以直接使用十六进制设置控件的颜色,而不必通过除以255.0进行转换 #define UICol ...

  4. MR hadoop streaming job的学习 combiner

    代码已经拷贝到了公司电脑的: /Users/baidu/Documents/Data/Work/Code/Self/hadoop_mr_streaming_jobs 首先是主控脚本 main.sh 调 ...

  5. IE8 XSS Filter Bypass

    漏洞说明:IE8是微软新推出的一款浏览器,其对CSS2.1的完整支持,HTML5的支持,内置开发工具等等.IE8在浏览器安全性上有非常大的改进,内置了一款无法卸载的Xss Filter,对非持久型跨站 ...

  6. Dedecms getip()的漏洞利用

    flyh4t在非安全发布了dedecms getip()的注射漏洞,漏洞本身的成因没什么好说的老掉牙的X-Forwarded-For的问题,我想这个漏洞很多人都找到了,不过这个漏洞的利用有个地方还是可 ...

  7. edittext SearchView 失去焦点问题

    edittext 默认自己主动获取焦点的 并且会出现小键盘非常烦人 <LinearLayout             android:id="@+id/focus"     ...

  8. 通过ssh上传文件到目标主机

    需要通过ssh上传文件到目标主机上,之前一直时通过ssh客户端来传文件的,这次因为本地没装客户端,所以考虑直接用终端通过ssh连接主机进行文件传输. 只需要一条命令就可以了: scp  ./serve ...

  9. Idea闪退问题-内存不能给太大

    Idea闪退问题-内存不能给太大 学习了:https://blog.csdn.net/qq_17776287/article/details/77529455 学习了:https://blog.csd ...

  10. JAVA利用HttpClient进行POST请求(HTTPS)

    目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用jQuery post进行请求. 但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而JQuery的p ...