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
第一个数字表示输入数据的数量,之后每行第一个数表示有几个可供使用的数字,然后用这些数字组成加法等式,同一个数字不能重复使用,但是多次输入的数字可以重复使用。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[50],b[50],vis[1000010],flog,n;
void bfs(int pos,int num,int sum,int key)
{/*pos指向a数组,选择a中的数,num表示b数组中数字个数,sum表示当前和
key表示最多使用的数字个数*/
if(num>key)
return ;
if(sum>a[n-1])/*最大的和a数组的最后一个数*/
return ;
if(num==key&&vis[sum])
{
flog=0;/*当找到一种符合情况的时,flog赋值*/
int i;
for(i=0;i<key-1;i++)
printf("%d+",b[i]);
printf("%d=%d\n",b[key-1],sum);
return ;
}
if(pos>=n)
return ;
b[num]=a[pos];
bfs(pos+1,num+1,sum+a[pos],key);/*对于当前操作的数,有两种选择,
要或不要,要的话num+1,否则不加,之后这个数会被下一个数覆盖*/
bfs(pos+1,num,sum,key);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i;
scanf("%d",&n);
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(vis,0,sizeof(vis));
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
vis[a[i]]=1;/*vis数组标记*/
}
flog=1;
sort(a,a+n);
for(i=2;i<n;i++)
bfs(0,0,0,i);
if(flog)
printf("Can't find any equations.\n");
printf("\n");
}
return 0;
}

Additive equations--zoj的更多相关文章

  1. ZOJ1204——Additive equations(DFS)

    Additive equations Description We all understand that an integer set is a collection of distinct int ...

  2. zoj 1204 Additive equations

    ACCEPT acm作业 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=204 因为老师是在集合那里要我们做这道题.所以我很是天 ...

  3. ZOJ 1204 一个集合能组成多少个等式

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

  4. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  5. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  6. ZOJ ACM 1204 (JAVA)

    毕业好几年了,对算法还是比較有兴趣,所以想又一次開始做ACM题.俺做题比較任意,一般先挑通过率高的题来做. 第1204题,详细描写叙述请參考,ZOJ ACM 1204 1)难度分析 这个题目,基本的难 ...

  7. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  8. [LeetCode] Additive Number 加法数

    Additive number is a positive integer whose digits can form additive sequence. A valid additive sequ ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. JavaScript异步加载方案

    (1) defer,只支持IE defer属性的定义和用法(我摘自w3school网站) defer 属性规定是否对脚本执行进行延迟,直到页面加载为止. 有的 javascript 脚本 docume ...

  2. MSCRM4 在过滤后的LOOKUP框中实现查找

    在MSCRM中让Lookup根据一定的条件实现过滤功能, 这个需求很常见, 在我接触的诸多项目中似乎都需要有这个功能. 但非常遗憾是, MSCRM 的SDK并没有提供实现这个功能的方法. 不过我们应该 ...

  3. Percona Xtrabackup对数据库进行部分备份

    Xtrabackup也可以实现部分备份,即只备份某个或某些指定的数据库或某数据库中的某个或某些表.但要使用此功能,必须启用innodb_file_per_table选项,即每张表保存为一个独立的文件. ...

  4. (转)基于Metronic的Bootstrap开发框架经验总结(7)--数据的导入、导出及附件的查看处理

    http://www.cnblogs.com/wuhuacong/p/4777720.html 在很多系统模块里面,我们可能都需要进行一定的数据交换处理,也就是数据的导入或者导出操作,这样的批量处理能 ...

  5. PAT_A1143#Lowest Common Ancestor

    Source: PAT A1143 Lowest Common Ancestor (30 分) Description: The lowest common ancestor (LCA) of two ...

  6. S-HR系统流程

  7. C#第二节课

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  8. css 陌生属性

    记录一些我之前没见过属性 1.width:100vh  100vh 2.min-height:calc(100vh + 51px);cale 3.:nth-child nth-child   和 :n ...

  9. 拷贝构造和拷贝赋值、静态成员(static)、成员指针、操作符重载(day06)

    十七 拷贝构造和拷贝赋值 浅拷贝和深拷贝 )如果一个类中包含指针形式的成员变量,缺省的拷贝构造函数只是复制了指针变量的本身,而没有复制指针所指向的内容,这种拷贝方式称为浅拷贝. )浅拷贝将导致不同对象 ...

  10. HDU 5729 Rigid Frameworks (联通块计数问题)

    题目传送门 通过看题解画图可以发现: 不论怎么转,一列里的横边/一行里的竖边始终平行 当我们加固一个格子时,会让它所在的这一行的竖边和这一列的横边保证垂直 而我们的目标是求所有竖边和横边都保证垂直的方 ...