zcmu1133(dfs+判重)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yu121380/article/details/82347105

1133: 第九章:致我们终将逝去的青春

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 28  Solved: 18
[Submit][Status][Web Board]

Description

                      

/青 春 是 用 来 追 忆 的

/当 你 怀 揣 着 她 时

/她 一 文 不 值

/只 有 将 她 耗 尽 后

/再 回 过 头 看

/一 切 才 有 了 意 义

/爱 过 我 们 的 人 和 伤 害 过 我 们 的 人

/都 是 我 们 青 春 存 在 的 意 义

         

在ACM中找寻 青春的意义:

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 x1, ..., xn. 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 x1, ..., xn 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

题意:给你一个数,求在给出的m个数中选几个加起来的和可以等于n,输出这几种等式,注意等式不能一样。

解析:dfs+判重,直接的dfs我们可能出现有重复的答案,比如400的例子,直接dfs有两个一样的答案,所以我们直接跳过50,跳到25。

#include<bits/stdc++.h>
using namespace std;
 
#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}

const int maxn=1000+5;
int n,m,a[maxn],ans[maxn],flag;
void dfs(int deep,int sum,int length)
{
	if(sum>n)return ;
	if(sum==n)
	{
		flag=1;
		for(int i=0; i<length; i++)
		{
			if(i==0)printf("%d",ans[i]);
			else printf("+%d",ans[i]);
		}
		puts("");
	}
	for(int i=deep; i<m; i++)
	{
		ans[length]=a[i];
		dfs(i+1,sum+a[i],length+1);
		while(a[i]==a[i+1])i++;
	}
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		if(n==0&&m==0)break;
		for(int i=0; i<m; i++)scanf("%d",&a[i]);
		flag=0;
		printf("Sums of %d:\n",n);
		dfs(0,0,0);
		if(!flag)puts("NONE");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/yu121380/article/details/82347105
今日推荐