|
16 | 16 | * Tags: Bit Manipulation, Prime Numbers, Combinatorics, Greedy, Bitmask
|
17 | 17 | */
|
18 | 18 |
|
| 19 | +#include <bit> |
19 | 20 | #include <iostream>
|
20 | 21 | #include <vector>
|
21 | 22 | #include <unordered_set>
|
22 | 23 | #include <algorithm>
|
23 | 24 |
|
24 | 25 | const int MAX = 1e6;
|
25 |
| -std::vector<bool> is_prime; |
| 26 | +static std::vector<bool> is_prime; |
26 | 27 |
|
27 | 28 | /**
|
28 |
| - * @brief Precomputes prime numbers up to MAX using the Sieve of Eratosthenes. |
| 29 | + * @namespace bit_manipulation |
| 30 | + * @brief Bit manipulation algorithms |
29 | 31 | */
|
30 |
| -void precomputePrimes() { |
31 |
| - is_prime.assign(MAX + 1, true); |
32 |
| - is_prime[0] = is_prime[1] = false; |
33 |
| - for (int i = 2; i * i <= MAX; i++) { |
34 |
| - if (is_prime[i]) { |
35 |
| - for (int j = i * i; j <= MAX; j += i) { |
36 |
| - is_prime[j] = false; |
| 32 | +namespace bit_manipulation { |
| 33 | + |
| 34 | + void precomputePrimes() { |
| 35 | + is_prime.assign(MAX + 1, true); |
| 36 | + is_prime[0] = is_prime[1] = false; |
| 37 | + for (int i = 2; i * i <= MAX; i++) { |
| 38 | + if (is_prime[i]) { |
| 39 | + for (int j = i * i; j <= MAX; j += i) { |
| 40 | + is_prime[j] = false; |
| 41 | + } |
37 | 42 | }
|
38 | 43 | }
|
39 | 44 | }
|
40 |
| -} |
41 |
| - |
42 |
| -/** |
43 |
| - * @brief Counts distinct prime numbers that can be formed from the given binary string. |
44 |
| - * @param s Binary string input |
45 |
| - * @return Number of distinct primes possible after allowed transformations |
46 |
| - */ |
47 |
| -int countPrimeBinaryStrings(const std::string &s) { |
48 |
| - int n = s.length(); |
49 |
| - int k = std::count(s.begin(), s.end(), '1'); |
50 |
| - int cnt = 0; |
51 |
| - int limit = 1 << n; |
52 |
| - |
53 |
| - std::unordered_set<int> seen; |
54 |
| - |
55 |
| - for (int i = 2; i < limit; i++) { |
56 |
| - if (__builtin_popcount(i) <= k && is_prime[i]) { |
57 |
| - if (!seen.count(i)) { |
58 |
| - cnt++; |
59 |
| - seen.insert(i); |
| 45 | + |
| 46 | + /** |
| 47 | + * @brief Counts distinct prime numbers that can be formed from the given binary string. |
| 48 | + * @param s Binary string input |
| 49 | + * @return Number of distinct primes possible after allowed transformations |
| 50 | + */ |
| 51 | + int countPrimeBinaryStrings(const std::string &s) { |
| 52 | + int n = s.length(); |
| 53 | + int k = std::count(s.begin(), s.end(), '1'); |
| 54 | + int cnt = 0; |
| 55 | + int limit = 1 << n; |
| 56 | + |
| 57 | + std::unordered_set<int> seen; |
| 58 | + |
| 59 | + for (int i = 2; i < limit; i++) { |
| 60 | + if (std::popcount(i) <= k && is_prime[i]) { |
| 61 | + if (!seen.count(i)) { |
| 62 | + cnt++; |
| 63 | + seen.insert(i); |
| 64 | + } |
60 | 65 | }
|
61 | 66 | }
|
| 67 | + |
| 68 | + return cnt; |
62 | 69 | }
|
63 |
| - |
64 |
| - return cnt; |
65 |
| -} |
66 |
| - |
| 70 | + |
| 71 | + void tests(){ |
| 72 | + precomputePrimes(); |
| 73 | + std::string s; |
| 74 | + std::cin >> s; |
| 75 | + std::cout << countPrimeBinaryStrings(s) << std::endl; |
| 76 | + } |
| 77 | + |
| 78 | +} //bit manipulation |
67 | 79 | /**
|
68 | 80 | * @brief Main function to test the algorithm.
|
69 | 81 | */
|
70 | 82 | int main() {
|
71 |
| - precomputePrimes(); |
72 |
| - std::string s; |
73 |
| - std::cin >> s; |
74 |
| - std::cout << countPrimeBinaryStrings(s) << std::endl; |
| 83 | + tests(); |
75 | 84 | return 0;
|
76 | 85 | }
|
77 |
| - |
|
0 commit comments