Additive Equations Time Limit: 10 seconds memory limit: 32768 KB
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 whatAdditive EquationIs, 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
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
33 1 2 33 1 2 56 1 2 3 5 4 6
Output for the sample input
1+2=3Can‘t find any equations.1+2=31+3=41+4=51+5=62+3=52+4=61+2+3=6
Question: give n numbers and select some numbers to form an equation. When outputting an equation, the elements in the equation are sorted in ascending order according to the length of the equation. If the equation cannot be found, the output can't find any equations.
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;vector <vector<int> > ans;int cur, flag;const int N = 35;int a[N], x[N];void dfs(int i, int len, int sum) { if(i == len) { if(cur == sum) { flag = 1; vector <int> v; int s = 0; for(int j = 0; j < len; j++) if(x[j]) { v.push_back(x[j]); s += x[j]; } v.push_back(s); ans.push_back(v); } } else { if(cur + a[i] <= sum) { x[i] = a[i]; cur += a[i]; dfs(i+1, len, sum); x[i] = 0; cur -= a[i]; } dfs(i+1, len, sum); }}bool comp(const vector<int> &a, const vector<int> &b) { if(a.size() != b.size()) return a.size() < b.size(); for(int i = 0; i < a.size(); i++) { if(a[i] < b[i]) return true; else if(a[i] > b[i]) return false; }}int main() { int T, n; scanf("%d",&T); while(T--) { scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", &a[i]); ans.clear(); sort(a, a+n); flag = 0; for(int i = 2; i < n; i++) dfs(0, i, a[i]); if(flag) { sort(ans.begin(), ans.end(), comp); for(int i = 0; i < ans.size(); i++) { for(int j = 0; j < ans[i].size() - 1; j++) { if(j == 0) printf("%d", ans[i][j]); else printf("+%d",ans[i][j]); } printf("=%d\n", ans[i][ans[i].size()-1]); } } else printf("Can't find any equations.\n"); printf("\n"); } return 0;}
Zoj 1204 additive equations (Deep Search)