PROBLEM LINK:
Author: Hiroto Sekido
Tester: Anton Lunyov
Editorialist: Anton Lunyov
DIFFICULTY:
HARD
PREREQUISITES:
Determinant, Cauchy-Binet formula, Gauss-Jordan elimination, Bitwise operations
PROBLEM:
Getting rid of the story line we have the following problem.
You are given N × N matrices Q1, Q2, …, QB.
For each a and b such that 1 ≤ a < b ≤ B we construct the matrix Ca,b by the formula
Ca,b[i][j] = Sum[Qa[i][k] * Qb[j][k] : 1 ≤ k ≤ N] for 1 ≤ i, j ≤ N.
For each permutation p = {p[1], p[2], …, p[N]} of integers from 1 to N, inclusive, we consider the sequence
Tr§ = {Ca,b[i][p[i]] : 1 ≤ i ≤ N}.
Denote by CNT(a, b) the number of permutations p, for which the sequence Tr§ has at least one odd element.
We call the pair (a, b) good if CNT(a, b) is odd.
Now the task is to calculate the total number of good pairs.
QUICK EXPLANATION:
In the case N = 1 the answer is M * (M − 1) / 2 where M be the number of odd values among {Qa[1][1] : 1 ≤ a ≤ B}.
So let N be at least 2.
Since permanent of the matrix coincides with its determinant modulo 2 (see here) then the pair (a, b) is good if and only if det(Da,b) mod 2 = 1, where Da,b[i][j] = (Ca,b[i][j] + 1) mod 2.
Let’s add column composed of ones to each matrix Qc and assume that each Qc is actually N × (N+1) matrix having last column composed of ones. Then Da,b = (Qa)T * Qb mod 2, where AT is the matrix transpose to A.
By Cauchy-Binet formula det(Da,b) mod 2 = Sum[det(Qa,c) * det(Qb,c) : 1 ≤ c ≤ N+1] mod 2,
where Qa,c is the matrix obtained from Qa by deleting its c-th column.
Introducing numbers Va = Sum[(det(Qa,c) mod 2) * 2c-1 : 1 ≤ c ≤ N+1] we can rewrite det(Da,b) mod 2 as bitCount(Va & Vb) mod 2, where X & Y is a bitwise “and” of the numbers X and Y, and bitCount(X) is the number of ones in binary of X. Using built-in function __builtin_parityll()
of GNU C++ Compiler or doing some precalc we can find det(Da,b) mod 2 in O(1) for each (a, b) after precomputing det(Qa,c) mod 2 for all a and c.
Applying Gauss-Jordan elimination to Qa modulo 2 we can assume that it is represented in reduced row echelon form since elementary row operations do not change any of the determinants det(Qa,c) mod 2. If rank of Qa is less than N then all determinants det(Qa,c) mod 2 are zero. Otherwise Qa could be obtained from identity matrix by inserting some column G = {G[1], …, G[N]} at some position (let it be k-th column of Qa). Then
det(Qa,c) mod 2 = G[c] for 1 ≤ c < k,
det(Qa,c) mod 2 = 1 for c = k, and
det(Qa,c) mod 2 = 0 for k < c ≤ N+1.
Since Gauss-Elimination could be performed in O(N2) time using bitwise operations we get the solution with complexity O(B * N2 + B2).
EXPLANATION:
##The case N = 1
In this case the matrix Ca,b has only one element and it equals to Qa[1][1] * Qb[1][1].
We have only one permutation p = {1} in this case.
Hence, the pair (a, b) is good if and only if Ca,b[1][1] is odd, which is equivalent to the oddness of both numbers Qa[1][1] and Qb[1][1].
This observation allows to come up with O(B) solution for this special case. Let M be the number of odd values among {Q1[1][1], Q2[1][1], …, QB[1][1]}. Since every good pair corresponds to some pair of odd numbers in this list, then the number of good pairs is M * (M − 1) / 2 and we are done.
From now on we assume that N ≥ 2.
##Reduction to the determinant
For the given pair (a, b) we construct another matrix such that its determinant is odd if and only if the pair (a, b) is good. Namely, let Da,b[i][j] = (Ca,b[i][j] + 1) mod 2. Then for each permutation p = {p[1], p[2], …, p[N]} the product
Product[Da,b[i][p[i]] : 1 ≤ i ≤ N] is zero if and only if we have at least one odd element among Ca,b[i][p[i]].
Indeed, when all they are even then Da,b[i][p[i]] = 1 for all i and the product equals to 1.
On the other hand, when we have at least one odd element, say Ca,b[i][p[i]], then Da,b[i][p[i]] = 0 and the product is zero.
Hence, the sum of these products over all permutations equals to the number of permutations p, for which the sequence Tr§ has all even elements, which is equal to N! − CNT(a, b).
Now the trick is that modulo 2 this sum coincide with the determinant of Da,b which is denoted as det(Da,b). Indeed, determinant also equals to the sum of these products over all permutations but each product is taken with some sign. But -1 and 1 has the same parity hence we can ignore signs when consider det(Da,b) mod 2.
Since N! is even when N ≥ 2 (that is one of the reasons to handle the case N = 1 separetely) then we get det(Da,b) mod 2 = CNT(a,b) mod 2.
Therefore, the pair (a, b) is good if and only if det(Da,b) mod 2 = 1.
This observation allows to get a solution with complexity O(B2 * N3) but this of course is too slow. We can optimize it to O(B2 * N2) using some bitwise operations but this still will be too slow.
##Representing Da,b as a product of matrices
Note that Da,b[i][j] = (Ca,b[i][j] + 1) mod 2
= (Sum[Qa[i][k] * Qb[j][k] : 1 ≤ k ≤ N] + 1) mod 2
= Sum[Qa[i][k] * Qb[j][k] : 1 ≤ k ≤ N+1] mod 2
if we put Qc[i][N+1] = 1 for all c and i.
In other words, we add column composed of ones to each matrix Qc.
So let’s assume that each Qc is actually N × (N+1) matrix having last column composed of ones.
This observation allows us to look at Da,b from another point of view.
Namely, Da,b = (Qa)T * Qb mod 2, where AT denotes the matrix transpose to the matrix A and star here is the standard matrix multiplication.
##Cauchy and Binet will save us
Now using Cauchy-Binet formula and the property det(A) = det(AT) we get
det(Da,b) mod 2 = Sum[det(Qa,c) * det(Qb,c) : 1 ≤ c ≤ N+1] mod 2,
where Qa,c is the matrix obtained from Qa by deleting its c-th column.
This observation gives O(B * N3 + B2) solution in the following way. We preprocess each matrix Qa by computing all the determinants det(Qa,c) mod 2. Each determinant could be computed in O(N2) time using Gaussian elimination and bitwise operations (see below). Since we need N+1 determinants for each matrix this step takes O(B * N3) operations in all.
Introducing numbers
Va = Sum[det(Qa,c) mod 2 * 2c-1 : 1 ≤ c ≤ N+1]
we can rewrite det(Da,b) mod 2 as bitCount(Va & Vb) mod 2, where X & Y is a bitwise “and” of the numbers X and Y, and bitCount(X) is the number of ones in binary of X. Since N ≤ 60 we can use 64-bit integer type to store Va. If you are using GNU C++ compiler then to calculate bitCount(X) mod 2 you can use built-in functions __builtin_parityll()
that returns exactly this number or more well-known __builtin_popcountll()
that returns the number of ones in binary. They seem to be implemented very fast and are sufficient to keep the double loop over all pairs (a, b) fast enough. See author’s solution as a reference. Another way is to precompute bitCount values for numbers up to some small bound (216 or 221) and then divide number into several parts of almost equal length in binary and compute bitCount as a sum of bitCounts of these parts using precomputed tables.
But such solution also appears to be slow due to O(B * N3) part (only mugurelionut was managed to optimize this enough to get AC).
##Gauss-Jordan elimination
The final step is to figure it out how to get the sequence {det(Qa,c) mod 2 : 1 ≤ c ≤ N+1} in O(N2) time.
For this we apply Gauss-Jordan elimination to the matrix Qa modulo 2.
That is, we reduce this matrix to reduced row echelon form using only elementary row operations performing all operations in the field Z2.
Note that each elementary row operation does not change any of the determinants det(Qa,c) mod 2.
Hence let’s assume that matrix Qa is already in its reduced row echelon form modulo 2.
At first we note that when rank of the matrix Qa mod 2 is less than N than all these determinants are zero.
When rank is N then its reduced row echelon form could be like this
100100 010000 001100 000010 000001
So it is obtained from identity matrix by inserting exactly one column. Moreover, if we insert the column G = {G[1], …, G[N]} to be the k-th column then we should have G[i] = 0 for i ≥ k so that it will not ruin row echelon form, while values G[1], …, G[k-1] could be arbitrary. The inserted column is bold in the example above.
Now the funny thing is that
det(Qa,c) mod 2 = G[c] for 1 ≤ c < k,
det(Qa,c) mod 2 = 1 for c = k, and
det(Qa,c) mod 2 = 0 for k < c ≤ N+1.
To see this we at first note that if 1 ≤ i1 < … < iL < k are positions of all ones in G then G is the sum of columns of the matrix Qa with indices i1, … iL. Hence if c is not equal to any of the i1, … iL and k then in the matrix Qa,c we have the column G and it is the linear combination of other columns of Qa,c. Hence det(Qa,c) mod 2 = 0 in this case. When we delete k-th column from Qa we simply get the identity matrix; therefore det(Qa,k) mod 2 = 1. Finally if c equals to one of the i1, … iL then all columns in Qa,c are linearly independent since column G can’t represented as sum of other columns; therefore det(Qa,c) mod 2 = 1 in this case. So we prove all formulas above.
The Gauss-Jordan elimination modulo 2 could be performed in O(N2) time using bitwise operation. For this we need represent each row of the matrix as 64-bit integer with binary form equal to this row. Then each pivoting could be made using “xor” operation in O(1) time.
Thus we get the solution with complexity O(B * N2 + B2) which is fast enough for this problem.
AUTHOR’S AND TESTER’S SOLUTIONS:
Author’s solution can be found here.
Tester’s solution can be found here.