Official

E - Integer Sequence Fair Editorial by en_translator


There are KNK^N sequences that will be evaluated; i.e. “a sequence of length NN consisting of integers between 11 and KK (inclusive).”
Thus, there are M(KN)M^{(K^N)} ways to “give each of the evaluated sequences a score between 11 and MM.”
Therefore, this problem asks to find the remainder of M(KN)M^{(K^N)} divided by P:=998244353P := 998244353.

If MM is a multiple of PP, then M(KN)M^{(K^N)} is a multiple of PP, so we should print 00.
Hereinafter we assume that MM is not a multiple of PP.

Then, since PP is a prime, by Fermat’s little theorem

MP11(modP)M^{P-1} \equiv 1 \pmod{P}

holds. Therefore, let qq and rr be the quotient and remainder when dividing KNK^N by (P1)(P-1), respectively, then

M(KN)=M(q(P1)+r)=(MP1)q×Mr1q×Mr=Mr(modP)M^{(K^N)} = M^{(q(P-1)+r)} = (M^{P-1})^q \times M^r \equiv 1^q \times M^r = M^r \pmod{P},

so the remainder of M(KN)M^{(K^N)} divided by PP is equal to the remainder of MrM^r by PP.
Hence, in order to find the remainder of M(KN)M^{(K^N)} by PP, it is sufficient to find the following two values:

  • The remainder rr when dividing KNK^N by (P1)(P-1), and
  • the remainder when dividing MrM^r by PP.

These can be computed fast enough by means of fast exponentiation.
Be sure to avoid overflows in the process of computation.

posted:
last update:



2025-04-08 (Tue)
16:12:44 +00:00