PROBLEM LINK:
Author:Iaroslav Tverdokhlib
Tester:Mahbub
Editorialist:Jingbo Shang
DIFFICULTY:
Hard
PREREQUISITES:
Circulant Matrix
PROBLEM:
Given a Circulant Matrix, determine whether it is full rank (i.e. non-zero determinant, or the null space is trivial).
EXPLANATION:
First of all, we need to know the matrix stated in the problem is a very special type of matrix called Circulant Matrix. There are a lot of properties about the determinant and rank of Circulant Matrix can be found in Wikipedia.
Let's summarize the properties looks relevant to this problem:
where, f(x) = A0 + A1 * x + A2 * x^2 + ... An * x^n.
To avoid the precision problems, we can choose some prime and one of its primitive root instead of the root of unity (it may have chances (really rare, but exists) to fail, but we can choose some different primes to check, e.g. 3 different primes). Also, it will better to try Number Theoretic Transform instead of double-valued FFT in the following steps.
One straight-forward way is to calculate the determinant directly (of course, using FFT or NTT, both setter and tester solved the problem in this way). But a lot of contestants failed due to the precision problem when checking whether a double value is non-zero.
Another way is the check whether "d = 0" i.e. gcd(x^n - 1, f(x)) is some constant. Here we take the later one to solve this problem. Another related knowledge is about Cyclotomic Polynomial.
With this property, we know that x^n-1 can be factorized into some cyclotomic polynomials Phid(x) for every divisor d of n. Actually, for all n between 1 and 30000, the number of divisors, denoted as D, is at most 96 (when n = 27720). Therefore, we can enumerate all possible divisors d and check the cyclotomic polynomial Phid(x) can divide f(x).
Then, the only thing we need to do is the polynomial remainder operation. If we adopt brute-force to do this, it will take O((N-d) * d) time. Directly applying this method (with some breaks) may get TLE. But there is a interesting view to combine this method with the double-valued FFT (like this method can work well when N is a prime). This solution accepted with a further check using this method.
The fastest and easiest solution (explanation provided by Anton Lunyov)
Let's understand how the fastest and shortest solution to this problem works.
As pointed above we need to check that our polynomial is divisible by Phid(x) for some divisor d of n. Instead of direct calculation of Phid(x) we can do the following trick. Let d has prime divisors p1, ..., pk. Now the crucial observation is the following:
Some polynomial P(x) is divisible by Phid(x) if and only if P(x) * (xd/p1 - 1) * ... * (xd/pk - 1) is divisible by xd - 1.
It follows from explicit formula for cyclotomic polynomial (see second formula there). Indeed the rational function
(xd/p1 - 1) * ... * (xd/pk - 1) / xd - 1
equals to Qd(x) / Phid(x) for some polynomial Qd(x) that is coprime to Phid(x) which yields the result.
Now all is simple. Polynomial computations modulo xd - 1 are straightforward: if P(x) = a0 + a1 * x + ... + am * xm then P(x) mod (xd - 1) = (a0 + ad + a2d + ... ) + (a1 + ad+1 + a2d+1 + ... ) * x + ... + (ad-1 + a2d-1 + ... ) * xd-1 (for clarity see lines 49-50 here). Multiplication by xk - 1 could be made by simple loop and requires only additions and subtractions. And it could be done modulo xd - 1 at the same time (see lines 53-62 here).
So we could easily find the remainder of polynomial division P(x) * (xd/p1 - 1) * ... * (xd/pk - 1) by xd - 1. If it's zero for some d then the answer is YES, otherwise NO.
The complexity of each step is O(n + d * nu(d)), where nu(d) is the number of prime divisors of d. Here we have n because we need first to adjust P(x) modulo xd - 1. Thus, overall complexity is (n * tau(n) + n * log n), where tau(n) is the number of divisors of n. Here n * log n is some estimate of the sum of d * nu(d) over divisors o n (I believe it is so :)). Hence we get super fast and simple solution that requires only additions and subtractions of polynomial coefficients.
Any different ideas and solutions are welcome.
AUTHOR'S AND TESTER'S SOLUTIONS:
Author's solution can be found here.
Tester's solution can be found here.