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. docker跨主机通信-overlay

    使用consul 1,让两个网络环境下的容器互通,那么必然涉及到网络信息的同步,所以需要先配置一下consul. 直接运行下面命令.启动consul. docker run -d -p 8500:85 ...

  2. linux-ssh加密与https安全-9

    非对称加密算法:RSA,DSA/DSS 对称加密算法:AES,RC4,3DES HASH算法:MD5,SHA1,SHA256 hash就是找到一种数据内容和数据存放地址之间的映射关系 (1) 文件校验 ...

  3. oracle 7.4安装nvidia驱动

    2019-8-28 参考网页: 如何在k8s集群中安装nvidia.cuda并使用GPU进行训练 https://blog.csdn.net/u013042928/article/details/78 ...

  4. Qt类图

    Qt对象命名与类图 QWidget=Windows get,即获得窗体,凡是我们能看到的界面都是从QWidget继承而来的. QDialog QAbstractButton 这是一个抽象类,不能直接使 ...

  5. 阿里云ECS服务器socket无法连接的问题

    把自己的项目部署到阿里云ecs服务器之后,只有127.0.0.1才能连接到服务器端,检查了阿里云安全组规则,以及socket绑定的地址无误后,发现没有开启服务器防火墙的对应端口. firewall-c ...

  6. OverFeat:基于卷积网络的集成识别、定位与检测

    摘要:我们提出了一个使用卷积网络进行分类.定位和检测的集成框架.我们展示了如何在ConvNet中有效地实现多尺度和滑动窗口方法.我们还介绍了一种新的深度学习方法,通过学习预测对象边界来定位.然后通过边 ...

  7. EcShop开发手册

    Ecshop文件结构 ecshop文件架构说明 ECShop 结构图及各文件相应功能介绍 ECShop upload 的目录 ┣ activity.php 活动列表 ┣ affiche.php 广告处 ...

  8. Spring Data Jpa (四)注解式查询方法

    详细讲解声明式的查询方法 1 @Query详解 使用命名查询为实体声明查询是一种有效的方法,对于少量查询很有效.一般只需要关心@Query里面的value和nativeQuery的值.使用声明式JPQ ...

  9. Jprofiler远程监控JVM

    一.下载并安装 本地和远程服务器分别安装Jprofiler,下载地址 二.Windows远程连接JVM配置 1.打开Windows客户端Jprofiler 2.点Cancel 3.创建远程会话 4.添 ...

  10. 如何设置linux bash终端的字符显示内容和颜色?

    通常linux有1-6个字符终端 tty, 有1个图形终端. 通常用 ctrl+alt+f1 到f6是字符终端, ctrl+alt+f7为图形终端, 但是, 也有不一样的, 如: fedora的4.0 ...