(搜索DFS,剪枝)Additive equations

Additive equations

Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 11   Accepted Submission(s) : 6
Problem Description
    We all understand that an integer set is a collection of distinct integers. Now the question is: given an integer set, can you find all its addtive equations? To explain what an  additive equation is, let's look at the following examples: 
    1+2=3 is an additive equation of the set {1,2,3}, since all the numbers that are summed up in the left-hand-side of the equation, namely 1 and 2, belong to the same set as their sum 3 does. We consider 1+2=3 and 2+1=3 the same equation, and will always output the numbers on the left-hand-side of the equation in ascending order. Therefore in this example, it is claimed that the set {1,2,3} has an unique additive equation 1+2=3.
    It is not guaranteed that any integer set has its only additive equation. For example, the set {1,2,5} has no addtive equation and the set {1,2,3,5,6} has more than one additive equations such as 1+2=3, 1+2+3=6, etc. When the number of integers in a set gets large, it will eventually become impossible to find all the additive equations from the top of our minds -- unless you are John von Neumann maybe. So we need you to program the computer to solve this problem.

Input

The input data consists of several test cases. 
The first line of the input will contain an integer N, which is the number of test cases. 
Each test case will first contain an integer M (1<=M<=30), which is the number of integers in the set, and then is followed by M distinct positive integers in the same line.

Output

For each test case, you are supposed to output all the additive equations of the set. These equations will be sorted according to their lengths first( i.e, the number of integer being summed), and then the equations with the same length will be sorted according to the numbers from left to right, just like the sample output shows. When there is no such equation, simply output "Can't find any equations." in a line. Print a blank line after each test case.

Sample Input
3
3 1 2 3
3 1 2 5
6 1 2 3 5 4 6

Output for the Sample Input

1+2=3

Can't find any equations.

1+2=3
1+3=4
1+4=5
1+5=6
2+3=5
2+4=6
1+2+3=6

 

 
#include<iostream>
#include<map>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 35
set<int> s;
int a[maxn], N;
int mark[maxn];
int flag;

void print(int num, int sum)
{
	int first = 1, i, cnt = 0;
	for (i = 1; i <= N - 1 && cnt <= num; i++)
		if (mark[i])
		{
		if (first)
		{
			printf("%d", a[i]);
			first = 0;
		}

		else
			printf("+%d", a[i]);
		cnt++;
		}
	printf("=%d\n", sum);
}

void DFS(int num, int cur, int sum, int p)
{
	if (cur == num)
	{
		if (s.find(sum) != s.end())	//使用set加快检索
		{
			print(num, sum);
			flag = 1;
		}
	}

	//if (cur < num)
	else
	{
		for (int i = p; i <= N - 1; i++)	//枚举起始位置
			if (a[i] + sum <= a[N])		//!!注意这个剪枝。算法对了却超时,这时就需要注意剪枝。此题剪枝后10s时限只需跑150ms。
		{
			sum += a[i];
			mark[i] = 1;
			DFS(num, cur + 1, sum, i + 1);
			sum -= a[i];
			mark[i] = 0;
		}
	}

}

void solve()
{
	for (int i = 2; i <= N - 1; i++)//枚举DFS的层数
	{
		memset(mark, 0, sizeof(mark));
		DFS(i, 0, 0, 1);
	}

}

int main()
{
	//freopen("f:\\input.txt", "r", stdin);
	int T;
	int i, j;
	int tmp;
	scanf("%d", &T);
	while (T--)
	{
		s.clear();
		scanf("%d", &N);
		for (i = 1; i <= N; i++)
		{
			scanf("%d", &a[i]);
			s.insert(a[i]);
		}
		sort(a + 1, a + 1 + N);
		flag = 0;
		solve();
		if (!flag)
			printf("Can't find any equations.\n");
		printf("\n");
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值