zoj 1204 Additive equations

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

简单的DFS,咋一看,这不是做过吗,和poj1564 Sum it Up 差不多,然后就按以前的思路敲了起来,发现不对,这个要按照长度大小输出等式,起初的想法就是把所有的结果
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;

int N,M,maxx,d,k,r;
int visit[40];
int a[40],path[40];
bool flag;  //记录是否有解

bool Search(int x)  //二分检查sum是否在数组a里面
{
    int i,mid,l,r;
    l=0,r=M-1;
    while (l<=r)
    {
        mid = (l+r)>>1;
        if (x<a[mid])
            r=mid-1;
        else if (x>a[mid])
            l=mid+1;
        else
            return true;
    }
    return false;
}

void DFS(int sum,int len,int num,int pos)  //Len是等式左边的长度
{
    int i;
    if (sum>maxx)  //sum比最大值都大了就不用搜了
        return ;
    if (len==0&&Search(sum))
    {
        printf("%d",path[0]);
        for (i=1;i<num;i++)
            printf("+%d",path[i]);
        printf("=%d\n",sum);
        flag=true;
    }
    for (i=pos;i<M;i++)
    {
        if (!visit[i])
        {
            visit[i]=1;
            path[num]=a[i];
            DFS(sum+a[i],len-1,num+1,i);
            visit[i]=0;
        }
    }
}


int main()
{
    int i;
    scanf("%d",&N);
    while (N--)
    {
        memset(visit,0,sizeof(visit));
        memset(path,0,sizeof(path));
        memset(a,0,sizeof(a));
        scanf("%d",&M);
        for (i=0;i<M;i++)
            scanf("%d",&a[i]);
        sort(a,a+M);
        maxx=a[M-1];
        flag=false;
        for (i=2;i<M;i++)
            DFS(0,i,0,0);
        if (!flag)
            printf("Can't find any equations.\n");
        printf("\n");
    }
    return 0;
}

记录下来排序后输出,发现不好处理,又想到干脆按长度从2到M暴搜一遍,就过了。。。
代码:
posted @ 2014-07-29 16:59  浪子小黄人  阅读(234)  评论(0编辑  收藏  举报