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


题意:给出n个数,从中选出一些数组成等式。输出等式时,先按照等式的长度排序,长度相同时,等式里的元素按照从小到大的顺序排序。如果找不出等式,输出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;
}
时间: 2024-07-31 12:34:06

ZOJ 1204 Additive equations(深搜)的相关文章

深搜———ZOJ 1004:anagrams by stack

细节问题各种虐!! 其实就是简单的一个深搜 看成二叉树来理解:每个节点有两个枝:入栈和出栈. 剪枝操作:只有当栈顶元素和当前位置的目标字符相同时才出栈,否则就不出栈 dfs写三个参数:depth搜索深度,npush压栈数,npop出栈数 npush用于记录压栈数:主要判断当前压栈是否合理,以及要压入的元素在原串种的位置 npop用于记录出栈数:判断生成的目标串元素的位置 当npush==npop==目标串时,说明生成了一个可执行的操作串 注意输出操作串的时候一定要用depth参数来控制,因为在多

ZOJ 题目1024 Additive equations(DFS)

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

HDU 2266 How Many Equations Can You Find(模拟,深搜)

题目 这是传说中的深搜吗....不确定,,,,貌似更加像是模拟,,,, //我要做深搜题目拉 //实际上还是模拟 #include<iostream> #include<string> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; int ans; char a[20]; __int64 n;

ZOJ Seeding 2100【简单深搜】

Seeding Time Limit: 2 Seconds      Memory Limit: 65536 KB It is spring time and farmers have to plant seeds in the field. Tom has a nice field, which is a rectangle with n * m squares. There are big stones in some of the squares. Tom has a seeding-ma

Additive equations

题目描述 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 i

ZOJ1204——Additive equations(DFS)

Additive equations 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 f

hdu1455 Sticks 深搜 强剪枝

Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6035    Accepted Submission(s): 1704 Problem Description George took sticks of the same length and cut them randomly until all parts becam

深搜笔记

看搜索已经很久了,对于搜索的思想从原来的死记硬背到现在终于懂了一点其实也蛮不错吧,我自己先总结出来了几条关于在图里面深搜的几条方法,仅供参考: 首先:我们得知道深搜是什么,其次于广搜的区别是什么.然后又哪些模板 举一个深搜例子:red and black:这是初学者最常见到的题.对于这题我们所要考虑的就是一个'.'的个数,这个题先要找到@的位置,这个好办,直接: for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { if(map[i][j]=='@') }

HDU1342 Lotto 【深搜】

应用 渗透问题 游戏中会用到. 动态连接 最近共同祖先 等价有限状态机 物理学Hoshen-Kopelman算法:就是对网格中的像素进行分块 Hinley-Milner多态类型推断 Kruskai最小生成树 Fortran等价语句编译 形态学开闭属性 Matlab中关于图像处理的bwlabel函数 渗透问题 一个N×N的矩阵,判断顶部和底部是否连通就是渗透问题. 下图中左侧的矩阵能渗透,右侧矩阵不能渗透. 渗透问题在电学.流体力学.社会交际中都有应用. 在游戏中可能需要生成一张地图,但是作为地图