Additive equations Time limit: Seconds Memory Limit: 32768 KB
We all understand this an integer set is a collection of distinct integers. Now the question Is:given a integer set, can you find all its addtive equations? To explainAdditive EquationIs, let's look at the following examples:
1+2=3 is a additive equation of the set {}, since all the numbers that was summed up in the left-hand-side of the EQ Uation, 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 would always output the numbers on the left-hand-side of the equation in Ascending order. Therefore in this example, it's claimed that the set {} have an unique additive equation 1+2=3.
It is not a guaranteed that any integer set with its-only additive equation. For example, the set {1,2,5} have no addtive equation and the set {1,2,3,5,6} have 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'll eventually become impossible to find all the additive equations F Rom the top of our minds – unless you is John von Neumann maybe. So we need a computer to solve this problem.
Input
The input data consists of several test cases.
The first line of the input would contain an integer N and which is the number of the test cases.
Each test case would first contain an integer M (1<=m<=30), which was the number of integers in the set, and then is F Ollowed by M distinct positive integers on the same line.
Output
For each test case, you is supposed to output all the additive equations of the set. These equations would be sorted according to their lengths first (i.e, the number of the integer being summed), and then the EQ Uations with the same length would be sorted according to the numbers from left to right, just like the sample output shows . When there was no such equation, simply output "Can ' t find any equations." 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
AC Code
#include <stdio.h> #include <string.h> #include <stdlib.h>int a[50],n,b[50],flag,vis[1000010];int CMP (const void *a,const void *b) {return * (int *) a-* (int *) b;} void Dfs (int pos,int num,int sum,int key) {if (Num>key) return;if (sum>a[n-1]) return;if (num==key&&vis[sum ] {flag=1;int i;printf ("%d", b[0]), for (i=1;i<num;i++) {printf ("+%d", B[i]);} printf ("=%d\n", sum); return;} if (pos>=n) Return;b[num]=a[pos];d FS (Pos+1,num+1,sum+a[pos],key);d FS (Pos+1,num,sum,key);} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d", &n), Int. i;memset (vis,0,sizeof (VIS)); for (i=0;i<n; i++) {scanf ("%d", &a[i]); vis[a[i]]=1;} Flag=0;qsort (A,n,sizeof (a[0]), CMP), for (i=2;i<n;i++) DFS (0,0,0,i), if (!flag) {printf ("Can ' t find any equations.\n" );} printf ("\ n");}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ZOJ topic 1024x768 Additive equations (DFS)