Editorial for LCC '24 Contest 3 S3 - Space Solitaire


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Colin

Subtask 1

We can solve this by brute force; Each turn there are at most 2 \cdot 7 + 6 = 20 valid moves you can make, and with 1 \leq N \leq 2 turns there are at most 20^2 = 400 possibilities.

Time complexity: O(20^N)

Subtask 2

20^{1000} \approx 1.07 \cdot 10^{1301}. In no universe are we brute forcing this one.

However, note that at any given turn, there are 7 cards, each with 3 possible states. This means there are a total of 3^7 = 2187 configurations every turn. We can thus do dynamic programming; for each turn, keep track of how many possibilities there are for all 2187 configurations.

Indexing a DP array by a state and iterating over every possible state could be difficult. One way of doing this is to number the suits 0, 1, 2 and interpret each state as a base-3 number. Finding a way to extract and/or modify a digit in this number is left as an exercise to the reader.

Time complexity: O(2187 \cdot N) = O(N)

(P.S.: If you are tasked with finding an answer "modulo <large prime>", usually 10^9 + 7, it's most likely a dynamic programming problem!)


Comments

There are no comments at the moment.