https://vjudge.net/contest/67836#problem/H

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 file 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

时间复杂度:$O(2^n)$

题解:dfs , 按照字典序输出

代码:

#include <bits/stdc++.h>
using namespace std; int t, n, add, cnt;
int a[15], key[15], vis[15]; struct Ans{
int b[15];
int len;
}ans[4200];
int sz; bool cmp2(const Ans& a, const Ans& b) {
for(int i = 0; i < min(a.len, b.len); i ++) {
if(a.b[i] != b.b[i]) return a.b[i] > b.b[i];
}
return a.len > b.len;
} bool cmp(int n1, int n2) {
return n1 > n2;
} void dfs(int x, int sum) {
if(sum > t) return;
if(x == n + 1) {
if(sum == t) {
ans[sz].len = 0;
for(int i = 1; i <= n; i ++) {
if(key[i]) {
ans[sz].b[ans[sz].len ++] = a[i];
}
}
sz ++;
}
return;
}
key[x] = 1;
dfs(x + 1, sum + a[x]);
key[x] = 0;
dfs(x + 1, sum);
} int main() {
while(~scanf("%d %d", &t, &n)) {
if(n == 0)
break; for(int i = 1; i <= n; i ++) {
scanf("%d", &a[i]);
} sort(a + 1, a + 1 + n, cmp);
sz = 0;
dfs(1, 0);
printf("Sums of %d:\n", t);
if(sz) {
sort(ans, ans + sz, cmp2);
for(int i = 0; i < sz; i ++) {
int fail = 1;
if(i == 0) fail = 0;
else {
if(ans[i].len != ans[i - 1].len) fail = 0;
for(int j = 0; j < ans[i].len; j ++) {
if(ans[i].b[j] != ans[i - 1].b[j])
fail = 0;
}
}
if(fail)
continue; for(int j = 0; j < ans[i].len; j ++) {
if(j != 0) printf("+");
printf("%d", ans[i].b[j]);
}
printf("\n");
}
} else {
printf("NONE\n");
}
}
return 0;
}

  

ZOJ 1711 H-Sum It Up的更多相关文章

  1. 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 ...

  2. POJ 1564(HDU 1258 ZOJ 1711) Sum It Up(DFS)

    题目链接:http://poj.org/problem?id=1564 题目大意:给定一个整数t,和n个元素组成的集合.求能否用该集合中的元素和表示该整数,如果可以输出所有可行解.1<=n< ...

  3. poj1564 Sum It Up (zoj 1711 hdu 1258) DFS

    POJhttp://poj.org/problem?id=1564 ZOJhttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=711 ...

  4. POJ 1775 (ZOJ 2358) Sum of Factorials

    Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematic ...

  5. zoj 3813 Alternating Sum(2014ACMICPC Regional 牡丹江站网络赛 E)

    1.http://blog.csdn.net/dyx404514/article/details/39122743 思路:题目意思很清楚了,这里只说思路. 设区间[L,R],区间长度为len=(R-L ...

  6. URAL 1146 Maximum Sum 最大子矩阵和

    题目:click here #include <bits/stdc++.h> using namespace std; typedef unsigned long long ll; con ...

  7. Uva10290 - {Sum+=i++} to Reach N

    Problem H {sum+=i++} to Reach N Input: standard input Output:  standard output Memory Limit: 32 MB A ...

  8. ZOJ 2059 The Twin Towers

    双塔DP. dp[i][j]表示前i个物品,分成两堆(可以不全用),价值之差为j的时候,较小一堆的价值为dp[i][j]. #include<cstdio> #include<cst ...

  9. [leetcode-560-Subarray Sum Equals K]

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...

随机推荐

  1. Zabbix 3.4.11监控 apache服务,ftp服务的配置

    一 zabbix 的安装部署 略 二监控 apache服务的配置 首先在本机下载模板:https://github.com/rdvn/zabbix-templates/archive/m aster. ...

  2. A1041

    输入n个数,找出第一个只出现一次的数,输出它. 如果没有,输出none. 思路: 将输入的数值作为HashTable的数组下标即可. #include<cstdio> ], hashTab ...

  3. 第一章:程序设计和C语言

    一.什么是计算机程序? 所谓程序就是一组计算机能识别和执行的指令.计算机的一切操作都是由程序控制的,本质是程序的机器,程序和指令是计算机系统最基本的概念. 二.什么是计算机语言? 人和计算机交流信息要 ...

  4. 2017Facebook面试题改编“一面砖墙 ”

    题目:一面砖墙 这道题改编自网上Facebook去年的一道面试题,是hihoCoder的1494题(https://hihocoder.com/problemset/problem/1494) 这道题 ...

  5. (转)service apache2 restart失败

    https://askubuntu.com/questions/431925/how-to-restart-apache2-when-i-get-a-pid-conflict sudo kill -9 ...

  6. python是一门解释性语言吗?

    其实这只能算说对了一半,准确来说是编译跟解释性语言.python跟java.C# 一样都是会预编译一部分代码(简称做了优化) 都知道java编译要先在cmd里敲 javac hello.world 是 ...

  7. AS 3.1 项目打包成jar或aar

    1.首先明白一个道理. Android Studio编译的时候会自动将项目生成jar和aar的,我一开始以为jar需要自己单独生成,其实AS已经自动生成了,网上找的很多资料都是一个复制的过程而已. 只 ...

  8. DevExpress 操作gridcontrol

    XtraGrid的关键类就是:GridControl和GridView.GridControl本身不显示数据,数据都是显示在GridView/CardView/XXXXView中.GridContro ...

  9. 利用Python Counter快速计算出现次数topN的元素

    需要用Python写一段代码,给定一堆关键词,返回出现次数最多的n个关键字. 第一反应是采用一个dict,key存储关键词,value存储出现次数,如此一次遍历即可得出所有不同关键词的出现次数,而后排 ...

  10. SLAM前沿问题梳理

    鲁棒性问题:数据关联是影响系统鲁棒性的主要原因 特征提取.线特征 短期内的数据关联是最容易处理的,新的研究方向包括特征提取.线特征等. 回环检测 对于前端的环闭合检测,检测当前测量中的特征并试图将它们 ...