Skip to content

Commit 25a5d73

Browse files
authored
Changes done in count_distinct_primes_from_binary_string.cpp
1 parent 789a92c commit 25a5d73

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

bit_manipulation/count_distinct_primes_from_binary_string.cpp

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,62 +16,70 @@
1616
* Tags: Bit Manipulation, Prime Numbers, Combinatorics, Greedy, Bitmask
1717
*/
1818

19+
#include <bit>
1920
#include <iostream>
2021
#include <vector>
2122
#include <unordered_set>
2223
#include <algorithm>
2324

2425
const int MAX = 1e6;
25-
std::vector<bool> is_prime;
26+
static std::vector<bool> is_prime;
2627

2728
/**
28-
* @brief Precomputes prime numbers up to MAX using the Sieve of Eratosthenes.
29+
* @namespace bit_manipulation
30+
* @brief Bit manipulation algorithms
2931
*/
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+
}
3742
}
3843
}
3944
}
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+
}
6065
}
6166
}
67+
68+
return cnt;
6269
}
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
6779
/**
6880
* @brief Main function to test the algorithm.
6981
*/
7082
int main() {
71-
precomputePrimes();
72-
std::string s;
73-
std::cin >> s;
74-
std::cout << countPrimeBinaryStrings(s) << std::endl;
83+
tests();
7584
return 0;
7685
}
77-

0 commit comments

Comments
 (0)