hdu5355 Cake
and today is their birthday. The 1-st
soda has prepared n cakes
with size 1,2,…,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.
indicating the number of test cases. For each test case:
The first contains two integers n and m (1≤n≤105,2≤m≤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.
If it is possible, then output m lines
denoting the m parts.
The first number si of i-th
line is the number of cakes in i-th
part. Then si numbers
follow denoting the size of cakes in i-th
part. If there are multiple solutions, print any of them.
4
1 2
5 3
5 2
9 3
NO
YES
1 5
2 1 4
2 2 3
NO
YES
3 1 5 9
3 2 6 7 3 3 4 8 这题和木棒拼接正方形非常像,用同样的思路即可了。 这里注意dfs可能比較深,所以要手动开栈。#pragma comment(linker, "/STACK:102400000,102400000") 这句话加在程序最前面。#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 100050
#define ll long long
int vis[maxn],liang,fen,n;
set<int>myset[20];
set<int>::iterator it; int dfs(int x,int pos,ll len)
{
int i;
if(x==fen)return 1;
for(i=pos;i>=1;i--){
if(!vis[i]){
vis[i]=1;
if(len+i<liang){
myset[x].insert(i);
if(dfs(x,i-1,len+i))return 1;
myset[x].erase(i);
}
else if(len+i==liang){
myset[x].insert(i);
if(dfs(x+1,n,0))return 1;
myset[x].insert(i);
}
vis[i]=0;
}
}
return 0;
} int main()
{
int i,j,T;
ll num;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&fen);
num=(ll)(n+1)*n/2;
if(n<fen || num%fen!=0 || num/fen<n){
printf("NO\n");continue;
}
liang=num/fen;
memset(vis,0,sizeof(vis));
for(i=0;i<=fen;i++){
myset[i].clear();
} if(dfs(0,n,0)){
printf("YES\n");
for(i=0;i<fen;i++){
printf("%d",myset[i].size());
for(it=myset[i].begin();it!=myset[i].end();it++){
printf(" %d",*it);
}
printf("\n");
}
}
else printf("NO\n");
}
return 0;
}
/*
100
50 10
NO
40 10
YES
3 3 39 40
3 7 37 38
3 11 35 36
3 15 33 34
3 19 31 32
3 23 29 30
4 1 26 27 28
5 2 9 22 24 25
5 6 17 18 20 21
8 4 5 8 10 12 13 14 16
*/
hdu5355 Cake的更多相关文章
- hdu5355 Cake(构造)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Cake Time Limit: 2000/1000 MS (Java/Other ...
- hdu5355 思维+爆搜
pid=5355">http://acm.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m sod ...
- Windows 7上执行Cake 报错原因是Powershell 版本问题
在Windows 7 SP1 电脑上执行Cake的的例子 http://cakebuild.net/docs/tutorials/getting-started ,运行./Build.ps1 报下面的 ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- Scalaz(15)- Monad:依赖注入-Reader besides Cake
我们可以用Monad Reader来实现依赖注入(dependency injection DI or IOC)功能.Scala界中比较常用的不附加任何Framework的依赖注入方式可以说是Cake ...
- uva10167 Birthday Cake
Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them. Now we put t ...
- HDU 4762 Cut the Cake(公式)
Cut the Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- Brute Force --- UVA 10167: Birthday Cake
Problem G. Birthday Cake Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudg ...
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest, B. Layer Cake
Description Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping ...
随机推荐
- css 引入方式
css介绍 现在的互联网前端分三层: HTML:超文本标记语言.从语义的角度描述页面结构. CSS:层叠样式表.从审美的角度负责页面样式. JS:JavaScript .从交互的角度描述页面行为 CS ...
- Spring Boot (3) 热部署devtools
热部署:当发现程序修改时自动启动应用程序. spring boot为开发者提供了一个名为spring-boot-devtools的模块来使sring boot应用支持热部署,提高开发者的开发效率,无需 ...
- [原创]PM2使nodejs应用随系统启动
1.安装 npm install pm2 -gd 2.启动 pm2 start ./bin/www 3.pm2 save 4.pm2 startup 5.pm2 save 注:上面几个步骤有几个坑: ...
- C#自动缩进排列代码的快捷键 c# 代码重新排版 变整齐
C#自动缩进排列代码的快捷键: ctrl + k + d 1.小技巧, 可以把最后一个}去掉, 重新写下,就可以达到排版的效果. 2.快捷键:编辑-高级-设置文档的格式 快捷键Ctrl+E,D,设置 ...
- Caffe2:ubuntuKylin17.04使用Caffe2.LSTM
一早发现caffe2的较成熟的release版发布了(the first production-ready release),那么深度学习平台在之后一段时间也是会出现其与tensorflow相互竞争的 ...
- 虚拟机+linux+大杂烩
出于项目需要,需要用到linux系统.这玩意儿平时很少用,要说体验度还是windows更人性化一些. 1.虚拟机的安装,这个没说的,百度VMware直接下最新版安装就好. 2.接着是linux系统的安 ...
- 【sqli-labs】 less26a GET- Blind based -All you SPACES and COMMENTS belong to us -String-single quotes-Parenthesis(GET型基于盲注的去除了空格和注释的单引号括号注入)
这个和less26差不多,空格还是用%a0代替,26过了这个也就简单了 ;%00 可以代替注释,尝试一下 order by 3 http://192.168.136.128/sqli-labs-mas ...
- rev
功能说明:反向输出文件内容. 字符串反转 文本反转
- java中反射讲解及实例
Java反射机制详解 java 反射 定义 功能 示例 概要: Java反射机制详解 | |目录 1反射机制是什么 2反射机制能做什么 3反射机制的相关API ·通过一个对象获得完整的包名和类名 ·实 ...
- Java导出Excel(附完整源码)
导出excel是咱Java开发的必备技能啦,之前项目有这个功能,现在将其独立出来,分享一下.所用技术就是SpringBoot,然后是MVC架构模式.废话不多说,直接上代码了,源码点末尾链接就可以下载. ...