HDU 1258 Sum It Up (DFS)
Sum It Up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2982 Accepted Submission(s): 1493
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; int n,k,a[],flag,res[]; void DFS(int cur,int sum,int cnt){
if(sum>n)
return ;
if(sum==n){
printf("%d",res[]);
for(int i=;i<cnt;i++)
printf("+%d",res[i]);
printf("\n");
flag=;
}
for(int i=cur;i<k;i++){
res[cnt]=a[i];
DFS(i+,sum+a[i],cnt+);
while(i+<k && a[i]==a[i+]) //去除相同的分解式
i++;
}
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d",&n,&k) && n){
for(int i=;i<k;i++)
scanf("%d",&a[i]);
printf("Sums of %d:\n",n);
flag=;
DFS(,,);
if(!flag)
printf("NONE\n");
}
return ;
}
HDU 1258 Sum It Up (DFS)的更多相关文章
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- HDU 1258 Sum It Up(dfs 巧妙去重)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1258 Sum It Up (dfs+路径记录)
pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 2553 N皇后问题(dfs)
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 在 ...
- POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)
题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...
- hdu 1016 Prime Ring Problem(dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 5423 Rikka with Tree(dfs)bestcoder #53 div2 1002
题意: 输入一棵树,判断这棵树在以节点1为根节点时,是否是一棵特殊的树. 相关定义: 1. 定义f[A, i]为树A上节点i到节点1的距离,父节点与子节点之间的距离为1. 2. 对于树A与树B,如 ...
- HDU 1312 Red and Black (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
- hdu 5423 Rikka with Tree(dfs)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
随机推荐
- MySQL在线大表DDL操作 (转)
http://www.cnblogs.com/janehoo/p/5382474.html 线大表DDL操作的方法: 1.主从架构轮询修改 需要注意: a.主库会话级别的记录binglog的参数关闭 ...
- JS 判断浏览器是否安装Flash 兼容IE、firefox
/** * @Author: HTL * @Email: Huangyuan413026@163.com * @DateTime: 2016-06-02 11:37:05 * @Description ...
- unity3d shader 学习
[浅墨Unity3D Shader编程] 着色器参考 [Unity Shaders]
- node.js " The requested service provider could not be loaded or initialized"
I'm trying to use any of the NodeJS or NPM commands but I always got the following error: socket: (1 ...
- ThinkPHP3.2多域名 Virtual .htaccess 匹配 RewriteCond %{HTTP_HOST}
<Files *> Options -Indexes </Files> <IfModule mod_headers.c> <FilesMatch " ...
- maven 添加jar到中央/远程仓库
maven 添加jar到中央/远程仓库 开源中国 发表于 2014-08-23 00:08:00 commond: mvn deploy:deploy-file -DgroupId=com.tima. ...
- PHPCMS标签
获取栏目名:{$CAT[catname]}或{$catname}获取父栏目名:{$CATEGORYS[$CAT[parentid]][catname]} 获取同级栏目列表: {pc:content a ...
- Unity3d 屏幕截图。并保存。iOS
- ( void ) imageSaved: ( UIImage *) image didFinishSavingWithError:( NSError *)error contextInfo: ( ...
- python网络爬虫 - 设定重试次数内反复抓取
import urllib.request def download(url, num_retries=2): print('Downloading:', url) try: html = urlli ...
- TOJ 3365 ZOJ 3232 It's not Floyd Algorithm / 强连通分量
It's not Floyd Algorithm 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 描述 When a directed grap ...