zoj1204-addtive eqations(加法算式)
2011-01-27 14:55
344 查看
ZOJ Problem Set - 1204: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
Output for the Sample Input
Source: Zhejiang University Local Contest 2002, Preliminary
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
Source: Zhejiang University Local Contest 2002, Preliminary
/** problem name:zoj-1204:addtive equations * coder:mike * date:2011-1-27 * note:此题DFS+剪枝+一点HASH,这道题time limit=10s,开始一直10001ms,以为是多了1ms,后来才知道,OJ在 * 10s就停了 ==|||。剪枝之后AC了。 * result:760ms,164KB,AC */ #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_NUM 40 #define MAX_VALUE 1000 int source[MAX_NUM], /**< 输入的数字串 */ state[MAX_NUM], /**< 状态,也就是解 */ hash[MAX_VALUE], /**< hash table */ max[MAX_NUM], /**< 剪枝用的 */ maxdepth, /**< 深搜最大深度 */ flag, /**< 是否有解的标记;0-无解;1-有解 */ amount, /**< 数字数目 */ ncase, /**< 测试数据组数 */ cur_test; /**< 当前组数 */ int swap(int* m,int* n) { int tmp=*m; *m=*n; *n=tmp; return 0; } int bubble(int* base,int nelement) /**< bubble sort */ { int i,j; for(i=nelement-2;i>=0;i--) for(j=0;j<=i;j++) if(base[j]>base[j+1]) swap(base+j,base+j+1); return 0; } int print(int n) /**< output answer */ { int i; for(i=0;i<n;i++) { printf("%d",state[i]); if(i!=n-1) putchar('+'); } return 0; } int dfs(int depth,int sum,int start) /**< depth first search */ { if(depth>maxdepth) { if(hash[sum]==cur_test) /**< 判断和数是否存在 */ { print(depth-1); printf("=%d/n",sum); flag=1; /**< 修改标志位 */ } return 0; } if(sum>max[start]) /**< 剪枝:sum已经大于其后的所有数字 */ return 0; int i; for(i=start;i<amount;i++) { if(i>start&&source[i]==source[i-1]) /**< 避免重复,原因详见anagrams by stack题解 */ continue; state[depth-1]=source[i]; dfs(depth+1,sum+source[i],i+1); } return 0; } int main(void) { scanf("%d",&ncase); int j; for(cur_test=1;cur_test<=ncase;cur_test++) { flag=0; /**< flag要初始化,因为ZOJ不是多文件测试,是单文件多数据 */ /* memset(hash,0,sizeof(int)*MAX_VALUE);*/ /**< 这个似乎耗时,所以在cur_test上标记 */ scanf("%d",&amount); for(j=0;j<amount;j++) /**< get input */ { scanf("%d",source+j); hash[source[j]]=cur_test; } bubble(source,amount); /**< sort */ /* qsort(source,amount,sizeof(int),comp);*/ max[amount-1]=source[amount-1]; /**< generate array max */ for(j=amount-2;j>=0;j--) { if(max[j+1]<source[j]) max[j]=source[j]; else max[j]=max[j+1]; } for(j=2;j<amount;j++) /**< 按长度深搜 */ { maxdepth=j; dfs(1,0,0); } if(flag==0) /**< 无解 */ puts("Can't find any equations."); putchar('/n'); } return 0; }
相关文章推荐
- UVA_10183||POJ_2413||ZOJ_1962||HDU_1316(大数加法、检索)
- ZOJ 1204 Additive equations(深搜)
- ZOJ 1204 Additive equations
- (zoj1204)Additive equations
- ZOJ 1204 Additive equations
- ZOJ_1204_AdditiveEquations
- ZOJ - 1204 Additive equations(搜索)
- ZOJ 1204 Additive equations
- ZOJ 1205 Martian Addition (20进制加法)
- ZOJ-1204 Additive equations (DFS)
- ZOJ 1204 Additive equations
- ZOJ1204 Additive equations,DFS+set+仿函数+剪枝
- ZOJ 1204
- ZOJ 1204:(DFS)
- zoj 1204 Additive equations
- 1-9九个数字不重复组成一个三位数加法算式,求出所有组合
- ZOJ 1204 Additive equations
- 问题四十六: 在下面的加法算式中,不同的符号代表不同的数字,相同的符号代表相同的数字。
- ZOJ_1204_Additive equations
- zoj 1204 Additive equations