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
| class Solution { static int MOD = (int) 1e9 + 7; static int[] p = new int[]{2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
public int numberOfGoodSubsets(int[] nums) { int[] cnts = new int[35]; for (int num : nums) ++cnts[num]; int mask = 1 << 10; long[] f = new long[mask]; f[0] = 1; for (int num = 2; num <= 30; ++num) { if (cnts[num] == 0) continue; int cur = 0, x = num; boolean flag = true; for (int j = 0; j < 10 && flag; ++j) { int t = p[j], c = 0; while (x % t == 0) { cur |= (1 << j); x /= t; ++c; } if (c > 1) flag = false; } if (!flag) continue; for (int prev = mask - 1; prev >= 0; prev--) { if ((prev & cur) != 0) continue; f[prev | cur] = (f[prev | cur] + f[prev] * cnts[num]) % MOD; } } long ans = 0; for (int i = 1; i < mask; ++i) ans = (ans + f[i]) % MOD; for (int i = 0; i < cnts[1]; ++i) ans = ans * 2 % MOD; return (int) ans; } }
|