problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 静态 Private 属性 | 所有成员列表
leetcode::the_number_of_good_subsets::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int numberOfGoodSubsets (vector< int > &nums)
 

静态 Private 属性

static constexpr int mask_max = 1 << primes.size()
 
static constexpr int mod = 1000000007
 
static constexpr int num_max = 30
 
static constexpr array< int, 10 > primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}
 

详细描述

在文件 leetcode.h1321 行定义.

成员函数说明

◆ numberOfGoodSubsets()

int leetcode::the_number_of_good_subsets::Solution::numberOfGoodSubsets ( vector< int > &  nums)
static

在文件 leetcode.cpp3391 行定义.

3391 {
3392 vector<int> freq(num_max + 1);
3393 for(const int num: nums) {
3394 freq[num]++;
3395 }
3396
3397 vector<int> f(mask_max);
3398 f[0] = 1;
3399 for(int i = 0; i < freq[1]; ++i) {
3400 f[0] = f[0] * 2 % mod;
3401 }
3402
3403 for(int i = 2; i <= num_max; ++i) {
3404 if(freq[i] == 0) {
3405 continue;
3406 }
3407
3408 // 检查 i 的每个质因数是否均不超过 1 个
3409 int subset = 0;
3410 const int x = i;
3411 bool check = true;
3412 for(int j = 0; j < primes.size(); j++) {
3413 const int prime = primes[j];
3414 if(x % (prime * prime) == 0) {
3415 check = false;
3416 break;
3417 }
3418 if(x % prime == 0) {
3419 subset |= 1 << j;
3420 }
3421 }
3422 if(!check) {
3423 continue;
3424 }
3425
3426 // 动态规划
3427 for(int mask = mask_max - 1; mask > 0; mask--) {
3428 if((mask & subset) == subset) {
3429 f[mask] = (f[mask] + static_cast<long long>(f[mask ^ subset]) * freq[i]) % mod;
3430 }
3431 }
3432 }
3433 int ans = 0;
3434 for(int mask = 1; mask < mask_max; mask++) {
3435 ans = (ans + f[mask]) % mod;
3436 }
3437 return ans;
3438 }
static constexpr array< int, 10 > primes
Definition: leetcode.h:1323

引用了 mask_max, mod, num_max , 以及 primes.

被这些函数引用 leetcode::the_number_of_good_subsets::TEST().

类成员变量说明

◆ mask_max

constexpr int leetcode::the_number_of_good_subsets::Solution::mask_max = 1 << primes.size()
staticconstexprprivate

在文件 leetcode.h1326 行定义.

被这些函数引用 numberOfGoodSubsets().

◆ mod

constexpr int leetcode::the_number_of_good_subsets::Solution::mod = 1000000007
staticconstexprprivate

在文件 leetcode.h1325 行定义.

被这些函数引用 numberOfGoodSubsets().

◆ num_max

constexpr int leetcode::the_number_of_good_subsets::Solution::num_max = 30
staticconstexprprivate

在文件 leetcode.h1324 行定义.

被这些函数引用 numberOfGoodSubsets().

◆ primes

constexpr array<int, 10> leetcode::the_number_of_good_subsets::Solution::primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}
staticconstexprprivate

在文件 leetcode.h1323 行定义.

被这些函数引用 numberOfGoodSubsets().


该类的文档由以下文件生成: