Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int st[100], n, L;
- int bara[100];
- int este = 0, s=0;
- int surjectiva(int k)
- {
- bool ok;
- for(int i = 1; i <= n; i++)
- {
- ok = false;
- for(int j = 1; j <= k; j++)
- if(bara[i] == bara[st[j]])
- ok = true;
- if(!ok)
- return 0;
- }
- return 1;
- }
- void citire()
- {
- ifstream f("bara.in");
- f >> L >> n;
- for(int i = 1; i <= n; i++)
- f >> bara[i];
- f.close();
- }
- void init(int k)
- {
- if(k == 1)
- st[k] = 0;
- else
- st[k] = st[k-1] - 1;
- }
- int succesor(int k)
- {
- if(st[k] < n)
- {
- st[k]++;
- return 1;
- }
- else
- {
- s -= bara[st[k-1]];
- return 0;
- }
- }
- int valid(int k)
- {
- if(s + bara[st[k]] <= L)
- {
- s += bara[st[k]];
- return 1;
- }
- return 0;
- }
- int solutie(int k)
- {
- return s == L;
- }
- void tipar(int k)
- {
- este = 1;
- for(int i = 1; i <= k; i++)
- cout << bara[st[i]] << " ";
- cout << "\n";
- s -= bara[st[k]];
- }
- void bkt(int k)
- {
- init(k);
- while(succesor(k))
- {
- if(valid(k))
- {
- if(solutie(k) && surjectiva(k))
- {
- tipar(k);
- }
- else
- {
- bkt(k+1);
- }
- }
- }
- }
- int main()
- {
- citire();
- bkt(1);
- if(!este)
- cout << "Imposibil";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement