Advertisement
Guest User

sumsitupTLE

a guest
Sep 18th, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <vector>
  7. #include <algorithm>  
  8.  
  9. #define inf 0x3f3f3f3f
  10. using namespace std;
  11.  
  12. typedef vector<int> vi;
  13.  
  14. typedef vector< vi > vvi;
  15.  
  16. vector<int> res;
  17. int n, t;
  18. int nums[15][2];
  19. //bool mark[10005];
  20.  
  21.  
  22.  
  23. bool pode = false;
  24.  
  25. vvi antigas;
  26.  
  27. bool foo(int a, int b) { return a< b;}
  28.  
  29. bool equalsVector(vi a, vi b){
  30.     //    printf("comparando\n");
  31.         int s = a.size();
  32.         if(s != b.size() ) return false;
  33.         sort(a.begin(), a.end(), foo);
  34.         sort(b.begin(), b.end(), foo);        
  35.  
  36.         for(int i=0; i < s; i++){
  37.                 if(a[i] != b[i]) return false;
  38.         }
  39.        // printf("iguais\n");
  40.         return true;
  41. }
  42.  
  43.  
  44.  
  45. void pvector(){
  46.         int s = res.size();
  47.         for(int i=0; i < s; i++){
  48.                 printf("%d", res[i]);
  49.                 if(i!=s-1) printf("+");
  50.         }
  51.         printf("\n");
  52. }
  53.  
  54. bool verifica(vi v){
  55.         int s = antigas.size();
  56.         for(int i =0; i < s; i++){
  57.                 if(equalsVector(antigas[i], v ) ) return false;
  58.         }
  59.         return true;
  60. }
  61.  
  62.  
  63. int bt(int cont){
  64.         if(cont > t ) return 0;
  65.         if( cont == t ) {
  66.                 if( verifica( res ) ) {
  67. //                        printf("imprimindo verificado\n");
  68.                         pvector();  
  69.                         antigas.push_back(res);    
  70.                 }
  71.           pode=true;
  72.         }
  73.         int anterior = 0, numProx;
  74.         for(int i=0; i < n; i++){
  75.                 numProx = nums[i][0];
  76.                 if(numProx == anterior || !(nums[i][1]) ) continue;
  77.                 nums[i][1]--;
  78.                 res.push_back(numProx);
  79.                 bt( cont + numProx);
  80. //                res.pop(numProx);
  81.                 anterior = numProx;
  82.                 nums[i][1]++;
  83.                 res.pop_back();
  84.  
  85.         }
  86.         return 0;
  87. }
  88.  
  89.  
  90.  
  91. int main (int argc, char const* argv[]) {
  92.  
  93.  
  94.    
  95.         while(1){
  96.                 scanf("%d %d", &t, &n);
  97.                 res.clear();
  98.                 antigas.clear();
  99.                 pode=false;
  100.                 memset(nums, 0, sizeof nums);
  101.  
  102.                 res.clear();
  103.                
  104.                 if(!n) break;
  105.                 for(int i=0; i < n; i++){
  106.                         scanf("%d", &nums[i][0]);
  107.                         nums[i][1]++;
  108.                 }
  109.                 printf("Sums of %d:\n", t);
  110.                 bt(0);        
  111.                 if(!pode) printf("NONE\n");
  112.         }
  113.  
  114.        
  115.         return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement