poj 1564 Sum It Up
题目连接
http://poj.org/problem?id=1564
Sum It Up
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 x 1 , . . . , x n . 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 x 1 , . . . , x n 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 distinct; the same sum cannot 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
dfs。。
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<set>
using std::set;
using std::sort;
using std::pair;
using std::swap;
using std::queue;
using std::multiset;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr, val) memset(arr, val, sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = 1010;
const int INF = 0x3f3f3f3f;
typedef unsigned long long ull;
bool f;
int n, tot, tar, A[N], B[N];
void dfs(int cur, int ret, int k) {
if (ret == tar) {
f = true;
for (int i = 0; i < k; i++) {
if (!i) printf("%d", B[i]);
else printf("+%d", B[i]);
}
putchar('\n');
return;
}
for (int i = cur; i < n; i++) {
if (i == cur || A[i] != A[i - 1]) { // 判重
B[k] = A[i];
dfs(i + 1, ret + A[i], k + 1);
}
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
while (~scanf("%d %d", &tar, &n), n) {
f = false;
rep(i, n) scanf("%d", &A[i]);
printf("Sums of %d:\n", tar);
dfs(0, 0, 0);
if (!f) { puts("NONE"); continue; }
}
return 0;
}
poj 1564 Sum It Up的更多相关文章
- poj 1564 Sum It Up (DFS+ 去重+排序)
http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- poj 1564 Sum It Up【dfs+去重】
Sum It Up Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6682 Accepted: 3475 Descrip ...
- POJ 1564 Sum It Up(DFS)
Sum It Up Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ 1564 Sum It Up (DFS+剪枝)
...
- poj 1564 Sum It Up 搜索
题意: 给出一个数T,再给出n个数.若n个数中有几个数(可以是一个)的和是T,就输出相加的式子.不过不能输出相同的式子. 分析: 运用的是回溯法.比较特殊的一点就是不能输出相同的式子.这个可以通过ma ...
- poj 1564 Sum It Up(dfs)
Sum It Up Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7191 Accepted: 3745 Descrip ...
- POJ 1564 经典dfs
1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #in ...
- ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法
POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS Memory Limit:65536KB 64bit IO Fo ...
随机推荐
- 如何在Flash中新窗口打开页面而不被拦截
Flash的wmode必须是opaque或者transparent,允许Flash访问页面脚本.另外跳转必须是点击直接触发. 代码:ExternalInterface.call("windo ...
- 支撑5亿用户、1.5亿活跃用户的Twitter最新架构详解及相关实现
如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 摘要:Twitter出道之初只是个奋斗在RoR上的小站点,而如今已拥有1.5亿的活跃 ...
- UVa11054 Gergovia的酒交易 Wine trading in Gergovia-递推
https://vjudge.net/problem/UVA-11054 As you may know from the comic “Asterix and the Chieftain’s Shi ...
- 洛谷P1206 [USACO1.2]回文平方数 Palindromic Squares
P1206 [USACO1.2]回文平方数 Palindromic Squares 271通过 501提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 暂时没有 ...
- 注册表 锁IE首页
用附件中的修改软件,或者用以下修改注册表的办法. 一.注册表被修改的原因及解决办法 其实,该恶意网页是含有有害代码的ActiveX网页文件,这些广告信息的出现是因为浏览者的注册表被恶意更改的结果. ...
- python os模块sys模块常用方法
官方文档看这里 https://docs.python.org/3.5/library/os.html http://www.cnblogs.com/wupeiqi/articles/5501365. ...
- ios开发之OC基础-类和对象
本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...
- Windows phone 8 学习笔记(4) 应用的启动(转)
Windows phone 8 的应用除了可以直接从开始菜单以及应用列表中打开外,还可以通过其他的方式打开.照片中心.音乐+视频中心提供扩展支持应用从此启动.另外,我们还可以通过文件关联.URI关联的 ...
- JS基础学习1——什么是基础js类和原型?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 为Magento2新主题添加使用Grunt
Go to \dev\tools\grunt\configs, open your themes.js file, and change it according to the following e ...