RainX_69

Count the Number of Square-Free Subsets | HARD LEETCODE PROBLEM | MATHY | OA

Feb 20th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. https://leetcode.com/problems/count-the-number-of-square-free-subsets/
  2.  
  3. You are given a positive integer 0-indexed array nums.
  4.  
  5. A subset of the array nums is square-free if the product of its elements is a square-free integer.
  6.  
  7. A square-free integer is an integer that is divisible by no square number other than 1.
  8.  
  9. Return the number of square-free non-empty subsets of the array nums. Since the answer may be too large, return it modulo 109 + 7.
  10.  
  11. A non-empty subset of nums is an array that can be obtained by deleting some (possibly none but not all) elements from nums. Two subsets are different if and only if the chosen indices to delete are different.
  12.  
  13.  
  14.  
  15. Example 1:
  16.  
  17. Input: nums = [3,4,4,5]
  18. Output: 3
  19. Explanation: There are 3 square-free subsets in this example:
  20. - The subset consisting of the 0th element [3]. The product of its elements is 3, which is a square-free integer.
  21. - The subset consisting of the 3rd element [5]. The product of its elements is 5, which is a square-free integer.
  22. - The subset consisting of 0th and 3rd elements [3,5]. The product of its elements is 15, which is a square-free integer.
  23. It can be proven that there are no more than 3 square-free subsets in the given array.
  24. Example 2:
  25.  
  26. Input: nums = [1]
  27. Output: 1
  28. Explanation: There is 1 square-free subset in this example:
  29. - The subset consisting of the 0th element [1]. The product of its elements is 1, which is a square-free integer.
  30. It can be proven that there is no more than 1 square-free subset in the given array.
  31.  
  32.  
  33. Constraints:
  34.  
  35. 1 <= nums.length <= 1000
  36. 1 <= nums[i] <= 30
  37.  
  38.  
  39. EXPLAINATION LINK -> https://leetcode.com/problems/count-the-number-of-square-free-subsets/discuss/3210623/C%2B%2B-or-KnapSack-or-DP-or-Recursive-Memo-or-Beginners-Friendly-or-Explained
  40. -----------------------------------------------------------------------------------------------------------------------------------
  41.  
  42.  
  43. class Solution {
  44. public:
  45.     long long dp[1<<11][1001];
  46.    
  47.     unordered_map<int,int> hash;  // the numbers are mapped to reduce bit count
  48.    
  49.     int primeFactorisation_MASK(int num){
  50.         int mask=0;
  51.            
  52.         if(num%2==0){
  53.             mask|=(1<<hash[2]);
  54.             num/=2;
  55.         }
  56.         for(int i=3;i<=sqrt(num);i+=2){
  57.             while(num%i==0){
  58.                 mask|=(1<<hash[i]);
  59.                 num=num/i;
  60.             }
  61.         }
  62.         if(num>2){
  63.             mask|=(1<<hash[num]);
  64.         }
  65.        
  66.         return mask;
  67.     }
  68.    
  69.     long long helper(int mask, int curr, vector<int> &masks){
  70.         if(curr==masks.size()){
  71.             return 1;
  72.         }
  73.         if(dp[mask][curr]!=-1){
  74.             return dp[mask][curr];
  75.         }
  76.        
  77.         int ACCEPT=0;
  78.         if((masks[curr] & mask)==0){ // checking that there is no common set bit in both masks cuz that will lead to a square number
  79.             ACCEPT=helper(masks[curr] | mask,curr+1,masks);
  80.         }
  81.        
  82.         int IGNORE=helper(mask,curr+1,masks);
  83.        
  84.         return dp[mask][curr]=(ACCEPT+IGNORE)%1000000007;
  85.     }
  86.    
  87.     int squareFreeSubsets(vector<int>& nums) {
  88.         int primeFactors[]={1,2,3,5,7,11,13,17,19,23,29}; // these numbers make up the prime factorizations from 1-30
  89.  
  90.         for(int i=0;i<11;i++){ // mapping squareFreePrimes to smaller bit places
  91.             hash[primeFactors[i]]=i;
  92.         }
  93.        
  94.         vector<int> masks;
  95.         for(auto x: nums){
  96.             if(x==4 ||  x==8 || x==9 || x==12 || x==16 || x==18 || x==20 || x==24 || x==25 || x==27 || x==28){ // number having more than 1 duplicate primes leading to a square number
  97.                 continue;
  98.             }
  99.            
  100.             int mask=primeFactorisation_MASK(x);
  101.             masks.push_back(mask);
  102.         }
  103.        
  104.         memset(dp,-1,sizeof(dp));
  105.         return helper(0,0,masks)-1;
  106.     }
  107. };
  108.  
Advertisement
Add Comment
Please, Sign In to add comment