Official

A - Simple Math 2 Editorial by evima


We can compute the formula with, for example, the code below (Python), but it will result in TLE or MLE.

print((10 ** n) // m % m)

This is because \(10^{N}\) (10 ** n) is too large. Generally speaking, the amount of memory needed to handle large integers (without errors) is proportional to the number of digits.

Here in AtCoder, we often consider very large numbers \(\bmod 998244353\). Can we use some \(\bmod\) this time as well, instead of handling \(10^N\) itself?

It turns out we can use \(10^N \bmod M^2\) instead of \(10^N\). We can show that subtracting a multiple of \(M^2\) from \(10^N\) does not affect the answer, as follows:

\[\lfloor \frac{10^N - kM^2}{M} \rfloor \equiv \lfloor \frac{10^N}{M} - kM \rfloor \equiv \lfloor \frac{10^N}{M} \rfloor - kM \equiv \lfloor \frac{10^N}{M} \rfloor \pmod M (k \in \mathbb{Z})\]

Thus, in the end, the following code gets AC.

n, m = map(int, input().split())
print(pow(10, n, m * m) // m % m)

posted:
last update: