-
Notifications
You must be signed in to change notification settings - Fork 0
/
574 - Sum It Up.cpp
67 lines (62 loc) · 1.13 KB
/
574 - Sum It Up.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <stack>
#include <map>
#define ll long long
using namespace std;
// vector ip trong ham main
vector<int> ip;
// value N
int N;
// vector trong ham backtrack
vector<int> solution;
// map de tranh trung nhau
map< vector<int>,bool > m;
//
vector<int> tmp;
void backtrack(int left, int tong){
if(left==ip.size() || tong >=N ){
if(tong==N && m[solution]==0){
bool dem=0;
for(int j=0;j<solution.size();j++){
if(dem) cout << "+";
cout << solution[j];
dem=1;
}
cout << endl;
m[solution]=1;
}
return;
}
for(int i=left;i<ip.size();i++){
solution.push_back(ip[i]);
backtrack(1+i,tong+ip[i]);
// xoa tai cho
solution.pop_back();
}
}
int main(){
// freopen("ip.txt","r",stdin);
// freopen("rs.txt","w",stdout);
while(cin >> N){
if(!N) break;
int n; cin >> n;
int a;
for(int i=0;i<n;i++){
cin >> a;
ip.push_back(a);
}
//
cout << "Sums of "<<N <<":"<<endl;
backtrack(0,0);
// in output
if(m.size()==0) cout << "NONE"<<endl;
//reset
ip.clear();
m.clear();
}
}