pid=5355">http://acm.hdu.edu.cn/showproblem.php?pid=5355

Problem Description
There are m soda
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.

 

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

 

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

 

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
/**
hdu5355 思维+爆搜
题目大意:1~n这n个数分成m份。问能否成功,若能请给出一种分配方式
解题思路:假设sum(1~n)%m!=0无解,而且sum/m<n亦无解,其它情况有解。令ans=n%(2*m),若ans=0,则一个有效方案为(1+n),(2+n-1),(3+n-2)....
每m个为一组,若ans!=0:令cnt=min(n,ans+(2*m)),这时候的cnt非常小我们爆搜就攻克了。关于爆搜我们总共要搜出m组。搜的时候枚举
每组的最小的数,然后从该数開始往大了搜索
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long LL; vector<int>vec[55];
LL n,m,dis,ans;
int vis[105],pos[105],num[25]; bool dfs(int cnt,int sum,int id)///ans个数分成和相等的m份(id为第cnt份中最小的数。爆搜)
{
if(cnt==m+1)return true;
for(int i=ans;i>=id;i--)
{
if(vis[i])continue;
if(sum+i==dis)
{
pos[i]=cnt;
vis[i]=1;
if(dfs(cnt+1,0,1))
return true;
vis[i]=0;
return false;
}
else if(sum+i<dis)
{
pos[i]=cnt;
vis[i]=1;
if(dfs(cnt,sum+i,i+1))
return true;
vis[i]=0;
}
}
return false;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&m);
LL sum=n*(n+1)/2;
if(sum%m)
{
puts("NO");
continue;
}
LL ave=sum/m;
if(ave<n)
{
puts("NO");
continue;
}
puts("YES");
ans=n%(2*m);
if(ans!=0)
{
ans+=2*m;
ans=min(ans,n);
}
for(int i=1;i<=m;i++)
{
vec[i].clear();
}
memset(num,0,sizeof(num));
for(int i=n;i>ans;i-=(2*m))///分成2*m份
{
for(int j=1;j<=m;j++)
{
int x=i-j+1;
int y=i-2*m+j;
vec[j].push_back(x);
vec[j].push_back(y);
num[j]+=(x+y);
}
}
dis=ave-num[1];
memset(vis,0,sizeof(vis));
memset(pos,0,sizeof(pos));
dfs(1,0,1);
for(int i=1;i<=ans;i++)
{
vec[pos[i]].push_back(i);
}
for(int i=1;i<=m;i++)
{
printf("%d",vec[i].size());
for(int j=0;j<vec[i].size();j++)
{
printf(" %d",vec[i][j]);
}
printf("\n");
}
}
return 0;
}

hdu5355 思维+爆搜的更多相关文章

  1. 【BZOJ-1853&2393】幸运数字&Cirno的完美算数教室 容斥原理 + 爆搜 + 剪枝

    1853: [Scoi2010]幸运数字 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 1817  Solved: 665[Submit][Status] ...

  2. POJ 1166 The Clocks (爆搜 || 高斯消元)

    题目链接 题意: 输入提供9个钟表的位置(钟表的位置只能是0点.3点.6点.9点,分别用0.1.2.3)表示.而题目又提供了9的步骤表示可以用来调正钟的位置,例如1 ABDE表示此步可以在第一.二.四 ...

  3. 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)

    Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...

  4. hdu5323 Solve this interesting problem(爆搜)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Solve this interesting problem Time Limit ...

  5. hdu4536-XCOM Enemy Unknown(爆搜)

    XCOM-Enemy Unknown是一款很好玩很经典的策略游戏. 在游戏中,由于未知的敌人--外星人入侵,你团结了世界各大国家进行抵抗.随着游戏进展,会有很多的外星人进攻事件.每次进攻外星人会选择3 ...

  6. poj1077 Eight【爆搜+Hash(脸题-_-b)】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298840.html   ---by 墨染之樱花 题目链接:http://poj.org/pr ...

  7. [NOIP2015] 斗地主 大爆搜

    考试的时候想了半天,实在是想不到解决的办法,感觉只能暴力..然后暴力也懒得打了,小数据模拟骗30分hhh 然而正解真的是暴力..大爆搜.. 然后我的内心拒绝改这道题(TAT) 不过在wcx大佬的帮助下 ...

  8. BZOJ 1207: [HNOI2004]打鼹鼠【妥妥的n^2爆搜,dp】

    1207: [HNOI2004]打鼹鼠 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3259  Solved: 1564[Submit][Statu ...

  9. HDU 4403 A very hard Aoshu problem(dfs爆搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比 ...

随机推荐

  1. 服务器通信REST、gRPC,Swagger/OpenAPI

    服务间的通信方式是在采用微服务架构时需要做出一个最基本的决策.默认的选项是通过 HTTP 发送 JSON,也就是所谓的 REST API.我们也是从 REST 开始的,但最近我们决定改用 gRPC. ...

  2. 3d数学 7 矩阵

    7.1 矩阵-数学定义 在线性代数中, 矩阵就是以行和列形式组织的矩形数字块.矩阵是向量的数组. 7.1.1 矩阵的维度和记法 矩阵的维度被定义为它包含了多少行和多少列.一个\(r \times c\ ...

  3. shell 实用命令学习

    查找文件 -iname 大小写不敏感 “*.log” .log后缀的文件 -type d 文件类型为目录的 find ./ -name 'index.html' 查找当前目录,及其子目录下文件

  4. C#之单列双列集合绑定数据

    ---恢复内容开始--- 1.单列集合绑定方式 davList.DataSource=new BindingList<类型名>(集合名); 2.双列集合绑定方式 BindingSource ...

  5. jquery.validate验证text,checkbox,radio,selected

    index.cshtml <form id="formLogin" method="post"> <div> <label for ...

  6. java Queue中 remove/poll, add/offer, element/peek区别

    offer,add区别: 一些队列有大小限制,因此如果想在一个满的队列中加入一个新项,多出的项就会被拒绝. 这时新的 offer 方法就可以起作用了.它不是对调用 add() 方法抛出一个 unche ...

  7. Unity引擎GUI之Slider和Scrollbar

    Slider(滑动条):是一个主要用于形象的拖动以改变目标值的控件,他的最恰当应用是用来改变一个数值,最大值和最小值自定义,拖动滑块可在此之间改变,例如改变声音大小. Scrollbar(滚动条):是 ...

  8. PL/SQL之基础篇

    参考文献:<Oracle完全学习手册>第11章 1.PL/SQL概述 PL/SQL(Procedure Language/Structuer Query Language)是Oracle对 ...

  9. Haar、pico、npd、dlib等多种人脸检测特征及算法结果比较

    原文:opencv.pico.npd.dlib.face++等多种人脸检测算法结果比较 NDP检测结果: 结果分析: Pico(Pixel Intensity Comparison-based Obj ...

  10. [Java]链表的打印,反转与删除

    class Node{ public int value; public Node next=null; public Node(int value) { this.value=value; } }p ...