ICPC Latin America Championship · Problem D

Displaying Decimals

2025By Bruno MonteiroOriginal problem set

Statement

After the BRICS group introduced the International Common Payment Currency (ICPC), all currencies (like real, peso, dollar, etc.) are now defined by their values in ICPCs.

To help people navigate this new world order, you decided to create a website to compute conversion rates between currencies. The website handles NN currencies, numbered from 1 to NN. Each currency ii has an associated integer value AiA_i, indicating that one unit of currency ii is worth AiA_i ICPCs.

The user selects a source currency ii and then a target currency jj (possibly j=ij=i), and the website displays the conversion rate between them: the exact decimal representation of Ai/AjA_i/A_j.

To deal with repeating decimals, you display only the first period, represented by a line above it when one exists. Thus 4/4=14/4=1 requires 1 digit, 4/3=1.3‾4/3=1.\overline3 requires 2 digits, 41/4=10.2541/4=10.25 requires 4 digits, and 3/14=0.2142857‾3/14=0.2\overline{142857} requires 8 digits.

Your website has been running successfully for quite some time. As part of your performance analytics, you want to compute the expected display size of conversion-rate pages.

Given A1,A2,…,ANA_1,A_2,\ldots,A_N, compute the expected number of digits needed to display Ai/AjA_i/A_j when ii and jj are chosen independently and uniformly at random from 11 through NN.

Input

The first line contains NN (1≤N≤1051\le N\le10^5), the number of currencies. The second contains A1,A2,…,ANA_1,A_2,\ldots,A_N (1≤Ai≤1051\le A_i\le10^5), where AiA_i is the value of one unit of currency ii in ICPCs.

Output

The expected digit count can be expressed as an irreducible fraction P/QP/Q, where QQ and M=998244353M=998244353 are coprime. Let Q−1Q^{-1} be the modular inverse of QQ modulo MM. Output P⋅Q−1 mod MP\cdot Q^{-1}\bmod M.

Example

Input
3
15 36 14

Output
332748121

The digit counts for every row/column pair are:

153614
15148
36217
14331

Each has probability 1/91/9. For example, 14/36=0.38‾14/36=0.3\overline8 requires 3 digits. The expectation is 30/9=10/330/9=10/3. Since 3−1=3327481183^{-1}=332748118, the answer is 10⋅332748118 mod 998244353=33274812110\cdot332748118\bmod998244353=332748121.

Tutorial

For positive integers XX and YY, let F(X,Y)F(X,Y) be the number of digits used by the exact decimal representation of X/YX/Y, counting only the first copy of a repeating period. We need

1N2∑i=1N∑j=1NF(Ai,Aj).\frac{1}{N^2}\sum_{i=1}^{N}\sum_{j=1}^{N}F(A_i,A_j).

Split the digit count into

F(X,Y)=I(X,Y)+D(X,Y),F(X,Y)=I(X,Y)+D(X,Y),

where II is the number of digits before the decimal point and DD is the number after it.

Computing the fractional length

First reduce the fraction. If gcd⁡(X,Y)=1\gcd(X,Y)=1, multiplication by the nonzero numerator only permutes the remainders in long division, so

D(X,Y)=D(1,Y).D(X,Y)=D(1,Y).

Write

Y=2a5bq,gcd⁡(q,10)=1.Y=2^a5^bq,\qquad \gcd(q,10)=1.

Only the factors 2 and 5 contribute to the non-repeating prefix. Its length is max⁡(a,b)\max(a,b). After removing those factors, the repeating part has length equal to the multiplicative order of 10 modulo qq:

D(1,Y)=max⁡(a,b)+{0,q=1,ord⁡q(10),q>1.D(1,Y)=\max(a,b)+ \begin{cases} 0,&q=1,\\ \operatorname{ord}_q(10),&q>1. \end{cases}

The order is the smallest positive kk such that

10k≡1(modq).10^k\equiv1\pmod q.

It divides φ(q)\varphi(q). Precompute φ\varphi for every number up to M=max⁡AiM=\max A_i with a sieve. For each qq coprime to 10, begin with k=φ(q)k=\varphi(q), factor φ(q)\varphi(q), and repeatedly divide kk by a prime pp whenever

10k/p≡1(modq).10^{k/p}\equiv1\pmod q.

This computes every value D(1,Y)D(1,Y) in O(Mlog⁡2M)O(M\log^2M) time and O(M)O(M) memory.

Grouping pairs by their gcd

For a pair (X,Y)(X,Y), let G=gcd⁡(X,Y)G=\gcd(X,Y) and write X=GxX=Gx, Y=GyY=Gy. Then gcd⁡(x,y)=1\gcd(x,y)=1 and

D(X,Y)=D(x,y)=D(1,y).D(X,Y)=D(x,y)=D(1,y).

We can therefore enumerate the possible gcd GG. For each input value UU and each divisor G∣UG\mid U, insert the normalized value U/GU/G into a list LGL_G. With frequencies, insert its multiplicity rather than making repeated copies.

For example, if the values are [4,6,8,10][4,6,8,10], the nonempty lists begin as follows:

GGLGL_G
14, 6, 8, 10
22, 3, 4, 5
32
41, 2
52
61
81
101

Inside list LGL_G, a normalized numerator xx and denominator yy represent a pair whose gcd is exactly GG if and only if gcd⁡(x,y)=1\gcd(x,y)=1.

To count values coprime to yy, use Möbius inclusion–exclusion. Maintain a counter cdc_d for every divisor dd of the currently active numerators. Then

#{x:gcd⁡(x,y)=1}=∑d∣yμ(d)cd.\#\{x:\gcd(x,y)=1\}=\sum_{d\mid y}\mu(d)c_d.

Adding or removing a value xx means updating cdc_d for every divisor d∣xd\mid x. Across all gcd lists, the divisor work is O(Mlog⁡2M)O(M\log^2M).

Adding the integer part

The integer part contains at most ⌊log⁡10M⌋+1≤6\lfloor\log_{10}M\rfloor+1\le6 digits. Handle each possible digit count dd separately.

For a fixed normalized denominator yy, the numerator has d≥2d\ge2 integer digits precisely when

y⋅10d−1≤x<y⋅10d.y\cdot10^{d-1}\le x<y\cdot10^d.

For d=1d=1, also include x<yx<y, because a proper fraction is displayed with the single integer digit 0; equivalently, the range is 1≤x<10y1\le x<10y.

Within each sorted list LGL_G, these ranges move monotonically as yy increases. Use two pointers to add and remove normalized numerators from the Möbius counters. A query then gives the number of coprime numerators in the current range. Multiply that count, with the appropriate input frequencies, by

d+D(1,y),d+D(1,y),

and add it to the total.

There are only six passes over the lists, so this changes only the constant factor. The complete algorithm runs in

O(Mlog⁡2M)O(M\log^2M)

time and O(Mlog⁡M)O(M\log M) memory for the gcd lists. Finally, divide the accumulated ordered-pair sum by N2N^2 modulo 998244353998244353.

Reference implementation