ZOJ 1204 Additive equations
2015-07-28 19:17
302 查看
A - Additive equations
Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit Status Practice ZOJ
1204
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
Output for the Sample Input
Time Limit:10000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit Status Practice ZOJ
1204
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
#include <iostream> #include<algorithm> using namespace std; int m,a[40],b[40],h; void dfs(int d,int k,int s,int g){ int i,j,f; if (d>g){ f=0; for (j=k+1;j<=m;j++) if (a[j]==s) {f=1;break;} if (f) { h=0; cout<<b[1]; for (j=2;j<d;j++) cout<<'+'<<b[j]; cout<<'='<<s<<endl; } } else { for (i=k+1;i<=m;i++){ b[d]=a[i]; if (s+b[d]>a[m]) break;//根据递增性剪枝 dfs(d+1,i,s+b[d],g); } } } int main() { int t,i,j; cin>>t; while (t--){ cin>>m; for (i=1;i<=m;i++) cin>>a[i];//dfs常用剪枝技巧,排序 sort(a+1,a+m+1); h=1; for (j=2;j<=m-1;j++){//<span style="color: rgb(34, 34, 34); font-family: 'times new roman'; font-size: 17px; background-color: rgb(234, 235, 255);">由于要按等式长度递增顺序输出所有方案,枚举限制长度DFS</span> for (i=1;i<=m;i++) { b[1]=a[i]; dfs(2,i,b[1],j); } } if (h) cout<<"Can't find any equations."<<endl; cout<<endl; } return 0; }
相关文章推荐
- Error building Player: CommandInvokationFailure: Unable to convert classes into dex format. See the
- 【effective c++读书笔记】【第0章】导读
- 【Android测试】【第五节】LogCat——命令行
- 【effective c++读书笔记】【第0章】导读
- 正则表达式、JAVA线程
- HDU 5323 Solve this interesting problem
- HDU 5317 RGCDQ
- leetcode 53: Spiral Matrix
- CSS截取字符串,额外的文本显示以省略号
- hdu 5326 dfs
- Codeforces Gym100571A Cursed Query
- LeetCode题解——Swap Nodes in Pairs
- 项目笔记用到的链接
- java培训 2015-07-28
- 杭电1087
- 数据结构和算法学习笔记-1
- [BZOJ1622] [Usaco2008 Open]Word Power 名字的能量
- 蚂蚁的难题(三)
- Invert Binary Tree
- POJ 1573 Robot Motion