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 currencies, numbered from 1 to . Each currency has an associated integer value , indicating that one unit of currency is worth ICPCs.
The user selects a source currency and then a target currency (possibly ), and the website displays the conversion rate between them: the exact decimal representation of .
To deal with repeating decimals, you display only the first period, represented by a line above it when one exists. Thus requires 1 digit, requires 2 digits, requires 4 digits, and 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 , compute the expected number of digits needed to display when and are chosen independently and uniformly at random from through .
Input
The first line contains (), the number of currencies. The second contains (), where is the value of one unit of currency in ICPCs.
Output
The expected digit count can be expressed as an irreducible fraction , where and are coprime. Let be the modular inverse of modulo . Output .
Example
Input
3
15 36 14
Output
332748121
The digit counts for every row/column pair are:
| 15 | 36 | 14 | |
|---|---|---|---|
| 15 | 1 | 4 | 8 |
| 36 | 2 | 1 | 7 |
| 14 | 3 | 3 | 1 |
Each has probability . For example, requires 3 digits. The expectation is . Since , the answer is .
Tutorial
For positive integers and , let be the number of digits used by the exact decimal representation of , counting only the first copy of a repeating period. We need
Split the digit count into
where is the number of digits before the decimal point and is the number after it.
Computing the fractional length
First reduce the fraction. If , multiplication by the nonzero numerator only permutes the remainders in long division, so
Write
Only the factors 2 and 5 contribute to the non-repeating prefix. Its length is . After removing those factors, the repeating part has length equal to the multiplicative order of 10 modulo :
The order is the smallest positive such that
It divides . Precompute for every number up to with a sieve. For each coprime to 10, begin with , factor , and repeatedly divide by a prime whenever
This computes every value in time and memory.
Grouping pairs by their gcd
For a pair , let and write , . Then and
We can therefore enumerate the possible gcd . For each input value and each divisor , insert the normalized value into a list . With frequencies, insert its multiplicity rather than making repeated copies.
For example, if the values are , the nonempty lists begin as follows:
| 1 | 4, 6, 8, 10 |
| 2 | 2, 3, 4, 5 |
| 3 | 2 |
| 4 | 1, 2 |
| 5 | 2 |
| 6 | 1 |
| 8 | 1 |
| 10 | 1 |
Inside list , a normalized numerator and denominator represent a pair whose gcd is exactly if and only if .
To count values coprime to , use Möbius inclusion–exclusion. Maintain a counter for every divisor of the currently active numerators. Then
Adding or removing a value means updating for every divisor . Across all gcd lists, the divisor work is .
Adding the integer part
The integer part contains at most digits. Handle each possible digit count separately.
For a fixed normalized denominator , the numerator has integer digits precisely when
For , also include , because a proper fraction is displayed with the single integer digit 0; equivalently, the range is .
Within each sorted list , these ranges move monotonically as 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
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
time and memory for the gcd lists. Finally, divide the accumulated ordered-pair sum by modulo .