Editorial for LCC '24 Contest 1 S5 - Exponential Sweets
Submitting an official solution before solving the problem yourself is a bannable offence.
For simplicity, let the array be
After one second:
Two seconds:
Three seconds:
It's easy to recognize these coefficients as diagonals in Pascal's Triangle, which are related to Combinations.
To understand why this is the case, consider the grid where each row is the array at time :
How much will contribute to the sum in ? Each path of only right and down moves between and represents one "propagation" of to from repeated prefix-summing. It's a bit abstract, but moving right is choosing to continue the propagation along the row in a specific frame of time, while moving down is choosing to move one second forward.
So, each term 's contribution to the final sum is .
Now, how do we calculate this mod ?
Recall that
The multiplication is quite straightforward, and the factorial can be precomputed. However, to perform the modular division, say, to divide by , you have to multiply by the modular inverse of in mod .
One way to find this modular inverse is using Fermat's Little Theorem, which states that for primes . By extension, . That means to divide by in mod , it is equivalent to multiplying by . To calculate this power, the following binary exponentiation method can be used:
long long fastPow(long long base, long long expo, long long mdl){
long long ret = 1;
while (expo > 0){
if (expo&1) ret = (ret*base) % mdl;
base = (base*base) % mdl;
expo >>= 1;
}
return ret;
}
Comments