ZOJ 1204 Additive equations

来源:互联网 发布:淘宝助理怎样下载宝贝 编辑:程序博客网 时间:2024/06/02 05:32
A - Additive equations
Time Limit:10000MS    Memory Limit:32768KB    64bit IO Format:%lld & %llu
SubmitStatusPracticeZOJ 1204

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 anadditive 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
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

 




DFS搜索题。

现在看来算是比较简单的DFS了。

题目的大概意思是,找出一组集合里元素能够组成的所有等式。

等式左边的元素可以是2到n-1个数,右边只可能有一个数。

循环左边的个数l(2-n-1)

那么递归出口便是当前的搜索到的元素总数等于了我这次的l,这个时候只要在集合里找到一个元素的值等于sum即可。

其余的情况。便是在搜索其他的元素数。在搜索之前排个序,如果当前的数字元素总和加上这一次的元素值小于了最大的元素数a[n-1],则意味着可以继续搜索,此时递归。

递归结束后记得回溯。

输出等式的时候也有些技巧,比如设一个标识来记录此时是否该输出"+"。设置一个标识判断是否有等式存在。

最后记得每组样例输出一个空行。


#include <stdio.h>#include <string.h>#include <algorithm>#define N 35using namespace std;bool issolve=false;int a[N],l,n,vis[N];void printEquation(int sum){        bool isplus=false;        for(int i=0;i<n;i++)                if(vis[i])                {                        if(isplus)                                printf("+");                        else                               isplus=true;                        printf("%d",a[i]);                }        printf("=%d\n",sum);}void dfs(int index,int sum,int num){        if(num==l)        {                for(int i=index;i<n;i++)                {                        if(a[i]>sum)                                return;                        if(a[i]==sum)                        {                                issolve=true;                                printEquation(sum);                        }                }                return;        }        if(index>=n)                return;        if(a[index]+sum<=a[n-1])        {                vis[index]=1;                dfs(index+1,sum+a[index],num+1);                vis[index]=0;                dfs(index+1,sum,num);        }}int main(){        int t;        scanf("%d",&t);        while(t--)        {                issolve=false;                memset(vis,0,sizeof(vis));                scanf("%d",&n);                for(int i=0;i<n;i++)                        scanf("%d",&a[i]);                sort(a,a+n);                for(int i=2;i<n;i++)                {                        l=i;                        dfs(0,0,0);                }                if(!issolve)                        printf("Can't find any equations.\n");                printf("\n");        }        return 0;}


0 0
原创粉丝点击