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

Sorting a Sequence

Back to problem

Editorial

Since K≤NK\leq NK≤N, the sequence XXX contains every integer from 222 through K+1K+1K+1. Therefore, the KKK-th smallest number in XXX is always K+1K+1K+1.

The required output is (K+1) mod 998 244 353(K+1)\bmod 998\ 244\ 353(K+1)mod998 244 353. Compute and output this value without constructing PPP, CCC, or .

Both the time complexity and the auxiliary space complexity are O(1)O(1)O(1).

Proof

Choose any integer mmm with 2≤m≤K+12\leq m\leq K+12≤m≤K+1. The number mmm is either prime or composite.

There are m−1m-1 integers from through . Thus, if is prime, its rank among the primes is at most . If it is composite, its rank among the composites is at most .

Solution written by GPT6

XX
X
m
−
1
222
mmm
mmm
m−1m-1m−1
m−1m-1m−1

Since m−1≤K≤Nm-1\leq K\leq Nm−1≤K≤N, in either case mmm belongs to the first NNN terms of the corresponding sequence. Therefore, XXX contains all of 2,3,…,K+12,3,\ldots,K+12,3,…,K+1.

Every element of XXX is an integer at least 222. No value is repeated within either sequence, and no number is both prime and composite, so all elements of XXX are distinct. Its smallest KKK elements are therefore exactly 2,3,…,K+12,3,\ldots,K+12,3,…,K+1, making K+1K+1K+1 the KKK-th smallest number.