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. ruby中的extend 和 include

    include include是把module中定义的instance_method给mixin,然后当做类的实例方法使用(是因为module本身不能使用module的实例方法),给类进行实例化一个对 ...

  2. python学习之文件读写入门(文件读的几种方式比较)

    1.文件读写简单实例:(以w写的方式打开一个文件,以r读一个文件) # Author : xiajinqi # 文件读写的几种方式 # 文件读写 f = open("D://test.txt ...

  3. kali aquatone安装

    https://www.jianshu.com/p/418eedb9d9c8

  4. POJ-2155:Matrix(二维树状数祖)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31892   Accepted: 11594 Descript ...

  5. 如何写chrome扩展

    转载:http://www.cnblogs.com/pingfan1990/p/4560215.html 最近看到公司同事经常写chrome扩展,来提高生成效率,回想想自己以前也写过chrome扩展, ...

  6. 西安Uber优步司机奖励政策(8月10日到8月16日)

    1) 工作日(周一到周五)早高峰时间段(7点到9:30点).晚高峰时间段(5点到8点)车费 2.0 倍,每单奖励部分上限35元 例:在高峰时段中,假设行程基本车费为¥15,只要达到奖励前提,最后你将获 ...

  7. 模拟实现MyBatis中通过SQL反射实体类对象功能

    话不多说,直接上干货! package cn.test; import java.lang.reflect.Method; import java.sql.Connection; import jav ...

  8. 「日常训练」Watering Flowers(Codeforces Round #340 Div.2 C)

    题意与分析 (CodeForces 617C) 题意是这样的:一个花圃中有若干花和两个喷泉,你可以调节水的压力使得两个喷泉各自分别以\(r_1\)和\(r_2\)为最远距离向外喷水.你需要调整\(r_ ...

  9. XSS--PHPwind5.3复现

    xss再分析 短payload <svg/onload=alert(1)> <body/onfocus=alert``> <body/onfocus=confirm``& ...

  10. php常用几个数组的区别

    本文主要介绍的php数组函数主要有:sort.rsort.asort.arsort.ksort.krsort 测试数据定义一个关联数组如下: $data=[ 'f'=>123, 'b'=> ...