Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <iostream>
- #include <cstdlib>
- #include <vector>
- #include <algorithm>
- #define inf 0x3f3f3f3f
- using namespace std;
- typedef vector<int> vi;
- typedef vector< vi > vvi;
- vector<int> res;
- int n, t;
- int nums[15][2];
- //bool mark[10005];
- bool pode = false;
- vvi antigas;
- bool foo(int a, int b) { return a< b;}
- bool equalsVector(vi a, vi b){
- // printf("comparando\n");
- int s = a.size();
- if(s != b.size() ) return false;
- sort(a.begin(), a.end(), foo);
- sort(b.begin(), b.end(), foo);
- for(int i=0; i < s; i++){
- if(a[i] != b[i]) return false;
- }
- // printf("iguais\n");
- return true;
- }
- void pvector(){
- int s = res.size();
- for(int i=0; i < s; i++){
- printf("%d", res[i]);
- if(i!=s-1) printf("+");
- }
- printf("\n");
- }
- bool verifica(vi v){
- int s = antigas.size();
- for(int i =0; i < s; i++){
- if(equalsVector(antigas[i], v ) ) return false;
- }
- return true;
- }
- int bt(int cont){
- if(cont > t ) return 0;
- if( cont == t ) {
- if( verifica( res ) ) {
- // printf("imprimindo verificado\n");
- pvector();
- antigas.push_back(res);
- }
- pode=true;
- }
- int anterior = 0, numProx;
- for(int i=0; i < n; i++){
- numProx = nums[i][0];
- if(numProx == anterior || !(nums[i][1]) ) continue;
- nums[i][1]--;
- res.push_back(numProx);
- bt( cont + numProx);
- // res.pop(numProx);
- anterior = numProx;
- nums[i][1]++;
- res.pop_back();
- }
- return 0;
- }
- int main (int argc, char const* argv[]) {
- while(1){
- scanf("%d %d", &t, &n);
- res.clear();
- antigas.clear();
- pode=false;
- memset(nums, 0, sizeof nums);
- res.clear();
- if(!n) break;
- for(int i=0; i < n; i++){
- scanf("%d", &nums[i][0]);
- nums[i][1]++;
- }
- printf("Sums of %d:\n", t);
- bt(0);
- if(!pode) printf("NONE\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement