DOJ logoDOJ
Problems
Recently added problemsSaved problems
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-backed certification.

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

jkrt's AND

Back to problem

Editorial

First, let us characterize all positive integers satisfying the condition.

Every one-digit positive integer 1,2,…,91,2,\ldots,91,2,…,9 satisfies the condition. Indeed, xxx and x−1x-1x−1 are different one-digit numbers, or x−1=0x-1=0x−1=0 when x=1x=1x=1, so their LCS length is 000.

Now suppose xxx has at least two digits. If the last digit of xxx is not 000, then x−1x-1x−1 is obtained by decreasing only the last digit of xxx by 11. Therefore, the prefix before the last digit remains unchanged, and this prefix is nonempty. Hence and have a common digit, so their LCS length cannot be .

Thus, any valid xxx with at least two digits must end with 000. Remove as many trailing zeros as possible and write

x=y⋅10t(t≥1),x = y \cdot 10^t \quad (t \ge 1),x=y⋅10t(t≥1),

where yyy does not end with 000.

If yyy has at least two digits, then y−1y-1y−1 is obtained by decreasing only the last digit of yyy by 111, so a nonempty prefix remains unchanged. Therefore, xxx and x−1x-1 still have a common digit. Hence must be a one-digit number.

So every valid number with at least two digits must have the form

x=d⋅10t.x = d \cdot 10^t.x=d⋅10t.

Here, 1≤d≤91 \le d \le 91≤d≤9 and t≥1t \ge 1t≥1.

If d=9d=9d=9, then the decimal representation of xxx contains digit 999, and the suffix of x−1x-1x−1 contains digit 999, so they have a common digit. Therefore, is impossible. Conversely, if , the digits of are only and , while the digits of are only and . These sets are disjoint, so the condition is satisfied.

Therefore, the valid numbers are listed as follows.

1,2,…,9,10,20,…,80,100,200,…,800,1000,2000,…1,2,\ldots,9,\quad 10,20,\ldots,80,\quad 100,200,\ldots,800,\quad 1000,2000,\ldots1,2,…,9,10,20,…,80,100,200,…,

If N≤9N \le 9N≤9, the answer is simply NNN.

If N≥10N \ge 10N≥10, then

AN=d⋅10e,A_N = d \cdot 10^e,AN​=d⋅10e,

where

d=((N−10) mod 8)+1d = ((N-10) \bmod 8) + 1d=((N−10)mod8)+1

and

e=⌊N−108⌋+1=⌊N−28⌋.e = \left\lfloor \frac{N-10}{8} \right\rfloor + 1 = \left\lfloor \frac{N-2}{8} \right\rfloor.e=⌊8N−10​⌋+1=

Since NNN can be very large, we process it as a decimal string. To compute ddd, it is enough to compute N mod 8N \bmod 8Nmod8. To compute eee, subtract 222 from the decimal string and divide it by , keeping only the quotient modulo .

Finally, by Fermat's little theorem,

10998244352≡1(mod998244353).10^{998244352} \equiv 1 \pmod {998244353}.10998244352≡1(mod998244353).

Therefore, when computing 10e(mod998244353)10^e \pmod {998244353}10e(mod998244353), it is enough to know eee modulo 998244352998244352998244352.

The time complexity is O(∣N∣+log⁡998244353)O(|N|+\log 998244353)O(∣N∣+log998244353), and the memory usage is O(∣N∣)O(|N|)O(∣N∣).

1
xxx
x−1x-1x−1
000
x−1
yyy
d=9d=9
d=9
1≤d≤81 \le d \le 81≤d≤8
xxx
ddd
000
x−1x-1x−1
d−1d-1d−1
999
800
,
1000
,
2000
,
…
⌊8N−2​⌋
.
888
998244352998244352998244352