POJ 1564--Sum It Up【DFS】

Sum It Up

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6623   Accepted: 3439

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
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m, flag;
int a[11000], b[11000];

int cmp(int a, int b){
	return a > b;
}

void dfs(int pos , int ans, int sum){
	if(sum == n){
		flag = 1;
		sort(b, b + ans, cmp);
		for(int i = 0; i < ans; ++i){
			if(!i)
				printf("%d",b[i]);
			else if(i)
				printf("+%d",b[i]);
		}
		printf("\n");
		return;
	}
	for(int i = pos; i < m; ++i){
		if(sum + a[i] <= n){
			b[ans] = a[i];
			dfs(i + 1, ans + 1, sum + a[i]);
			while(i + 1 < m && a[i] == a[i + 1])//去重
				++i;
		}
	}
}

int main (){
	while(scanf("%d%d", &n, &m), n || m){
		for(int i = 0; i < m; ++i)
			scanf("%d", &a[i]);
		flag = 0;
		sort(a, a + m, cmp);
		printf("Sums of %d:\n", n);
		dfs(0, 0, 0);
		if(!flag)
			printf("NONE\n");
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-01 21:29:54

POJ 1564--Sum It Up【DFS】的相关文章

poj 1564 Sum It Up【dfs+去重】

Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6682   Accepted: 3475 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

poj 3009 Curling 2.0 【DFS】

题意:从2出发,要到达3, 0可以通过,碰到1要停止,并且1处要变成0, 并且从起点开始沿着一个方向要一直前进,直至碰到1(或者3)处才能停止,(就是反射来反射去知道反射经过3).如果反射10次还不能到达3,就输出-1. 策略:深搜. 易错点,方向不容易掌握,并且,出题人把n, m顺序反了. 代码: #include<stdio.h> #include<string.h> int map[25][25]; int ans, n, m; const int dir[4][2] = {

HDU1258 Sum It Up 【DFS】+【判重】

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3944    Accepted Submission(s): 2026 Problem Description Given a specified total t and a list of n integers, find all distinct sums usi

poj 1564 Sum It Up (DFS+ 去重+排序)

http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i]和上一个数相同,那么记录数组的同一个位置就没有必要再放入这个数.例如:4 3 3 2构成和是7,b数组的第二个位置放了3,则后面的那个3就没有必要再放入记录数组的第二个位置了.(可能会放到后面的位置)... #include<stdio.h> #include<string.h> #i

POJ 1564 Sum It Up(DFS)

Sum It Up Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status 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

Poj 1426 Find The Multiple 【DFS】

Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19815 Accepted: 8048 Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only t

poj 1011 Sticks 【DFS】+【剪枝】

题意:有未知根(长度一样)木棒(小于等于n),被猪脚任意的截成n段,猪脚(脑抽了)想知道被截之前的最短长度(我推测猪脚得了健忘症). 这道题光理解题意就花了好久,大意就是任意根被截后的木棒拼到一起,能不能组成s(<=n)根的相同的木棒, 例:数据 9  5 1 2 5 1 2 5 1 2 可以组成最短为6 的(5+1, 2+2+2)3根木棒. 策略:深搜. 不过要是传统的深搜的话,TLE妥妥的.所以要剪枝(所谓剪枝,就是多加几个限制条件). 话不多说直接上代码. 代码1: #include <

poj 1088 滑雪 【记忆化搜索】+【DFS】

策略:如题 题目链接:http://poj.org/problem?id=1088 代码: #include<stdio.h> #include<string.h> int map[105][105], dp[105][105], n, m; const int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; //四个方向 int limit(int x, int y) //判断是不是越界了 { if(x<1||x>n||y<1||

POJ 1564 Sum It Up (DFS+剪枝)

 Sum It Up Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5820   Accepted: 2970 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

poj 1321 棋盘问题 【DFS】

题意:... 策略:深搜. 仔细分析我们发现,我们只需要对列进行标记,对于行我们考虑放棋子还是不放就行了. 代码: #include<stdio.h> #include<string.h> char s[10][10]; int n, m; int vis[10]; int ans; void dfs(int cur, int step) { if(step == m){ ans ++; return; } if(cur > n-1) return ; int i, j; f