about summary refs log tree commit diff
path: root/usth/MATH2.3/3/sum-set.cc
blob: 880fc6d0392e5d7060b7053c03e00eaf960bc001 (plain) (blame)
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
#include <iostream>
#include <vector>

using namespace std;

void
backtrack (vector<size_t>& big, vector<size_t>& smol, size_t& n, size_t index)
{
  size_t sum = 0;
  for (auto const& i : smol)
    sum += i;
  if (sum == n)
    {
      for (auto const& i : smol)
        cout << i << " ";
      cout << endl;
    }
  else
    for (size_t i = index; i < big.size (); ++i)
      if (sum + big[i] <= n)
        {
          smol.push_back (big[i]);
          backtrack (big, smol, n, i + 1);
          smol.pop_back ();
        }
}

int
main ()
{
  size_t n;
  size_t tmp;
  vector<size_t> s;

  cin >> n;
  for (size_t i = 0; i < n; ++i)
    {
      cin >> tmp;
      s.push_back (tmp);
    }

  vector<size_t> r;
  backtrack (s, r, n, 0);
}