DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Pi Problem

Back to problem

Editorial

Let P=998 244 353P=998\,244\,353P=998244353. If aaa has ddd decimal digits, then

rep(a,b)=a∑k=0b−110dk=a10db−110d−1.\mathrm{rep}(a,b)=a\sum_{k=0}^{b-1}10^{dk}=a\frac{10^{db}-1}{10^d-1}.rep(a,b)=ak=0∑b−1​10dk=a

For 1≤d≤101\leq d\leq101≤d≤10, 10d−110^d-110d−1 is not divisible by PPP, so we can divide using its modular inverse. Let be the geometric-series part of this expression modulo .

Let did_idi​ be the number of digits in AiA_iAi​. The product over all pairs with left endpoint iii is

AiN−i∏j=i+1NGdi(Aj).A_i^{N-i}\prod_{j=i+1}^{N}G_{d_i}(A_j).AiN−i​j=i+1∏

Scan from right to left and maintain a suffix product Sd=∏j>iGd(Aj)S_d=\prod_{j>i}G_d(A_j)Sd​=∏j>i​Gd for each digit count . First multiply the answer by . Then multiply every by . This order excludes the pair .

Precompute the inverses of 10d−110^d-110d−1. To compute every Gd(Ai)G_d(A_i)Gd​(Ai​, calculate once and then obtain by successive multiplication. Each case takes time and space.

Proof

Immediately before processing iii, SdS_dSd​ is exactly the product of Gd(Aj)G_d(A_j)Gd​( over all . Initially there are no elements to the right, so every . After multiplying the contribution of into the answer, multiplying by preserves the invariant for the next index.

In subtask 1, the repeated number can be built directly. In subtask 2, evaluate the geometric-series formula for every pair. In subtask 3, every value has one digit, so only one suffix product is needed. For the full constraints, maintain one suffix product for each of the ten possible digit counts.

Solution written by GPT5

10d−110db−1​
.
Gd(b)G_d(b)
Gd​(b)
PPP
N
​
Gdi​​
(
Aj​
)
.
​
(
Aj​
)
ddd
AiN−iSdiA_i^{N-i}S_{d_i}AiN−i​Sdi​​
SdS_dSd​
Gd(Ai)G_d(A_i)Gd​(Ai​)
(i,i)(i,i)(i,i)
)
x=10Ai mod Px=10^{A_i}\bmod Px=10Ai​modP
x1,x2,⋯ ,x10x^1,x^2,\cdots,x^{10}x1,x2,⋯,x10
O(N(10+log⁡N+log⁡max⁡Ai))O(N(10+\log N+\log\max A_i))O(N(10+logN+logmaxAi​))
O(N)O(N)O(N)
Aj​
)
j>ij>ij>i
Sd=1S_d=1Sd​=1
iii
SdS_dSd​
Gd(Ai)G_d(A_i)Gd​(Ai​)

Each pair (i,j)(i,j)(i,j) contributes one factor of AiA_iAi​ and one factor of Gdi(Aj)G_{d_i}(A_j)Gdi​​(Aj​) when its left endpoint iii is processed. It is not included at any other step. Thus, the final answer is the required product over all pairs.