Skip to content
Open
77 changes: 77 additions & 0 deletions bit_manipulation/count_distinct_primes_from_binary_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @file count_distinct_primes_from_binary_string.cpp
* @brief Count distinct primes formed from binary strings using allowed operations.
*
* @author Rudraksh Tank
* @date July 2025
*
* @details
* Given a binary string, the task is to count how many distinct prime decimal numbers
* can be formed by:
* - Swapping any two characters (makes position irrelevant)
* - Changing any '1' to '0' (not the reverse)
*
* Efficient solution using bit manipulation and Sieve of Eratosthenes.
*
* Tags: Bit Manipulation, Prime Numbers, Combinatorics, Greedy, Bitmask
*/

#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
#include <bit> /// for std::popcount
#include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add documentation for all the above headers as shown in the example


const int MAX = 1e6;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max cannot be a negative number, hence replace int with uint32_t

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you still havent done this

Suggested change
const int MAX = 1e6;
const uint32_t MAX = 1e6;

std::vector<bool> is_prime;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::vector<bool> is_prime;
static std::vector<bool> is_prime;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you want to pre compute primes why not make this a constexpr? and assign in it directly? instead of a call from main. Also afaik vectors cant be used as constexpr in c++17 hence it might be preferred if you use std::array instead.

if any of this sounds confusing let me know I can explain or direct you to resources to learn about this feature of c++


/**
* @brief Precomputes prime numbers up to MAX using the Sieve of Eratosthenes.
*/
void precomputePrimes() {
is_prime.assign(MAX + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i <= MAX; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= MAX; j += i) {
is_prime[j] = false;
}
}
}
}

/**
* @brief Counts distinct prime numbers that can be formed from the given binary string.
* @param s Binary string input
* @return Number of distinct primes possible after allowed transformations
*/
int countPrimeBinaryStrings(const std::string &s) {
int n = s.length();
int k = std::count(s.begin(), s.end(), '1');
int cnt = 0;
int limit = 1 << n;

std::unordered_set<int> seen;

for (int i = 2; i < limit; i++) {
if (__builtin_popcount(i) <= k && is_prime[i]) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__builtin_popcount is not part of the STL library it only exists in GCC.
thus this should be replaced with std::popcount()

Suggested change
if (__builtin_popcount(i) <= k && is_prime[i]) {
if (std::popcount(i) <= k && is_prime[i]) {

if (!seen.count(i)) {
cnt++;
seen.insert(i);
}
}
}

return cnt;
}

/**
* @brief Main function to test the algorithm.
*/
int main() {
precomputePrimes();
std::string s;
std::cin >> s;
std::cout << countPrimeBinaryStrings(s) << std::endl;
return 0;
}