-
Notifications
You must be signed in to change notification settings - Fork 19
/
DSA02010 - TỔ HỢP SỐ CÓ TỔNG BẰNG X
87 lines (87 loc) · 2.74 KB
/
DSA02010 - TỔ HỢP SỐ CÓ TỔNG BẰNG X
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
struct data
{
vector<int> b;
};
int n, k, a[25], c[105];
vector<struct data> d;
void Try(int x, int i, int s)
{
for (int ii = i; ii <= n; ii++)
{
s += a[ii];
c[x] = a[ii];
if (s == k)
{
struct data e;
for (int jj = 1; jj <= x; jj++)
e.b.push_back(c[jj]);
d.push_back(e);
}
else if (s < k)
Try(x + 1, ii, s);
s -= a[ii];
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
d.clear();
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
Try(1, 1, 0);
if (d.size() == 0)
{
cout << "-1" << endl;
continue;
}
for (int i = 0; i < d.size(); i++)
{
cout << "[" << d[i].b[0];
for (int j = 1; j < d[i].b.size(); j++)
{
if (d[i].b[j] == 0)
break;
cout << " " << d[i].b[j];
}
cout << "]";
}
cout << endl;
}
}
/* Do Xuan Huong
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@##################@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@#############################@@@@@@@@@@@@@@@@
@@@@@@@@@@@@&####################################@@@@@@@@@@@@
@@@@@@@@@@##########################################@@@@@@@@@
@@@@@@@@##############################################@@@@@@@
@@@@@@#################################################@@@@@@
@@@@@####################################################@@@@
@@@%#####################@@@@@@@@@@@######################@@@
@@@###################@@@@@@@@@@@@@@@@@####################@@
@@##################@@@@@@ @@@@@@##################@@
@@#################@@@@@ @@@@###################@
@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@
@ &@@@@ @@@@ .......@@
@@ @@@@@@ @@@@@@ .......@@
@@ @@@@@@@@@@@@@@@@@ .......@@@
@@@ @@@@@@@@@@@ ......@@@@
@@@@ ......@@@@@
@@@@@@ ......@@@@@@
@@@@@@@ .....@@@@@@@@
@@@@@@@@@ .....@@@@@@@@@@
@@@@@@@@@@@@ ....@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@ ....@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@% .@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*/