From 55d5f0d8b7d7f310309a9e3b4d5b6596876ff238 Mon Sep 17 00:00:00 2001 From: Rudraksh Tank <24ucs052@lnmiit.ac.in> Date: Sat, 19 Jul 2025 15:16:48 +0530 Subject: [PATCH 1/5] Add files via upload --- ...unt_distinct_primes_from_binary_string.cpp | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 bit_manipulation/count_distinct_primes_from_binary_string.cpp diff --git a/bit_manipulation/count_distinct_primes_from_binary_string.cpp b/bit_manipulation/count_distinct_primes_from_binary_string.cpp new file mode 100644 index 0000000000..ccb2e23f6d --- /dev/null +++ b/bit_manipulation/count_distinct_primes_from_binary_string.cpp @@ -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 +#include +#include +#include + +const int MAX = 1e6; +std::vector is_prime; + +/** + * @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 seen; + + for (int i = 2; i < limit; i++) { + if (__builtin_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; +} + From 628174d296891b8278d583c27cfd283093c9e3a1 Mon Sep 17 00:00:00 2001 From: Rudraksh Tank <24ucs052@lnmiit.ac.in> Date: Sat, 19 Jul 2025 15:18:14 +0530 Subject: [PATCH 2/5] Delete bit_manipulation/count_distinct_primes_from_binary_string.cpp --- ...unt_distinct_primes_from_binary_string.cpp | 77 ------------------- 1 file changed, 77 deletions(-) delete mode 100644 bit_manipulation/count_distinct_primes_from_binary_string.cpp diff --git a/bit_manipulation/count_distinct_primes_from_binary_string.cpp b/bit_manipulation/count_distinct_primes_from_binary_string.cpp deleted file mode 100644 index ccb2e23f6d..0000000000 --- a/bit_manipulation/count_distinct_primes_from_binary_string.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/** - * @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 -#include -#include -#include - -const int MAX = 1e6; -std::vector is_prime; - -/** - * @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 seen; - - for (int i = 2; i < limit; i++) { - if (__builtin_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; -} - From 05f1a9c045fa83299493a16fe8ee744ff9642b40 Mon Sep 17 00:00:00 2001 From: Rudraksh Tank <24ucs052@lnmiit.ac.in> Date: Sat, 19 Jul 2025 15:27:26 +0530 Subject: [PATCH 3/5] Add files via upload Add count_distinct_primes_from_binary_string.cpp --- ...unt_distinct_primes_from_binary_string.cpp | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 bit_manipulation/count_distinct_primes_from_binary_string.cpp diff --git a/bit_manipulation/count_distinct_primes_from_binary_string.cpp b/bit_manipulation/count_distinct_primes_from_binary_string.cpp new file mode 100644 index 0000000000..ccb2e23f6d --- /dev/null +++ b/bit_manipulation/count_distinct_primes_from_binary_string.cpp @@ -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 +#include +#include +#include + +const int MAX = 1e6; +std::vector is_prime; + +/** + * @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 seen; + + for (int i = 2; i < limit; i++) { + if (__builtin_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; +} + From 25a5d730c22fdb4123374685b23a07edd0bbd191 Mon Sep 17 00:00:00 2001 From: Rudraksh Tank <24ucs052@lnmiit.ac.in> Date: Thu, 21 Aug 2025 12:31:32 +0530 Subject: [PATCH 4/5] Changes done in count_distinct_primes_from_binary_string.cpp --- ...unt_distinct_primes_from_binary_string.cpp | 84 ++++++++++--------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/bit_manipulation/count_distinct_primes_from_binary_string.cpp b/bit_manipulation/count_distinct_primes_from_binary_string.cpp index ccb2e23f6d..361156c054 100644 --- a/bit_manipulation/count_distinct_primes_from_binary_string.cpp +++ b/bit_manipulation/count_distinct_primes_from_binary_string.cpp @@ -16,62 +16,70 @@ * Tags: Bit Manipulation, Prime Numbers, Combinatorics, Greedy, Bitmask */ +#include #include #include #include #include const int MAX = 1e6; -std::vector is_prime; +static std::vector is_prime; /** - * @brief Precomputes prime numbers up to MAX using the Sieve of Eratosthenes. + * @namespace bit_manipulation + * @brief Bit manipulation algorithms */ -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; +namespace bit_manipulation { + + 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 seen; - - for (int i = 2; i < limit; i++) { - if (__builtin_popcount(i) <= k && is_prime[i]) { - if (!seen.count(i)) { - cnt++; - seen.insert(i); + + /** + * @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 seen; + + for (int i = 2; i < limit; i++) { + if (std::popcount(i) <= k && is_prime[i]) { + if (!seen.count(i)) { + cnt++; + seen.insert(i); + } } } + + return cnt; } - - return cnt; -} - + + void tests(){ + precomputePrimes(); + std::string s; + std::cin >> s; + std::cout << countPrimeBinaryStrings(s) << std::endl; + } + +} //bit manipulation /** * @brief Main function to test the algorithm. */ int main() { - precomputePrimes(); - std::string s; - std::cin >> s; - std::cout << countPrimeBinaryStrings(s) << std::endl; + tests(); return 0; } - From 1213f92130b4cdc1ba0fccbce3c298c474c2ebf8 Mon Sep 17 00:00:00 2001 From: Rudraksh Tank <24ucs052@lnmiit.ac.in> Date: Sun, 24 Aug 2025 20:01:05 +0530 Subject: [PATCH 5/5] changes done to count_distinct_primes_from_binary_string.cpp --- ...unt_distinct_primes_from_binary_string.cpp | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/bit_manipulation/count_distinct_primes_from_binary_string.cpp b/bit_manipulation/count_distinct_primes_from_binary_string.cpp index 361156c054..0da58ad237 100644 --- a/bit_manipulation/count_distinct_primes_from_binary_string.cpp +++ b/bit_manipulation/count_distinct_primes_from_binary_string.cpp @@ -16,27 +16,31 @@ * Tags: Bit Manipulation, Prime Numbers, Combinatorics, Greedy, Bitmask */ -#include -#include -#include -#include -#include +#include ///< For std::popcount (bit manipulation utilities) +#include ///< For input/output stream handling (std::cin, std::cout) +#include ///< For dynamic array storage (std::vector) +#include ///< For storing distinct primes without duplicates (std::unordered_set) +#include ///< For algorithms like std::count +#include ///< For assert-based testing -const int MAX = 1e6; +const uint32_t MAX = 1e6; ///< Upper bound for prime sieve static std::vector is_prime; /** * @namespace bit_manipulation - * @brief Bit manipulation algorithms + * @brief Bit manipulation algorithms and prime preprocessing */ namespace bit_manipulation { + /** + * @brief Precomputes all prime numbers up to MAX using 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++) { + for (uint32_t i = 2; i * i <= MAX; i++) { if (is_prime[i]) { - for (int j = i * i; j <= MAX; j += i) { + for (uint32_t j = i * i; j <= MAX; j += i) { is_prime[j] = false; } } @@ -67,17 +71,26 @@ namespace bit_manipulation { return cnt; } - - void tests(){ - precomputePrimes(); - std::string s; - std::cin >> s; - std::cout << countPrimeBinaryStrings(s) << std::endl; - } - -} //bit manipulation + +} // namespace bit_manipulation + +/** + * @brief Static test function using assertions instead of I/O. + */ +static void tests() { + using namespace bit_manipulation; + + precomputePrimes(); + + // Example test cases + assert(countPrimeBinaryStrings("1") == 0); // Only "1" -> not prime + assert(countPrimeBinaryStrings("11") > 0); // Should form primes like 3 + assert(countPrimeBinaryStrings("101") > 0); // Can form primes like 5 + assert(countPrimeBinaryStrings("000") == 0); // No 1s -> no primes possible +} + /** - * @brief Main function to test the algorithm. + * @brief Main function to run tests. */ int main() { tests();