Andy Chen

← All posts

Indigo Codes 2024

· 3 min · competitive programming , solutions


It’s been a while since I last posted. I had a few ideas for a post but never had the chance to write them.

Last Saturday, Indigo Codes 2024 happened.

I did not manage to take it due to a conflict, but the problems I managed to solve afterwards were quite interesting.

1. Special Mods#

Pretty standard prefix sum problem. Since N5000N \leq 5000, you can iterate over every range and see if the sum is equivalent to KmodMK \bmod M

2. Array Swapping#

Notice that the way a swap is defined means that you can only change a position to exactly one other position

Thus, you only need to check if each number ii is in either the ii-th position or the (n+i1)(n + i - 1)-th position.

If ii is in the (n+i1)(n + i - 1)-th position, add one to the answer, and swap the ii-th and (n+i1)(n + i - 1)-th position.

3. Beautiful Tree#

Greedily add edges.

4. William’s Garden 1#

Define fif_i as the number of ways modulo 109+710^9 + 7 to restore a i×2i \times 2 grid

The transition is as follows:

fi=(fi1+fi2+2j=0i2fj)mod109+7\displaystyle{f_i = (f_{i - 1} + f_{i - 2} + 2 \cdot \sum_{j = 0}^{i - 2} f_j) \bmod 10^9 + 7}

with f0=1f_0 = 1 and f1=1f_1 = 1

This is correct because at each ii, we can either add a vertical swap, 22 horizontal swaps, or a cycle of arbitrary length (counter)clockwise

Alternatively, one can notice that fif_i is the square of the ii-th fibonacci number (proof as to why on William’s Garden 2)

5. Favorite Sequences#

Define fi,jf_{i, j} as the number of lovable sequences ending on the number ii after the jj-th number is considered modulo 109+710^9 + 7 and gi,jg_{i, j} as the sum of lovable sequences ending on the number ii after the jj-th number is considered modulo 109+710^9 + 7

The transitions are as follows:

fi,j=(k=ixi+xfk,j1)mod109+7\displaystyle{f_{i, j} = (\sum_{k = i - x}^{i + x} f_{k, j - 1}) \bmod 10^9 + 7}

gi,j=(i(fi,jfi,j1)(k=ixi1gk,j1+k=i+1i+xgk,j1)+gi,j1)mod109+7\displaystyle{g_{i, j} = (i \cdot (f_{i, j} - f_{i, j - 1}) \cdot (\sum_{k = i - x}^{i - 1} g_{k, j - 1} + \sum_{k = i + 1}^{i + x} g_{k, j - 1}) + g_{i, j - 1}) \bmod 10^9 + 7}

At each aja_j, we only need to update one value of ff and gg, so the runtime is O(nx)O(nx)

6. Cifjump#

I don’t quite know how to utilize the small bound on viv_i, so I will describe a solution that works for larger viv_i

We can restate the problem as:

For each position xx, what is the minimum number of hops to move at least nn positions?

Using binary jumping, we can find the distance travelled in mm moves in O(logm)O(\log m). Naively binary searching leads to a time complexity of O(nlog2n)O(n \log^2 n), which is too slow for n106n \leq 10^6

Notice that if we are doing a lot of recomputation every time we try a value of mm. If we allow the midpoint of the binary search to split the current interval into powers of 2, then we only need to do O(1)O(1) operations per check (This is also the idea behind bitwise binary search).

7. I Like My Trees Drippy#

I haven’t implemented this yet.

8. William’s Garden 2#

I also haven’t implement this yet, but the idea is really nice. Remember how the solution to the easy version was the square of the fibonacci numbers? This is because we can imagine swaps as two domino tilings, where the first one represents where the first tiling represents the way to move a pepper and the second represents the way to move a tomato.

Now comes the problem of calculating domino tilings. One can construct a linear recurrence dp for m=2,3,4m = 2, 3, 4 and use matrix exponentiation.

9. Work Shifts#

I haven’t read or implemented this yet.

10. Casino Dice Game#

Too hard.

Code#

Code for the first 6 problems can be found in this repository