-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva00574.cpp
56 lines (49 loc) · 1.23 KB
/
uva00574.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
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <climits>
#include <cmath>
#include <set>
#include <bitset>
#include <sstream>
#include <iomanip>
#define _ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(15);
using namespace std;
set<string> ansSet;
vector<int> vi;
vector<int> candidate;
void recurse(int sum, int i){
if(sum == 0){
string str;
for(int i = 0; i < candidate.size()-1; ++i)
str = str + to_string(candidate[i]) + "+";
str += to_string(candidate.back());
if(!ansSet.count(str)){
cout << str << endl;
ansSet.insert(str);
}
return;
}
if(sum < 0) return;
if(i == vi.size()) return;
candidate.push_back(vi[i]);
recurse(sum - vi[i], i+1);
candidate.pop_back();
recurse(sum, i+1);
}
int main(){ _
int t, n;
while(cin >> t >> n){
if(t == 0) break;
cout << "Sums of " << t << ":" << endl;
ansSet.clear();
vi.assign(n, 0); candidate.clear();
for(int i = 0; i < n; ++i) cin >> vi[i];
recurse(t, 0);
if(ansSet.size() == 0) cout << "NONE" << endl;
}
return 0;
}