RainX_69

Count subsequences where AND of elements consists of k set bits | OA | TRICKY | HARD

May 9th, 2023
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 KB | Source Code | 0 0
  1. https://codeforces.com/contest/1829/problem/H
  2.  
  3. Given an array a, consisting of n positive integers. Count the number of subsequences for which the bitwise AND elements in the subsequence has exactly k single bits in binary representation. The answer can be large, so print it modulo 10^9+7.
  4. Recall that a subsequence of an array a is the sequence that can be obtained from a, removing some (maybe none) of the elements. For example,[1,2,3], [3], [1,3] are subsequences[1,2,3], but[3,2] and [4,5,6] are Not.
  5. note that AND stands for the logical AND operation .
  6.  
  7. Input data
  8. Each test contains several sets of input data. The first line contains the number of tests t (1≤t≤10^4). This is followed by a description of the input data sets.
  9. The first line of each test case consists of two integers n and k (1≤n≤210^5 , 0≤k≤6)
  10. The second line of each test case consists of n integers ai(0≤ai≤63)
  11.  
  12. It is guaranteed that the sum n for all test cases does not exceed 210^5.
  13.  
  14. Output
  15. For each test case, print one integer - the number of subsequences whose binary representation has the value of the bitwise AND
  16. has exactly k set bits. The answer can be large, so print it modulo 10^9+7.
  17.  
  18. Example
  19. input data
  20. 6
  21. 5 1
  22. 1 1 1 1 1
  23. 4 0
  24. 0 1 2 3
  25. 5 1
  26. 5 5 7 4 2
  27. 1 2
  28. 3
  29. 12 0
  30. 0 2 0 2 0 2 0 2 0 2 0 2
  31. 10 6
  32. 63 0 63 5 5 63 63 4 12 13
  33. output
  34. 31
  35. 10
  36. 10
  37. 1
  38. 4032
  39. 15
  40.  
  41. -------------------------------------------------------------------------------------------------------------------------------------
  42.  
  43. ITERATIVE SOLUTION
  44.  
  45. #include<bits/stdc++.h>
  46. using namespace std;
  47.  
  48. void solve(){
  49.     int n;
  50.     int k;
  51.     cin>>n>>k;
  52.     vector<int> arr(n);
  53.     for(int i=0;i<n;i++){
  54.         cin>>arr[i];
  55.     }
  56.     long long cnt[64]={0};
  57.     long long tmp[64]={0};
  58.     for(int i=0;i<n;i++){
  59.         memset(tmp,0,sizeof(tmp));
  60.         for(int j=0;j<64;j++){
  61.             (tmp[arr[i] & j]+=cnt[j])%1000000007;
  62.         }
  63.         for(int j=0;j<64;j++){
  64.             cnt[j]+=tmp[j];
  65.             cnt[j]%=1000000007;
  66.         }
  67.         cnt[arr[i]]++;
  68.         cnt[arr[i]]%1000000007;
  69.     }
  70.     long long res=0;
  71.     for(int i=0;i<64;i++){
  72.         if(__builtin_popcount(i)==k){
  73.             res+=cnt[i];
  74.             res%=1000000007;
  75.         }
  76.     }
  77.     cout<<res<<endl;
  78. }
  79.  
  80. int main(){
  81.     int TC;
  82.     cin>>TC;
  83.     while(TC--){
  84.         solve();
  85.     }
  86. }
  87.  
  88. ---------------------------------------------------------------------------------------------------------------------------------
  89.  
  90. RECURSIVE SOLUTION
  91.  
  92. #include<bits/stdc++.h>
  93. using namespace std;
  94.  
  95. long long MOD=1000000007;
  96.  
  97. long long dp[200001][64][2];
  98. int N;
  99.  
  100. // flag here represents whether the AND consists of non empty subsequences
  101.  
  102. long long helper(vector<int> &arr, int curr, int AND, int k, int flag){
  103.     if(curr==N){
  104.         return __builtin_popcount(AND)==k && flag==1;
  105.     }
  106.     if(dp[curr][AND][flag]!=-1){
  107.         return dp[curr][AND][flag];
  108.     }
  109.     long long IGNORE=helper(arr,curr+1,AND,k,flag);
  110.     long long ACCEPT=0;
  111.     if(flag==1){
  112.         ACCEPT=helper(arr,curr+1,AND & arr[curr],k,flag);
  113.     }
  114.     else{
  115.         ACCEPT=helper(arr,curr+1,arr[curr],k,1);
  116.     }
  117.     return dp[curr][AND][flag]=(ACCEPT+IGNORE)%MOD;    
  118. }
  119.  
  120. void solve(){
  121.     int n;
  122.     int k;
  123.     cin>>n>>k;
  124.     N=n;
  125.     vector<int> arr(n);
  126.     for(int i=0;i<n;i++){
  127.         cin>>arr[i];
  128.     }
  129.     for(int i=0;i<n;i++){
  130.         for(int j=0;j<64;j++){
  131.             dp[i][j][0]=-1;
  132.             dp[i][j][1]=-1;
  133.         }
  134.     }
  135.     cout<<helper(arr,0,0,k,0)<<endl;
  136. }
  137.  
  138. int main(){
  139.     int TC;
  140.     cin>>TC;
  141.     while(TC--){
  142.         solve();
  143.     }
  144. }
  145.  
Advertisement
Add Comment
Please, Sign In to add comment