1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| class Solution { unordered_map<int, unordered_map<int, long long>> dp; int ones = 1; int mod = 1e9 + 7; unordered_map<int, int> table{{2, 0}, {3, 1}, {5, 2}, {7, 3}, {11, 4}, {13, 5}, {17, 6}, {19, 7}, {23, 8}, {29, 9}}; int convert(int n) { int mask = 0; for(int i = 2; i <= 30 and n > 1; i++) { if(n % i == 0) { mask |= 1 << table[i]; n /= i; } if(n % i == 0) return -1; } return mask; } vector<int> convert(vector<int>& nums) { vector<int> res; unordered_map<int, int> cache; for(auto& n : nums) { if(n == 1) { ones++; } else if(cache.count(n)) { if(cache[n] != -1) res.push_back(cache[n]); } else { int mask = convert(n); cache[n] = mask; if(mask != -1) res.push_back(mask); } } return res; } long long helper(int mask, int pos, vector<pair<int, int>>& A) { if(pos == A.size()) return (mask > 0); if(dp.count(pos) and dp[pos].count(mask)) return dp[pos][mask]; long long& res = dp[pos][mask] = helper(mask, pos + 1, A); if(!(mask & A[pos].first)) { res = (res + A[pos].second * helper(mask | A[pos].first, pos + 1, A)) % mod; } return res; } long long modpow(int x, int n) { if(n == 0) return 1; long long res = modpow(x, n>>1); res = (res * res) % mod; if(n & 1) res = (res * x) % mod; return res; } public: int numberOfGoodSubsets(vector<int>& nums) { vector<int> A = convert(nums); unordered_map<int, int> freq; for(auto& a : A) freq[a]++; vector<pair<int, int>> B; for(auto& [m, c] : freq) B.push_back({m, c}); return modpow(2, ones - 1) * helper(0, 0, B) % mod; } };
|