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. IE8 如何 不显示 选项 卡,直接在任务显示 各个页面?

    IE8  如何 不显示 选项 卡,直接在任务显示 各个页面? 在  工具->Internet 选项(o) ->常规--选项卡-设置->把第一个勾去掉  “启用选项卡浏览(需要重新启动 ...

  2. hive 查看表结构和属性

    1.查看数据库/表 show databases/tables; 2.切换数据库 use database_name; 3.查看表结构 desc table_name; 4.查看表详细属性 desc ...

  3. Qt——styleSheet

    1.两个地方调用 QWidget::setStyleSheet() QApplication::setStyleSheet() 2.基本语法 selector {attribute : value} ...

  4. JZ2440开发板:修改ARM芯片时钟(学习笔记)

    想要修改ARM芯片的时钟,需要去查询芯片手册和原理图,获取相关的信息(见下方图片) 首先来看时钟的结构图 根据结构图可以看出,时钟源有两种选择:1. XTIpll和XTOpll所连接的晶振 2. EX ...

  5. 使用JAX-WS(JWS)发布WebService(二)

    将项目改为maven工程,并发布到Tomcat: WebService常用到的注解以及作用: 发布过程中遇到的问题总结: 一.将项目改为maven工程,并发布到Tomcat: 继续上一篇,将代码完善成 ...

  6. 5 Ways to Prevent the 300ms Click Delay on Mobile Devices

    http://www.sitepoint.com/5-ways-prevent-300ms-click-delay-mobile-devices/

  7. Java 数字用二进制表示,以及原码,反码,补码、负数的二进制表示

    首先我们要对原码.反码和补码有个了解: 1.所谓原码就是二进制定点表示法,即最高位为符号位,"0"表示正,"1"表示负,其余位表示数值的大小. 2.反码表示法规 ...

  8. springboot jpa操作redis

    SpringBoot使用Redis缓存   (1)pom.xml引入jar包,如下: <dependency> <groupId>org.springframework.boo ...

  9. UpdateLog

    2014-10-20 增加数据适配器,使支持多数据库类型2015-01-08 增加没有主键ID的抽象类,使能自义主键字段实现MODEL 增加虚拟字段转换,将指定函数或语法转换为对象属性,灵活性更大了 ...

  10. 使用redux-actions优化actions管理

    redux-actions的api很少,有三个createAction(s)  handleASction(s)   combineActions 主要用到createAction去统一管理actio ...