E - Integer Sequence Fair Editorial by en_translator
There are sequences that will be evaluated; i.e. “a sequence of length consisting of integers between and (inclusive).”
Thus, there are ways to “give each of the evaluated sequences a score between and .”
Therefore, this problem asks to find the remainder of divided by .
If is a multiple of , then is a multiple of , so we should print .
Hereinafter we assume that is not a multiple of .
Then, since is a prime, by Fermat’s little theorem
holds. Therefore, let and be the quotient and remainder when dividing by , respectively, then
,
so the remainder of divided by is equal to the remainder of by .
Hence, in order to find the remainder of by , it is sufficient to find the following two values:
- The remainder when dividing by , and
- the remainder when dividing by .
These can be computed fast enough by means of fast exponentiation.
Be sure to avoid overflows in the process of computation.
posted:
last update: