Boolean Product of Boolean Matrices
Time limit2sMemory limit512 MB
Compute the Boolean product of two N by N 0/1 matrices and count how many entries in the result are 1.
- Level
Medium4 of 10
- Topics
- Matrix, Brute force, Bit manipulation
- Solved
- No attempts yet
Problem
While setting problems, Ukje suddenly wanted to write a twisted one. Unfortunately, many contestants in this contest are new to programming, so he could not. Still, Ukje could not give up his urge to torment the freshmen.
"Ha ha! Can the freshmen really solve this one?"
The task is simple. You are given two Boolean matrices (matrices made only of 0s and 1s) and . Compute their Boolean product and count the 1s in . The Boolean product is computed as follows.
Here is the element in row , column of matrix , is logical AND, and is logical OR. Now, start coding!
Input
The first line contains the matrix size (). The next lines contain the Boolean matrix , and the lines after that contain the Boolean matrix . Each of these lines contains integers, each 0 or 1, separated by spaces.
Output
Print the number of 1s in the matrix , the Boolean product of and .
Hint
The Boolean product for sample 1 is:
1 1 1
1 1 1
0 0 1
So the number of 1s is 7.
The Boolean product for sample 2 is:
0 0
1 1
So the number of 1s is 2.