Given two numbers x, y which denotes the number of set bits. Also given is a number C. The task is to print the number of ways in which we can form two numbers A and B such that A has x number of set bits and B has y number of set bits and A+B = C.
Input: X = 1, Y = 1, C = 3 Output: 2 So two possible ways are (A = 2 and B = 1) and (A = 1 and B = 2) Input: X = 2, Y = 2, C = 20 Output: 3
Approach: The above problem can be solved using bitmask DP.
- Initialise a 4-D DP array of size 64 * 64 * 64 * 2 as 10^18 has a maximum of 64 set bits with -1
- The first state of the DP array stores the number of bits traversed in C from right. The second state stores the number of set-bits used out of X and third state stores the number of set bits used out of Y. The fourth state is the carry bit which refers to the carry generated when we perform an addition operation.
- The recurrence will have 4 possibilities. We start from the rightmost bit position.
- If the bit position at C is 1, then there are four possibilities to get 1 at that index.
- If the carry is 0, then we can use 1 bit out of X and 0 bit out of Y, or the vice-versa which generates no carry for next step.
- If the carry is 1, then we can use 1 set bit’s from each which generates a carry for the next step else we use no set bits from X and Y which generates no carry.
- If the bit position at C is 0, then there are four possibilities to get 0 at that bit position.
- If the carry is 1, then we can use 1 set bit out of X and 0 bit out of Y or the vice versa which generates a carry of 1 for the next step.
- If the carry is 0, then we can use 1 and 1 out of X and Y respectively, which generates a carry of 1 for next step. We can also use no set bits, which generates no carry for the next step.
- Summation of all possibilites is stored in dp[third][seta][setb][carry] to avoid re-visiting same states.
Below is the implementation of the above approach:
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count pairs with set bits sum equal to K
- Count all pairs of an array which differ in K bits
- Count pairs in an array such that both elements has equal set bits
- Check if bits of a number has count of consecutive set bits in increasing order
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count total bits in a number
- Count unset bits of a number
- Count Set-bits of number using Recursion
- Count number of bits changed after adding 1 to given N
- Count number of bits to be flipped to convert A to B | Set-2
- Program to count number of set bits in an (big) array
- Count number of set bits in a range using bitset
- Count number of bits to be flipped to convert A to B
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.