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. 1222/2516. Kup

    题目描述 Description 首先你们得承认今天的题目很短很简洁... 然后,你们还得承认接下来这个题目的描述更加简洁!!! Task:给出一个N*N(1≤N≤2000)的矩阵,还给出一个整数K. ...

  2. js 中使用typeof

    >typeof(null) <"object" 对null执行typeof预算,结果返回字符串'object',也就是说,可以将null认为是一个特殊的对象值,含义是“ ...

  3. springboot 集成apollo,根据不同环境加载配置

  4. Python模块之-OS模块

    一.os模块概述 Python os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.(一语中的) 二.常用方法 1.os.name 输出字符串指示正在使用的平台 ...

  5. window.location.href 兼容性问题 (ie 浏览器下设置失效)

    window.location.href 兼容性问题 (ie 下设置失效) window.location.href = "../index.html" (ie 浏览器失效) wi ...

  6. vue相关基础知识

    参考文章: Vue 项目里戳中你痛点的问题及解决办法(更新) vue中前进刷新.后退缓存用户浏览数据和浏览位置的实践

  7. Maven开发环境搭建

    配置Maven流程: 1.下载Maven,官网:http://maven.apache.org/ 2.安装到本地: 1 ).解压apache-maven-x.x.x-bin.zip文件 2 ).配置M ...

  8. ES6注

    1.Promise构造函数 //resolve(成功),reject(失败)两个参数 function runAsync(){ var p = new Promise(function(resolve ...

  9. leetcode-mid-sorting and searching - 33. Search in Rotated Sorted Array

    mycode class Solution(object): def search(self, nums, target): """ :type nums: List[i ...

  10. C#通用类库

    http://www.cnblogs.com/feiyangqingyun/archive/2010/12/20/1911630.html