#P12178. Repeating Decimal Rounding Puzzle

    ID: 14281 Type: Default 1000ms 256MiB

Repeating Decimal Rounding Puzzle

Repeating Decimal Rounding Puzzle

We are given a pure repeating decimal with period n, which can be written as \(a = 0.\overline{a_1a_2\dots a_n}\). Due to precision problems, only n numbers \(b_1, b_2, \dots, b_n\) are provided. Here, for each \(i\) the value \(b_i\) is defined as the \(i\)th digit (after the decimal point) obtained by rounding \(a\) to \(i\) decimal places. Formally, using the rounding function \(\mathrm{round}\) (with the rule \(\mathrm{round}(0.5)=1\) and \(\mathrm{round}(0.49999)=0\), i.e. round half up), we have:

[ b_i = \Bigl(\mathrm{round}(a \times 10^i)\Bigr) \bmod 10. ]

Your task is to find all possible values of \(a\) (each of which is a pure repeating decimal with period n) that are consistent with the given \(b_i\)'s. Let each such \(a\) be expressed in the form \(a = \frac{d}{10^n-1}\) (where \(d\) is an integer with possible leading zeros). Compute the sum of all possible \(a\) values and then output the product of this sum with \(10^n-1\). In other words, if the valid values are \(a_1, a_2, \dots, a_k\) and each \(a_j = \frac{d_j}{10^n-1}\), then you should output:

[ \left(\sum_{j=1}^k a_j\right) \times (10^n-1)=\sum_{j=1}^k d_j. ]

Note: The rounding for each \(i\) is determined by considering the value \(a \times 10^i\). Write \(a \times 10^i\) in its exact value as \(\frac{d\times 10^i}{10^n-1}\). Let the quotient and remainder when dividing \(d\times 10^i\) by \(10^n-1\) be \(q\) and \(r\) respectively. Then \(\mathrm{round}(a \times 10^i)=q+\begin{cases}1,&\text{if }2\,r\geq(10^n-1),\\0,&\text{otherwise.}\end{cases}\)

inputFormat

The first line contains an integer \(n\) indicating the length of the repeating cycle. The second line contains \(n\) space-separated digits \(b_1, b_2, \dots, b_n\).

outputFormat

Output a single integer, which is the sum of all possible \(d\) (where \(a=\frac{d}{10^n-1}\)) corresponding to those \(a\) that satisfy the rounding conditions, i.e. the value of \(\left(\sum a_j\right)\times (10^n-1)=\sum d_j\).

sample

1
6
5