Additive equations(ZOJ 1204) (dfs应用)

本文探讨了一种算法,该算法能够找出一个整数集合的所有可能的加法方程。通过对输入数据进行深度优先搜索(DFS),程序可以找出所有可能的组合,并按长度和数值顺序对结果进行排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Additive equations

Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)

Total Submission(s) : 26   Accepted Submission(s) : 3

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

Source

Zhejiang University Local Contest 2002, Preliminary

我写的DFS开始只能跑到27就栈溢出了,提交也是超时,后来想着把27-30的数特别处理,复杂度分析错了,导致浪费了好多时间……然后莫名其妙地DFS能跑到30了,而且很快,提交TLE,再调试,发现还可以剪枝,再提交,WA,原来是打错了字符……

#include<bits/stdc++.h>
using namespace std;
int a[35],vis[100005],num[35],m,pp,v[100005];
struct node
{
    int aa[35];
    int len;
    int ans;
} t[100005];
void dfs(int pos,int ans)
{
    if(pos>m||ans>a[m])
        return;
    if(v[ans]&&pos!=2)
    {
        for(int i=1; i<pos; i++)
        {
            t[pp].aa[i-1]=num[i];
        }
        t[pp].len=pos-1;
        t[pp].ans=ans;
        sort(t[pp].aa,t[pp].aa+t[pp].len);
        pp++;
    }
    for(int i=1; i<=m; i++)
    {
        if(!vis[a[i]]&&a[i]>num[pos-1])
        {
            vis[a[i]]=1;
            num[pos]=a[i];
            dfs(pos+1,ans+a[i]);
            vis[a[i]]=0;
        }
    }
}
bool cmp(node x,node y)
{
    if(x.len!=y.len)
        return x.len<y.len;
    else
    {
        int j=x.len;
        for(int i=0; i<j; i++)
            if(x.aa[i]!=y.aa[i])
                return x.aa[i]<y.aa[i];
    }
}
void print()
{
    for(int i=0; i<pp; i++)
    {
        if(i==0)
        {
            printf("%d",t[i].aa[0]);
            for(int j=1; j<t[i].len; j++)
                printf("+%d",t[i].aa[j]);
            printf("=%d\n",t[i].ans);
            continue;
        }
        if(t[i].len==t[i-1].len)
        {
            int flag=1;
            for(int j=0; j<t[i].len; j++)
                if(t[i].aa[j]!=t[i-1].aa[j])
                {
                    flag=0;
                    break;
                }
            if(!flag)
            {
                printf("%d",t[i].aa[0]);
                for(int j=1; j<t[i].len; j++)
                    printf("+%d",t[i].aa[j]);
                printf("=%d\n",t[i].ans);
            }
        }
        else
        {
            printf("%d",t[i].aa[0]);
            for(int j=1; j<t[i].len; j++)
                printf("+%d",t[i].aa[j]);
            printf("=%d\n",t[i].ans);
        }
    }
    printf("\n");
}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&m);
        memset(v,0,sizeof(v));
        for(int i=1; i<=m; i++)
            scanf("%d",&a[i]),v[a[i]]++;
        sort(a+1,a+m+1);
        pp=0;
        for(int i=1; i<m-1; i++)
        {
            num[1]=a[i];
            for(int j=i+1; j<=m; j++)
                vis[a[j]]=0;
            vis[a[i]]=1;
            dfs(2,num[1]);
        }
        if(!pp)
        {
            printf("Can't find any equations.\n\n");
            continue;
        }
        sort(t,t+pp,cmp);
        print();
    }
}/*
100
3 1 2 3
3 1 2 5
6 1 2 3 5 4 6
20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
26 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
28 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
29 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值