Editorial for LCC/Moose '19 Contest 2 S4 - Autobattler
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Let be the total cost of a unit with size and tier .
For 20% of the points, you should notice that
but so .
If you store all the values of in an array as you go along, you can compute this in time.
For the rest of the points, you should notice that is just .
There are a few interpretations for why this is the case, including
- The formula is the same as the formula for Pascal's Triangle, rotated by 45 degrees
- This is equivalent to a combinatorics problem that can be solved using Stars and Bars
- The "Hockey Stick" Identity
Of course, during contest it's enough to just write down a few values of and find that it matches Pascal's Triangle by inspection, but for fun, you can try to prove this formula on you own.
To compute modulo in general, you can use the formula .
Computing modulo is easy; just loop and multiply, making sure to take the modulo at each step to prevent overflow.
However, dividing by a number and getting the result modulo is harder.
Let be an integer. We say that is a modular multiplicative inverse of if , modulo . Note that for any number divisible by , .
It turns out that if is prime, the modular multiplicative inverse of a number is unique and can be found by the formula , modulo . This is a direct result of Fermat's Little Theorem.
Calculating modulo is possible in time using fast exponentiation.
Comments