Java’s BitSet class implements a vector of bit values (i.e.: false (0) or true (1)) that grows as needed, allowing us to easily manipulate bits while optimizing space (when compared to other collections). Any element having a bit value of 1 is called a set bit.
Given 2 BitSets, B1 and B2, of size N where all bits in both BitSets are initialized to 0, perform a series of M operations. After each operation, print the number of set bits in the respective BitSets as two space-separated integers on a new line.
Input Format
The first line contains 2 space-separated integers, N (the length of both BitSets B1 and B2) and M (the number of operations to perform), respectively.
The M subsequent lines each contain an operation in one of the following forms:
In the list above,<set> is the integer 1 or 2, where 1 denotes B1 and 2 denotes B2.
<index>is an integer denoting a bit’s index in the BitSet corresponding to <set>.
For the binary operations AND, OR, and XOR, operands are read from left to right and the BitSet resulting from the operation replaces the contents of the first operand. For example:
AND 2 1
B2 is the left operand, and B1 is the right operand. This operation should assign the result of B2 ^ B1 to B2.
Constraints
- 1 ≤ N ≤1000
- 1 ≤ M ≤10000
Output Format
After each operation, print the respective number of set bits in BitSet B1 and BitSet B2 as 2 space-separated integers on a new line.
Sample Input
5 4
AND 1 2
SET 1 4
FLIP 2 2
OR 2 1
Sample Output
0 0
1 0
1 1
1 2
Explanation
Initially: N = 5, M = 4 , B1 = {0, 0, 0, 0, 0}, and B2 = {0, 0, 0, 0, 0}. At each step, we print the respective number of set bits in B1 and B2 as a pair of space-separated integers on a new line.
Mo = AND 12
B 1 =B 1 ^ B 2 = {0, 0, 0, 0, 0} ^ {0, 0, 0, 0, 0} ={ 0,0,0,0,0}
B1 = {0, 0, 0, 0, 0},
B2 = {0, 0, 0, 0, 0}
The number of set bits in B1 and B2 is 0.
M₁ = SET 14
Set B1[4] to true (1).
B1 = {0, 0, 0, 0, 1} ,
B2 = {0, 0, 0, 0, 0},
The number of set bits in B1 is 1 and B2 is 0.
M2 = FLIP 2 2
Flip B2[2] from false (0) to true (1).
B1 = {0, 0, 0, 0, 1}
B2 = {0, 0, 1, 0, 0}
The number of set bits in B1 is 1 and B2 is 1.
M3 =OR 2 1
B 2 =B 2 vee B 1 ={0,0,1,0,0} v {0, 0, 0, 0, 1} = {0, 0, 1, 0, 1}
B1 = {0, 0, 0, 0, 1},
B2 = {0, 0, 1, 0, 1},
The number of set bits in B1 is 1 and B2 is 2.
Solution Implementation
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class JavaPrimalityTest
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int bitSetSize = sc.nextInt();
int numOperations = sc.nextInt();
BitSet[] bitSetArray = new BitSet[2];
bitSetArray[0] = new BitSet(bitSetSize);
bitSetArray[1] = new BitSet(bitSetSize);
sc.nextLine();
for(int i = 0;i < numOperations; i++)
{
String[] opArray = sc.nextLine().split(" ");
switch(opArray[0]){
case "AND": bitSetArray[Integer.parseInt(opArray[1])-1].and(bitSetArray[Integer.parseInt(opArray[2])-1]);
break;
case "OR": bitSetArray[Integer.parseInt(opArray[1])-1].or(bitSetArray[Integer.parseInt(opArray[2])-1]);
break;
case "XOR": bitSetArray[Integer.parseInt(opArray[1])-1].xor(bitSetArray[Integer.parseInt(opArray[2])-1]);
break;
case "FLIP": bitSetArray[Integer.parseInt(opArray[1])-1].flip(Integer.parseInt(opArray[2]));
break;
case "SET": bitSetArray[Integer.parseInt(opArray[1])-1].set(Integer.parseInt(opArray[2]));
break;
}
System.out.println(bitSetArray[0].cardinality() + " " + bitSetArray[1].cardinality());
}
}
}