Editorial for COCI '06 Contest 6 #5 V
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.
Submitting an official solution before solving the problem yourself is a bannable offence.
If
If
We will construct the function
To calculate the values of this function, first observe that, for any
- If all numbers are between
and , then the actual prefix doesn't matter when forming the rest of the number, just its remainder when divided by . The limited range of the remainders reduces the state space and we can memoize the values of the function based on the remainder. - If all numbers are less than
or greater than , then the value is zero. - If some (but not all) numbers are between
and , then the value of the function doesn't depend only on the remainder of dividing the prefix by , so it isn't possible to memoize the value of the function on the remainder. Luckily, there are few such cases (proportional to the length of the number).
Comments