Advertisement
Ahmed_Negm

Untitled

Apr 9th, 2023
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. #define OO 2'000'000'000
  8. #define ull unsigned long long
  9. #define nl '\n'
  10. #define sz(x) (ll)(x.size())
  11. #define all(x) x.begin(),x.end()
  12. #define rall(s)  s.rbegin(), s.rend()
  13. #define getline(s) getline(cin>>ws,s)
  14. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  15. #define pi  3.141592653589793
  16. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  17. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  18.  
  19.  
  20. void Fast_IO(){
  21. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  22. // freopen("filename.in", "r", stdin);
  23. // freopen("filename.txt", "w", stdout);
  24. #ifndef ONLINE_JUDGE
  25. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  33. int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  34.  
  35.  
  36. ll n, m, k;
  37. vector<ll> v;
  38.  
  39.  
  40. ll DAC(){
  41.     vector<vector<ll>> left(41), right(41);
  42.     int mid = n/2;
  43.     vector<ll> l(v.begin(), v.begin()+mid), r(v.begin()+mid, v.end());
  44.     for(int i = 0; i < (1<<mid); i++){
  45.         ll sum = 0;
  46.         for(int j = 0; j < mid; j++){
  47.             if(i & (1<<j)){
  48.                 sum += l[j];
  49.             }
  50.         }
  51.         if(sum<=k and __builtin_popcount(i)<=m)
  52.             left[__builtin_popcount(i)].emplace_back(sum);
  53.     }
  54.  
  55.     for(int i = 0; i < (1<<(n-mid)); i++){
  56.         ll sum = 0;
  57.         for(int j = 0; j < (n-mid); j++){
  58.             if(i & (1<<j)){
  59.                 sum += r[j];
  60.             }
  61.         }
  62.         if(sum<=k and __builtin_popcount(i)<=m)
  63.             right[__builtin_popcount(i)].emplace_back(sum);
  64.     }
  65.  
  66.     for(int i = 0; i <= 40; i++){
  67.         sort(all(left[i]));
  68.         sort(all(right[i]));
  69.     }
  70.  
  71.     ll ans = 0;
  72.  
  73.     for(int i=0;i<=m;i++){
  74.         for(int j=0;j<=m;j++){
  75.             if(i+j>m)continue;
  76.             for(auto &x : left[i]){
  77.                 ll rem = k-x;
  78.                 if(rem<0)continue;
  79.                 auto it = upper_bound(all(right[j]), rem);
  80.                 ans += it - right[j].begin();
  81.             }
  82.         }
  83.     }
  84.  
  85.  
  86.     return ans;
  87. }
  88.  
  89.  
  90.  
  91. void solve(){
  92. cin>>n>>m>>k;
  93. v = vector<ll>(n);
  94. for(auto &i : v)cin>>i;
  95.  
  96. sort(all(v));
  97. ll ans = DAC();
  98. cout<<ans<<nl;
  99.  
  100.  
  101.  
  102.  
  103. }
  104.  
  105. int main(){
  106.     Fast_IO();
  107. int t =1;
  108. //cin>>t;
  109. while(t--){
  110. solve();
  111. }
  112. return 0;
  113. }  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement