Additive equations, additiveequations
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 norann maybe. so we need you to program the computer to solve this problem.
Input requirements
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 requirements
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.
Assume that
33 1 2 33 1 2 56 1 2 3 5 4 6
Output
1+2=3Can't find any equations.1+2=31+3=41+4=51+5=62+3=52+4=61+2+3=6
The question is very simple, that is, input a string of integers with a length of M, find the equations in this set of data and output them. The equation length ranges from short to long, and the equations increase left to right.
The idea is to first sort this set of data, and then from 2 -- (M-1) the length of the equation for Deep Search, Deep Search attention to pruning, otherwise it will time out, not found an equation is saved to the table, finally, sort the table.
#include<iostream>#include<algorithm>#include <vector>#include<string.h>#include<string>#include<ctype.h>#include<cmath>#include <queue>#define MAXN 30000using namespace std;int a[50],flag[50];int N;struct s{int a[50];int lenth;} str[MAXN];int k=0;void input(){k++;str[k].lenth=0;int t=1;for (int i=1; i<=N; i++)if (flag[i]!=0) str[k].a[t++]=a[i],str[k].lenth++;}void dfs(int i,int sum){ sum+=a[i];if (sum>a[N]) return ;for (int j=i+1; j<=N; j++)if (sum==a[j]){flag[j]=1;input();flag[j]=0;break;}for (int j=i+1; j<N; j++){flag[j]=1;dfs(j,sum);flag[j]=0;}}bool cmp(struct s a,struct s b){if(a.lenth!=b.lenth)return a.lenth<b.lenth;for(int i=1; i<=a.lenth; i++)if (a.a[i]!=b.a[i])return a.a[i]<b.a[i];}void solve();int main(){solve();return 0;}void solve(){ int T;cin>>T;while (T--){memset(flag,0,sizeof(flag));memset(a,0,sizeof(a));k=0;cin>>N;for (int i=1; i<=N; i++)cin>>a[i];sort(a+1,a+1+N);for (int i=1; i<N-1; i++){flag[i]=1;dfs(i,0);flag[i]=0;}sort(str+1,str+k+1,cmp);if (k==0)printf("Can't find any equations.\n\n");elsefor (int i=1; i<=k; i++){int j;printf("%d",str[i].a[1]);for (j=2; j<str[i].lenth; j++)printf("+%d",str[i].a[j]);printf("=%d\n",str[i].a[j]);}printf("\n");}}