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. Services

    *在实际运行中同样的Service的确只能有一个. Services有两种启动形式: Started:其他组件调用startService()方法启动一个Service.一旦启动,Service将一直 ...

  2. Cesium 事件

    1.相机事件(移动开始.移动结束等等) viewer.scene.camera.moveEnd.addEventListener(function(){ }): 2.鼠标事件(单击.移动.右键等) v ...

  3. 【POI】修改已存在的xls,新添一列后,再保存本文件+获取最大有效行号+获取单元格内容

    使用POI版本: ① ② ③ ④ package com.poi.dealXlsx; import java.io.File; import java.io.FileInputStream; impo ...

  4. Java笔记6:多态

    一.多态的分类对象的多态性:动物 x = new 猫();函数的多态性:函数重载.重写 二.多态的体现父类的引用指向了自己的子类对象父类的引用也可以接收自己的对象 三.多态的前提必须是类与类之间只有关 ...

  5. JavaScript 数字与字符串 比较大小

    总结一下JS中经常遇到纯数字和各种各样的字符串进行比较: 纯数字之间的比较 alert(1<3);//true 数字字符串比较,会将其先转成数字 alert("1"<& ...

  6. JAVA加解密 -- 消息摘要算法

    消息摘要算法是一种单向加密算法 主要用于验证数据完整性,也是数字签名的核心算法 消息鉴别:指在接收方将原始信息进行摘要,然后与接收到的摘要信息进行对比 a.MD家族 – MD5(128位摘要信息) M ...

  7. MPTCP 源码分析(二) 建立子路径

    简述      MPTCP在进行三次握手之后,客户端和服务端会进行地址信息的交换,让对方知道彼此未用的地址信息. 当客户端知道服务端的地址后就可以建立其他子路径.三次握手和建立子路径的过程如图1:   ...

  8. 万字总结:学习MySQL优化原理(转)

    本文转自:https://www.tuicool.com/wx/2eMBfmq 前言 说起MySQL的查询优化,相信大家收藏了一堆奇技淫巧:不能使用SELECT *.不使用NULL字段.合理创建索引. ...

  9. B4:策略模式 Strategy

    它定义了算法家族,分别封装起来,让他们之间可互相替换,此模式让算法的变化,不会影响到使用算法的客户. UML 示例代码: abstract class Strategy { protected $mo ...

  10. npm install --no-bin-links中的参数“no-bin-links”表示什么意思

    npm install --no-bin-links中的参数"no-bin-links"表示什么意思