The June challenge is over. I am very happy, because I finished 19th overall, which is my personal best:

While editorials for all problems are available on Codechef forum, almost every solution to the tie breaker problem is unique, so I want to share mine, because it is completely different from the one posted in the official editorial.

## Problem statement

The full problem statement is here. You are given a matrix with rows and colums. In addition, you are given an integer , where . Your task is to fill the matrix with letters from 4-letters alphabet in such a way, that the number of different submatrices of matrix is as close to as possible - the less the difference is the greater score you have.

## Solution

I used the concept of 2 dimensional variant of polynomial hashing in order to count the number of different submatrices in time . The concept allows to compare any two submatrices in time.

Lets make some observations:

- It is impossible to create a matrix with less than different submatrices, because every 2 submatrices of different size are different. It follows that,if , the best you can do is to fill the matrix with only one letter, lets cay .
- It is impossible to create a matrix with more than different submatrices, because this is the number of all possible submatrices. It follows that if , the best you can do is to return a matrix with rows and colums with the maximal number of different submatrices.

My approach is to precompute for every of 225 pairs the matrix with the maximum number of different submatrices. I did that by repeatedly exchanging some random letter in a matrix and if the resulting matrix has a greater number of different submatrices than it had before, then it becomes the resulting matrix. Of course it might not return the best matrix, but if you repeat the exchange many times it should be at least quite close to the optimal matrix. It is worth to mention, that such a a process can stuck in a local maximum, so it is better to choose the result from many different runs. - Since the maximum allowed source file limit on Codechef is 50kb, you can store a lot precomputed information in it. Let . Based on two above cases, we know that . For every pair of , I assigned to a matrix of size with exactly different submatrices, where is equally distributed over . For example, let , then and we chose to memorize matrices which differ in number of different submatrices by 500, then we compute for . It helps us to chose the initial matrix for the beginning of the on-line computation. For example, if , and we computed as above, we select as the initial matrix. I computed using the same random method as in Step 2.
- Lets fill the matrix with the letter only and consider, how many different submatrices we can generate by placing letter in the -th row and -th column. If we do that, then every submatrix with the upper left corner in where and and the lower right corner in , where and is unique, because any other matrix of the same size has either no letter or has letter at different relative position. It follows that using that method, we can create a matrix with up to about different submatrices. The natural extension is to insert letters and at some positions different than in our matrix, which helps us to create a matrix with up to about different submatrices.

We are ready now for the on-line computation. We choose as the initial matrix the one, which can be generated in any of the above steps with the closest number of different submatrices to .

My online computation consists of only one technique. For any pair of , I set the number of computation steps to for the largest possible which doesn't exceed the time limit, in which I try to exchange some random elements in the matrix and hope to generate a better one.

At the beginning, I count the number of different submatrices in my initial matrix. Then, for every computations step, I exchange exactly random elements, again count the number of different submatrices and if it generated a better matrix, I store it as a current matrix.

## Conclusion

While the obvious method is to try to exchange on-line some elements in order to achieve a better solution, the crucial step is to set up the initial matrix in the best possible way. I am pretty satisfied with my solution. It results in 20th best score in that problem and gave me 19th place in the contest.