### PROBLEM LINKS

### DIFFICULTY

EASY

### PREREQUISITES

Kirchhoff’s Matrix Tree Theorem, Math

### PROBLEM

Find the number of Trees that contain **k*n** nodes, which are labeled from **0** to **k*n-1**, such that

- There is no edge between two vertices
**a**and**b**for which**a div k = b div k**, where**div**is the**integer****division**operator

### QUICK EXPLANATION

Let us assume there is a graph on **k*n** vertices such that for any two vertices **a** and **b**, if **a** **div** **k** is not equal to **b** **div** **k**, there is an edge between them. On such a graph, every unique **spanning** **tree** is a valid tree according to the definition of the problem statement.

Since there are only **3** possible different values of **k**, let us consider them one by one.

## k = 1

The **Kirchhoff’s** **Matrix** for a graph with **n** vertices will look like

n-1 -1 -1 ... -1 -1 n-1 -1 ... -1 -1 -1 n-1 ... -1 ... -1 -1 -1 ... n-1

We have to find the the **determinant** of any **cofactor** of the above matrix to find the number of spanning trees. Let us consider the **(n,n)** **cofactor**.

n-1 -1 -1 ... -1 -1 n-1 -1 ... -1 -1 -1 n-1 ... -1 ... -1 -1 -1 ... n-1

The **(n,n)** **cofactor** consists of **n-1** rows and **n-1** columns.

The determinant of the above matrix will give us the answer for **k = 1**. Let us perform some operations that do not affect the determinant and make the matrix **triangular**. This way the determinant is found as the **product of the terms in the major diagonal**.

Starting from the top row and proceeding downwards, let us subtract the next row from the current row. Thus

- Subtract row 2 from row 1
- Subtract row 3 from row 2, and so on

n -n 0 ... 0 0 0 n -n ... 0 0 0 0 n ... 0 0 .... 0 0 0 ... n -n -1 -1 -1 ... -1 n-1

- We can use the rows 1 to n-2 to eliminate the first -1 in row n-1
- We can use the rows 2 to n-2 to eliminate the second -1 in row n-1, and so on

Eventually we get

n -n 0 ... 0 0 0 n -n ... 0 0 ... 0 0 0 ... n -n 0 0 0 ... 0 1

Thus, the answer for **k = 1** is **n ^{n-2}**.

The analysis for **k = 2** and **k = 3** are much more involved.

### EXPLANATION

## k = 2

**(2n,2n)** **cofactor** of the Kirchhoff’s Matrix for a graph with **2*n** nodes will look like

2n-2 0 -1 -1 ... -1 -1 -1 0 2n-2 -1 -1 ... -1 -1 -1 -1 -1 2n-2 0 ... -1 -1 -1 -1 -1 0 2n-2 ... -1 -1 -1 ... -1 -1 -1 -1 ... 2n-2 0 -1 -1 -1 -1 -1 ... 0 2n-2 -1 -1 -1 -1 -1 ... -1 -1 2n-2

It contains **2n-1** rows and **2n-1** columns. Similar to how we subtracted the next row from the current one for k = 1, we will subtract the next row from the current one, starting from the top row and working downwards.

2n-2 -(2n-2) 0 0 ... 0 0 0 1 2n-1 -(2n-1) -1 ... 0 0 0 0 0 2n-2 -(2n-2) ... 0 0 0 0 0 1 2n-1 ... 0 0 0 ... 0 0 0 0 ... 2n-2 -(2n-2) 0 0 0 0 0 ... 1 2n-1 -(2n-1) -1 -1 -1 -1 ... -1 -1 2n-2

We can use the first row to eliminate terms on the left of the major diagonal in the second row. Similarly, we can use the third row to eliminate terms on the left of the major diagonal in the fourth row, and so on.

2n-2 -(2n-2) 0 0 ... 0 0 0 0 2n -(2n-1) -1 ... 0 0 0 0 0 2n-2 -(2n-2) ... 0 0 0 0 0 0 2n ... 0 0 0 ... 0 0 0 0 ... 2n-2 -(2n-2) 0 0 0 0 0 ... 0 2n -(2n-1) -1 -1 -1 -1 ... -1 -1 2n-2

We can use row 3 on row 2, row 5 on row 4 and so on to convert the matrix into

2n-2 -(2n-2) 0 0 ... 0 0 0 0 2n 0 -2n ... 0 0 0 0 0 2n-2 -(2n-2) ... 0 0 0 0 0 0 2n ... 0 0 0 ... 0 0 0 0 ... 2n-2 -(2n-2) 0 0 0 0 0 ... 0 2n -(2n-1) -1 -1 -1 -1 ... -1 -1 2n-2

Now we can convert the final row using all the previous rows into

2n-2 -(2n-2) 0 0 ... 0 0 0 0 2n 0 -2n ... 0 0 0 0 0 2n-2 -(2n-2) ... 0 0 0 0 0 0 2n ... 0 0 0 ... 0 0 0 0 ... 2n-2 -(2n-2) 0 0 0 0 0 ... 0 2n -(2n-1) 0 0 0 0 ... 0 -(2n-2) 2n-2

The last row can reduce the second to last row into

2n-2 -(2n-2) 0 0 ... 0 0 0 0 2n 0 -2n ... 0 0 0 0 0 2n-2 -(2n-2) ... 0 0 0 0 0 0 2n ... 0 0 0 ... 0 0 0 0 ... 2n-2 -(2n-2) 0 0 0 0 0 ... 0 1 0 0 0 0 0 ... 0 -(2n-2) 2n-2

Of course the matrix is triangular now and the determinant is simply the product of the terms in the major diagonal. There are **n** occurances of **2n-2** and **n-2** occurances of **2n** in the major diagonal.

Thus, the answer for **k=2** is **(2n) ^{n-2}(2n-2)^{n}**.

We leave **k = 3** as an exercise to the reader. As a matter of fact, this month’s tester David proved during the testing of this problem that there is a closed form for any k that can be calculated using the same ideas presented above for **k = 1** and **k = 2**. The result for any **k** is

**(k*n) ^{n-2}(k*(n-1))^{(k-1)*n}**

The limits were intentionally kept **low** **enough** to not let the existance of closed form solution be apparent.

### SETTER’S SOLUTION

Can be found here.

### TESTER’S SOLUTION

Can be found here.