iscsi

Spoj Subset Sums

Aug 12th, 2011
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <cstdio>
  10. #include <cstdlib>
  11. #include <cctype>
  12. #include <cmath>
  13. #include <algorithm>
  14. #include <iostream>
  15. #include <sstream>
  16. #include <string>
  17. #include <vector>
  18. #include <queue>
  19. #include <set>
  20. #include <map>
  21. #include <cstdio>
  22. #include <cstdlib>
  23. #include <cctype>
  24. #include <cmath>
  25. #include <ctime>
  26.  
  27. using namespace std;
  28. typedef long long LL;
  29. #define FOR(k,a,b) for(int k(a); k < (b); ++k)
  30. #define REP(k,a) for(int k=0; k < (a); ++k)
  31.  
  32. int main()
  33. {
  34.     int N,A,B;
  35.     cin >> N >> A >> B;
  36.     vector<LL> S(N);
  37.     LL res=0;
  38.     REP(i,N)
  39.     {
  40.         cin >> S[i] ;
  41.     }
  42.     vector<LL> v;
  43.     int vi=N/2,wi=N-vi;
  44.     REP(i,1<<vi)
  45.     {
  46.         v.push_back(0);
  47.         REP(j,vi)
  48.         {
  49.             if((1<<j) &i )
  50.                 v.back()+=S[j];
  51.         }
  52.         if(v.back()>=A && v.back()<=B)
  53.             ++res;
  54.     }
  55.     sort(v.begin(),v.end());
  56.     FOR(i,1,1<<wi)
  57.     {
  58.         LL w=0;
  59.         REP(j,wi)
  60.         {
  61.             if((1<<j) &i )
  62.                 w+=S[j+vi];
  63.         }
  64.         //binary search
  65.         //w+v_i>=A => min   i: v_i>=A-w
  66.         //w+v_i<=B => max   i: v_i<=B-w
  67.         w=A-w;
  68.         vector<LL>::iterator low=lower_bound(v.begin(),v.end(),w);
  69.         w=B-A+w;
  70.         LL dist=0;
  71.         vector<LL>::iterator up=upper_bound(v.begin(),v.end(),w);
  72.         dist=distance(low,up);
  73.         res+=dist;
  74.     }  
  75.     cout << res << endl;
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment