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

Déjà vu

Back to problem

Editorial

For a subsequence bbb, f(b)f(b)f(b) retains exactly the prime powers whose exponents are equal in every chosen element. For example, 12=22⋅312=2^2\cdot312=22⋅3 and 20=22⋅520=2^2\cdot520=22⋅5 retain only 222^222, giving f((12,20))=4f((12,20))=4f((12,20))=4.

Write d∥xd\parallel xd∥x if d∣xd\mid xd∣x and gcd⁡(d,x/d)=1\gcd(d,x/d)=1gcd(d,x/d)=1. Such a divisor is obtained by keeping or discarding each entire prime-power factor of . Let and compute . Count the frequency of each value and enumerate all subset products of its prime-power factors.

Let EdE_dEd​ count subsequences with f(b)=df(b)=df(b)=d. Initialize Ed=2cd−1E_d=2^{c_d}-1E. Process , subtracting from for every with . The answer is

∑d=1AdEd,\sum_{d=1}^{A}dE_d,d=1∑A​dEd​,

with all calculations performed modulo 998 244 353998\,244\,353998244353.

Precomputing smallest prime factors and powers of 222 gives O(N+Alog⁡A)O(N+A\log A)O(N+AlogA) time and O(N+A)O(N+A)O(N+A) space per case.

Proof

After rrr operations choosing a prime ppp, an exponent eie_iei​ becomes max⁡(ei−r,0)\max(e_i-r,0)max(. Equal exponents are best left unchanged. Unequal exponents can become equal only when all reach zero. Hence retains exactly the positive prime powers shared with identical exponents by all chosen elements.

xx
x
A=max⁡iaiA=\max_i a_iA=maxi​ai​
cd=#{i:d∥ai}c_d=\#\{i:d\parallel a_i\}cd​=#{i:d∥ai​}
d
​
=
2cd​−
1
x=A,A−1,…,1x=A,A-1,\ldots,1x=A,A−1,…,1
ExE_xEx​
EdE_dEd​
d∥xd\parallel xd∥x
d<xd<xd<x
e
i​
−
r,0)
f(b)f(b)f(b)

It follows that d∥f(b)d\parallel f(b)d∥f(b) holds exactly when d∥aid\parallel a_id∥ai​ for every chosen element. Therefore,

2cd−1=∑x: d∥xEx.2^{c_d}-1=\sum_{x:\,d\parallel x}E_x.2cd​−1=x:d∥x∑​Ex​.

Since every proper divisor is smaller than its value, processing values in decreasing order finalizes ExE_xEx​ before subtracting its contribution from smaller divisors.

Every enumerated divisor is also an ordinary divisor, so their total number is at most ∑d=1A⌊A/d⌋=O(Alog⁡A)\sum_{d=1}^{A}\lfloor A/d\rfloor=O(A\log A)∑d=1A​⌊A/d⌋=O(AlogA).