Sum It Up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 6720    Accepted Submission(s): 3535

Problem Description
Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four different sums that equal 4: 4,3+1,2+2,
and 2+1+1.(A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.
 
Input
The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x1,...,xn. If n=0 it signals the end of the input; otherwise,
t will be a positive integer less than 1000, n will be an integer between 1 and 12(inclusive), and x1,...,xn will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order,
and there may be repetitions.
 
Output
For each test case, first output a line containing 'Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line 'NONE'. The numbers within each sum must appear in nonincreasing order.
A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums
with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distince; the same sum connot appear twice.
 
Sample Input
4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0
 
Sample Output
Sums of 4:
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
 
Source
Problem : 1258 ( Sum It Up )     Judge Status : Accepted

RunId : 21150701    Language : G++    Author : hnustwanghe

Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>

using namespace std;

const int N = 50+5;
int x,n,a[N],save[N],pos;
bool flag;
bool cmp(const int x,const int y){
return x>y;
}
void DFS(int sum,int d){
if(sum>x) return;
if(sum==x){
flag = false;
for(int i=0;i<pos-1;i++)
printf("%d+",save[i]);
printf("%d\n",save[pos-1]);
return ;
}
int last = -1;
for(int i=d+1;i<=n;i++){
if(a[i]!=last){
save[pos++] = a[i];
last = a[i];
DFS(sum+a[i],i);
pos--;
}
}
}
int main(){
while(scanf("%d %d",&x,&n)==2 &&(x||n)){
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1,cmp);
flag = true;
printf("Sums of %d:\n",x);
DFS(0,0);
if(flag) printf("NONE\n");
}
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>

using namespace
std; const int N = 50+5;
int
x,n,a[N],save[N],pos;
bool
flag;
bool
cmp(const int x,const int y){
return
x>y;
}
void
DFS(int sum,int d){
if(
sum>x) return;
if(
sum==x){
flag = false;
for(int
i=0;i<pos-1;i++)
printf("%d+",save[i]);
printf("%d\n",save[pos-1]);
return ;
}
int
last = -1;
for(int
i=d+1;i<=n;i++){
if(
a[i]!=last){
save[pos++] = a[i];
last = a[i];
DFS(sum+a[i],i);
pos--;
}
}
}
int main(){
while(
scanf("%d %d",&x,&n)==2 &&(x||n)){
for(int
i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1,cmp);
flag = true;
printf("Sums of %d:\n",x);
DFS(0,0);
if(
flag) printf("NONE\n");
}
}

搜索专题: HDU1258Sum It Up的更多相关文章

  1. HDU(搜索专题) 1000 N皇后问题(深度优先搜索DFS)解题报告

    前几天一直在忙一些事情,所以一直没来得及开始这个搜索专题的训练,今天做了下这个专题的第一题,皇后问题在我没有开始接受Axie的算法低强度训练前,就早有耳闻了,但一直不知道是什么类型的题目,今天一看,原 ...

  2. NOIP2018提高组金牌训练营——搜索专题

    NOIP2018提高组金牌训练营——搜索专题 1416 两点 福克斯在玩一款手机解迷游戏,这个游戏叫做”两点”.基础级别的时候是在一个n×m单元上玩的.像这样: 每一个单元有包含一个有色点.我们将用不 ...

  3. 搜索专题:Balloons

    搜索专题:Balloons 这道题一看与时间有关,第一想到的就是BFS,定义一个状态,包含每一个状态的剩余气球数,已经进行的时间和每一个志愿者上一次吹气球的时间: 每一次状态转换时,检查是否有没有使用 ...

  4. 2014 UESTC暑前集训搜索专题解题报告

    A.解救小Q BFS.每次到达一个状态时看是否是在传送阵的一点上,是则传送到另一点即可. 代码: #include <iostream> #include <cstdio> # ...

  5. 【PHP高效搜索专题(2)】sphinx&coreseek在PHP程序中的应用实例

    PHP可以通过三种途径来调用sphinx 通过Sphinx官方提供的API接口(接口有Python,Java,Php三种版本) 通过安装SphinxSE,然后创建一个中介sphinxSE类型的表,再通 ...

  6. 【PHP高效搜索专题(1)】sphinx&Coreseek的介绍与安装

    我们已经知道mysql中带有"%keyword%"条件的sql是不走索引的,而不走索引的sql在大数据量+大并发量的时候,不仅效率极慢还很有可能让数据库崩溃.那我们如何通过某些关键 ...

  7. 2015 UESTC 搜索专题F题 Eight Puzzle 爆搜

    Eight Puzzle Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 ...

  8. 2015 UESTC 搜索专题B题 邱老师降临小行星 记忆化搜索

    邱老师降临小行星 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Des ...

  9. 蓝桥杯dfs搜索专题

    2018激光样式 #include<bits/stdc++.h> using namespace std; /* dfs(i) 第i个激光机器 有两种选择:vis[i-1] == 0 时 ...

随机推荐

  1. Comet OJ - Contest #12 D

    题目描述 求(x,y)的对数满足x∈[0,a],y∈[0,b],x⊕y=0且|x-y|<=m 题解 一种比较sb的做法是考虑x-y的借位,根据借位以及差值进行转移 还有一种比较正常的做法,假设一 ...

  2. 16. ClustrixDB Rebalancer

    管理平衡 Clustrix Rebalancer被设计成自动作为后台进程运行,以便跨集群重新平衡数据.介绍如何配置和监视rebalancer,但是大多数部署不需要用户干预. Rebalancer主要通 ...

  3. 【Leetcode】二叉树的层次遍历

    题目: 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 例如: 思路:采用宽度优先搜索. 时间复杂度:O(n).n为节点的数量,遍历所有节 ...

  4. yii2.0 curd操作数据写法

    一.执行原生sql查询,创建yii\db\Command         insert(),update(),delete()直接构建,相应的sql语句 查: 1.查询一条 \Yii::$app-&g ...

  5. 读取hibernate.cfg.xml文件

    new Configuration().configure().buildSessionFactory() new Configuration()默认是读取hibernate.properties 所 ...

  6. spring boot 和 mybatis集成

    1.pom.xml 添加mybatis和mysql依赖 <!-- 添加 MyBatis --> <dependency> <groupId>org.mybatis. ...

  7. Linux清空文本内容

    测试文件:test.txt 第一种: $> test.txt 第二种: $echo "" > test.txt 第三种: $cat /dev/null > tes ...

  8. C++ 左值与右值

    https://baike.baidu.com/item/%E5%B7%A6%E5%80%BC%E4%B8%8E%E5%8F%B3%E5%80%BC/5537417?fr=aladdin https: ...

  9. Vue知识整理8:条件、URL、点击

    1.通过v-if实现对条件的判断和执行: 2.通过v-bind:href实现对url地址的绑定,其中url写在data中: 3.通过@click="click1"实现点击事件,其中 ...

  10. 用ajax提交请求,预期Json返回 406错误的解决办法!

    正常情况下在Controller端已经配置好了 @ResponseBody    @RequestMapping  返回Json格式数据 发生406错误 ,应该检查提交的请求路径是否含有 .html ...