### 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) = A_{0} + A_{1} * x + A_{2} * x^2 + … A_{n} * 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 Phi_{d}(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 Phi_{d}(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 **Phi _{d}(x)** for some divisor

**d**of

**n**. Instead of direct calculation of

**Phi**we can do the following trick. Let

_{d}(x)**d**has prime divisors

**p1, …, pk**. Now the crucial observation is the following:

Some polynomial **P(x)** is divisible by **Phi _{d}(x)** if and only if

**P(x) * (x**is divisible by

^{d/p1}- 1) * … * (x^{d/pk}- 1)**x**.

^{d}- 1It follows from explicit formula for cyclotomic polynomial (see second formula there). Indeed the rational function

**(x ^{d/p1} - 1) * … * (x^{d/pk} - 1) / x^{d} - 1**

equals to

**Q**for some polynomial

_{d}(x) / Phi_{d}(x)**Q**that is coprime to

_{d}(x)**Phi**which yields the result.

_{d}(x)Now all is simple. Polynomial computations modulo **x ^{d} - 1** are straightforward: if

**P(x) = a**then

_{0}+ a_{1}* x + … + a_{m}* x^{m}**P(x) mod (x**(for clarity see lines 49-50 here). Multiplication by

^{d}- 1) = (a_{0}+ a_{d}+ a_{2d}+ … ) + (a_{1}+ a_{d+1}+ a_{2d+1}+ … ) * x + … + (a_{d-1}+ a_{2d-1}+ … ) * x^{d-1}**x**could be made by simple loop and requires only additions and subtractions. And it could be done modulo

^{k}- 1**x**at the same time (see lines 53-62 here).

^{d}- 1So we could easily find the remainder of polynomial division **P(x) * (x ^{d/p1} - 1) * … * (x^{d/pk} - 1)** by

**x**. If it’s zero for some

^{d}- 1**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 **x ^{d} - 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.