-
Notifications
You must be signed in to change notification settings - Fork 4
/
UVA00574.cpp
53 lines (49 loc) · 1.05 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
#include <cstdio>
using namespace std;
int n, t, n2;
int nums[12][2];
int sol[12], once;
void proc(int pos, int sum, int elems) {
if (sum == t) {
once = true;
bool first = false;
for (int i = 0; i < pos; i++) {
for (int j = 0; j < sol[i]; j++) {
if (first) printf("+");
else first = true;
printf("%d", nums[i][0]);
}
}
printf("\n");
}
if (sum < t && pos < n && sum + (n2-elems)*nums[pos][0] >= t) {
sol[pos] = nums[pos][1];
for (int i = 0; sol[pos] >= 0; sol[pos]--, i++) {
proc(pos+1, sum + (sol[pos]*nums[pos][0]), elems+i);
}
sol[pos] = 0;
}
}
int main() {
for (int i = 0; i < 12; i++) sol[i] = 0;
while (scanf("%d %d", &t, &n) && !(t == 0 && n == 0)) {
n2 = n;
once = false;
printf("Sums of %d:\n", t);
int prev = -1, cont = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &nums[i-cont][0]);
if (nums[i-cont][0] == prev) {
cont ++;
nums[i-cont][1]++;
} else {
prev = nums[i-cont][0];
nums[i-cont][1] = 1;
}
}
n -= cont;
proc(0, 0, 0);
if (!once) printf("NONE\n");
}
return 0;
}