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. Hadoop1.0 与Hadoop2.0

    Hadoop1.0的局限-MapReduce •扩展性 –集群最大节点数–4000 –最大并发任务数–40000 (当 map-reduce job 非常多的时候,会造成很大的内存开销,潜在来说,也增 ...

  2. Libcurl交叉编译

    目录 配置configure 执行make 取得su权限 开始安装 踩坑总结 配置configure ./configure --build=arm --host=mipsel-openwrt-lin ...

  3. STM32 USB设备描述符、配置描述符、端点描述符含义

    查了一整天的资料,自己把不懂的全部试了一遍 一下是程序以及注释 /* USB设备描述符*/ const uint8_t CustomHID_DeviceDescriptor[CUSTOMHID_SIZ ...

  4. go包管理工具glide使用方法

    golang没有官方最佳管理方案,在go的世界里存在大量的自制解决方案. go语言的包是没有中央库统一管理的,通过使用go get命令从远程代码库(github.com,goolge code 等)拉 ...

  5. BZOJ1029_建筑抢修_KEY

    题目传送门 这是一道贪心的问题. 总体做法是这样的:先按照报废的快慢从小到大SORT一遍,优先修报废快的.同时开一个大根堆(C++的朋友可以用priority_queue),用来记录已经修了的建筑的耗 ...

  6. photoshop cc 2018安装破解教程(破解补丁,亲测,绝对可用)

    破解步骤说明:下载地址百度网盘,https://pan.baidu.com/s/1cWtpUesl2fms3tFwEC0MiQ 1.右键解压Adobe Photoshop CC 2018 64位这个文 ...

  7. fastCMS八大核心对象

    fastCMS内置system对象,该对象包含八大核心对象,应用于不同的操作场景,分别是: 1.system.string 对象(处理字符类操作) 2.system.number 对象(处理数字类操作 ...

  8. JVM--内存模型与线程

    一.硬件与效率的一致性 计算机的存储设备与处理器的运算速度存在几个数量级的差距,现在计算机系统不得不在内存和处理器之间增加一层高速缓存(cache)来作为缓冲.将运算需要的数据复制到缓存中,让运算能够 ...

  9. 利用maven进行项目管理

    下面为maven项目管理的一个结构 首先pom是路径文件,我们在编译或是运行程序时调用到jdk或一些自己写的jar包时会需要指明物理路径,这里的pom是一样的道理,同时在maven的管理下多出来了一些 ...

  10. 正式放弃Edge,重新拥抱Chrome

    从Edge还叫斯巴达的时候我就开始用了,本来对浏览器的要求也没多高,能够打开多个选项卡,稳定,支持最新的规范就好了. 但是Edge真的是越来越让我失望了,卡死问题越来越多,崩溃越来越频繁,我也快奔溃了 ...