H - Simple Division Editorial by evima
To summarize the first part of the official editorial, we want to find the remainder \(r\) when \(N = \displaystyle \sum_{i=1}^{K} c_i \times \frac{10^{l_i}-1}{9} \times 10^{\sum_{j=i+1}^{K} l_j}\) is divided by \(10007M\), and \(\lfloor r / M \rfloor\) is the answer. Here, \(r\) equals the quotient obtained by dividing “the remainder when \(9N = \displaystyle \sum_{i=1}^{K} c_i \times (10^{l_i}-1) \times 10^{\sum_{j=i+1}^{K} l_j}\) is divided by \(9 \times 10007M\)” by \(9\). Therefore, it suffices to compute powers of \(10\) modulo \(9 \times 10007M\) efficiently, which in some languages can be done with a standard library function. (Even in that case, the standard library internally performs doubling or similar operations.)
Implementation example (Python)
K, M = map(int, input().split())
mod = 9 * 10007 * M
c, l = [0] * K, [0] * K
for i in range(K):
c[i], l[i] = map(int, input().split())
ans, s = 0, 0
for i in range(K - 1, -1, -1):
ans += c[i] * (pow(10, l[i], mod) - 1) * pow(10, s, mod)
s += l[i]
print(ans % mod // 9 // M)
posted:
last update: