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 ...
随机推荐
- 取消VS2017窗口置顶
今天打开VS2017,莫名其妙窗口置顶了,百度了一下如何取消窗口置顶,就是Ctrl+Alt+Esc组合键,就可以取消窗口置顶了,至于到底怎么会突然置顶的我也不知道emmm... /********** ...
- 5.29clone项目地址
- Java创建Excel-DEMO
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.Out ...
- js实现“级联菜单”
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- numpy安装失败-小失误
1. 古老的方法: 安装python numpy库AMD64 失败,网上的教程是这样的:http://www.cnblogs.com/zhuyp1015/archive/2012 ...
- Polymorphism (computer science)
In programming languages and type theory, polymorphism (from Greek πολύς, polys, "many, much&qu ...
- BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会 树形DP + 带权重心
Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1<=N<=100,0 ...
- 7.5 pragma 指令
第1个pragma禁用了我们感兴趣的特别警告,第2个恢复了它.禁用尽可能小的一段代码的警告是一个良好的做法,以便你不会错过任何真正应该修复的错误.如果你想在单独一行上禁用或恢复多个警告,那么只需用逗号 ...
- 软件工程1916|W(福州大学)_助教博客】团队Beta冲刺作业(第9次)成绩公示
1. 作业链接: 项目Beta冲刺(团队) 2. 评分准则: 本次作业包括现场Beta答辩评分(映射总分为100分)+团队互评分数(总分40分)+博客分(总分130分)+贡献度得分,其中博客分由以下部 ...
- 共享内存、网络(day13)
一.共享内存 .获取一个键值 ftok() .使用键值获取共享内存的id shmget() #include <sys/ipc.h> #include <sys/shm.h> ...