Editorial for Ones


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Sucram314

This problem is solved easiest by printing out the product for small values of A and B and looking for a pattern. In this editorial I will attempt to provide a solution which derives the formula without proof by AC™.

Initial Observations

Notice that O_x = 10^0 + 10^1 + \dots + 10^{x-1}. Thus, multiplying a number k by O_x has the effect of creating x copies of k, the ith copy having i additional trailing zeroes (effectively shifting the number i digits left), and then summing up all of the copies.

For example, O_3 \times 14 = 14 + 140 + 1400 = 1554.

So, what happens when we multiply O_A and O_B? Without loss of generality, assume that A \ge B. We now want to calculate the number of ones in the decimal representation of:

\displaystyle 
\left.
\underline{\begin{matrix}
& & & & 1 & \dots & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & + \\
& & & 1 & \dots & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & & + \\
& & 1 & \dots & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & & & + \\
& 1 & \dots & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & & & & + \\
1 & \dots & 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 & & & & & + \\
& & & & \vdots & & & & & & & & & & \vdots \\
\end{matrix}}
\right\} B\text{ rows} \\

\displaystyle  \begin{matrix} \phantom{1} & \phantom{\dots} & \phantom{1} & \phantom{1} & \phantom{1} & \phantom{1} & \phantom{1} & \phantom{1} & \phantom{1} & \dots & 5 & 4 & 3 & 2 & 1 & \phantom{+} \end{matrix} \phantom{\}  B\text{ rows}}

Where each 1 \dots 11111111 is equal to O_A, containing A ones. Trailing zeroes are ommitted from the above diagram.

Adding the columns without carry-overs, we get something like

\displaystyle \overbrace{1 \quad 2 \quad 3 \quad \dots \quad B-2 \quad B-1}^{\text{ascending sequence}} \quad \overbrace{B \quad B \quad \dots \quad B}^{(A-B+1) \text{ } B \text{'s}} \quad \overbrace{B-1 \quad B-2 \quad \dots \quad 3 \quad 2 \quad 1}^{\text{descending sequence}}

Subtask 1

Using the above observations, a loop which simply calculates each digit by from right to left and handles carry-overs is sufficient to count the number of ones in the product. This is a less general application of the technique used in A Plus B (Hard).

Time Complexity: \mathcal{O}(A+B)

Subtask 2

Since 1 \le A,B \le 10^9, we need a way to calculate the answer in constant time.

just give me the answer bro It turns out that, assuming A \ge B, the exact formula for the number of 1's in O_A \times O_B is: \displaystyle 
    \begin{cases}
    1 + \left\lfloor\dfrac{B + 7}{9}\right\rfloor + A - B & \text{if } B \equiv 1 \pmod{9} \\
    1 + \left\lfloor\dfrac{B + 7}{9}\right\rfloor & \text{otherwise}
    \end{cases}
    If A < B, just swap them and apply the same formula. In python terms, this formula could be written as 1 + (B + 7) // 9 + (B % 9 == 1)*(A - B)
Time Complexity: \mathcal{O}(1)

A somewhat formal proof will be provided below, by doing casework on each piece of the sum without carry-overs above. A lot of the math shown below can be skipped by simply recognizing the pattern with intuition. We will continue to assume without loss of generality that A \ge B.

Piece 1: Least Significant Digits (Rightmost Descending Sequence)

In order to analyze the rightmost descending sequence, it is useful to see what happens if the rightmost descending sequence is extended infinitely to the left. If we can understand the pattern when the rightmost descending sequence is extended infinitely to the left, we can take the first B-1 digits from the left to count how many 1's are present in O_A \times O_B.

Consider O_\infty \times O_\infty, where O_\infty is the 10-adic number consisting of infinite 1's. We can find the equivalent value of O_\infty in our regular number system as follows:

\displaystyle O_\infty = \overbrace{\dots111111}^{\infty\text{ }1\text{'s}}

\displaystyle 9O_\infty = \dots999999

\displaystyle 9O_\infty + 1 = (\dots999999) + 1

Intuitively, adding the 1 would cause a chain of carry-overs, resulting in 0 on the right hand side. Thus,

\displaystyle 9O_\infty + 1 = 0

\displaystyle O_\infty = -\frac{1}{9}

So, if we want to find the 10-adic representation of O_\infty \times O_\infty, we can convert \left(-\dfrac{1}{9}\right) \times \left(-\dfrac{1}{9}\right) = \dfrac{1}{81} back into its 10-adic representation.

To do this, we use long division, each time expressing \dfrac{x}{81} in the form 10q + r, where r is an integer and q is another fraction with denominator 81:

Click for long division jumpscare! \displaystyle \frac{1}{81} = 10\left(-\frac{8}{81}\right) + 1 \displaystyle -\frac{8}{81} = 10\left(-\frac{17}{81}\right) + 2 \displaystyle -\frac{17}{81} = 10\left(-\frac{26}{81}\right) + 3 \displaystyle -\frac{26}{81} = 10\left(-\frac{35}{81}\right) + 4 \displaystyle -\frac{35}{81} = 10\left(-\frac{44}{81}\right) + 5 \displaystyle -\frac{44}{81} = 10\left(-\frac{53}{81}\right) + 6 \displaystyle -\frac{53}{81} = 10\left(-\frac{62}{81}\right) + 7 \displaystyle -\frac{62}{81} = 10\left(-\frac{71}{81}\right) + 8 \displaystyle -\frac{71}{81} = 10\left(-\frac{80}{81}\right) + 9 \displaystyle -\frac{80}{81} = 10\left(-\frac{8}{81}\right) + 0 We have reached -\dfrac{8}{81}, which we have already expressed earlier. So, we can now write: \displaystyle -\frac{8}{81} = 10\left(-\frac{17}{81}\right) + 2 \displaystyle -\frac{8}{81} = 10^2\left(-\frac{26}{81}\right) + 32 \displaystyle -\frac{8}{81} = 10^3\left(-\frac{35}{81}\right) + 432 \displaystyle -\frac{8}{81} = 10^4\left(-\frac{44}{81}\right) + 5432 \displaystyle -\frac{8}{81} = 10^5\left(-\frac{53}{81}\right) + 65432 \displaystyle -\frac{8}{81} = 10^6\left(-\frac{62}{81}\right) + 765432 \displaystyle -\frac{8}{81} = 10^7\left(-\frac{71}{81}\right) + 8765432 \displaystyle -\frac{8}{81} = 10^8\left(-\frac{80}{81}\right) + 98765432 \displaystyle -\frac{8}{81} = 10^9\left(-\frac{8}{81}\right) + 98765432 \displaystyle -\frac{8}{81} = \dots098765432098765432098765432 \displaystyle -\frac{8}{81} = \overline{098765432} So, \displaystyle \frac{1}{81} = 10\left(-\frac{8}{81}\right) + 1 \displaystyle \frac{1}{81} = 10\left(\overline{098765432}\right) + 1 \displaystyle \frac{1}{81} = \overline{098765432}1

You will find that:

\displaystyle O_\infty \times O_\infty = \frac{1}{81} = \overline{098765432}1 = ...0987654320987654320987654321

Thus, there will only be a singular 1 (the units digit) in the descending sequence piece of O_A \times O_B, as it will be a suffix of this 10-adic number.

Piece 2: Most Significant Digits (Leftmost Ascending Sequence)

Similarly, we can analyze the most significant digits by considering the product of infinite 1's past the decimal point:

\displaystyle 0.1111111111... = 0.\overline{1} = \frac{1}{9}

The desired product is:

\displaystyle \frac{1}{9} \times \frac{1}{9} = \frac{1}{81}

\dfrac{1}{81} shows up once again!

I will not go through the process of determining the decimal expansion of \dfrac{1}{81}. It's just regular long division. You will find that \dfrac{1}{81} = 0.\overline{012345679}. Note the lack of 8's.

The most significant digits of O_A \times O_B will be a prefix of this sequence of digits.

A 1 appears every 9 digits. We know for a fact that the most significant digit in O_A \times O_B will be a 1, so every 9^{th} digit after that will also be a 1. Since the descending sequence segment has length B - 1, there will be \left\lceil\dfrac{B-1}{9}\right\rceil = \left\lfloor\dfrac{B+7}{9}\right\rfloor ones in this piece.

Piece 3: Somewhat??? Significant Digits (Middle Sequence of repeated B's)

We can think about the middle sequence of repeated B's as one of the infinite descending sequences subtracted from a another infinite descending sequence multiplied by 10^{B}.

To understand what I mean by this, the sum without carry overs in the infinite descending sequence looked like this:

\displaystyle \dots \quad B + 3 \quad B + 2 \quad B + 1 \quad B \quad B - 1 \quad \dots \quad 5 \quad 4 \quad 3 \quad 2 \quad 1

If we subtract this sequence from itself shifted B digits to the left, we get something like this:

\displaystyle 
\begin{matrix}
\phantom{-} & & \dots & B + 4 & B + 3 & B + 2 & B + 1 & B & B - 1 & \dots & 5 & 4 & 3 & 2 & 1 \\
-& & \dots & 4 & 3 & 2 & 1 \\ \\
=& & \dots & B & B & B & B & B & B - 1 & \dots & 5 & 4 & 3 & 2 & 1
\end{matrix}

which is exactly the desired sequnece.

Thus, we can use our previous knowledge that O_\infty \times O_\infty = \dfrac{1}{81} = \overline{098765432}1 = ...0987654320987654320987654321.

We wish to find the digits of O_\infty \times O_\infty - O_\infty \times O_\infty \times 10^B, which will have a different pattern depending on B \pmod 9. The vertical pipe represents the split between the descending sequence and the repeated B sequence.

\displaystyle 
\begin{cases}
\begin{matrix} \dots & 999999999 & | & 876543298765432 & \dots 98765432987654321 \end{matrix} & ~B \equiv 0 \pmod 9~ \\ \\
\begin{matrix} \dots & 111111110 & | & 9876543298765432 & \dots 98765432987654321 \end{matrix} & ~B \equiv 1 \pmod 9~ \\ \\
\begin{matrix} \dots & 222222222 & | & 09876543298765432 & \dots 98765432987654321 \end{matrix} & ~B \equiv 2 \pmod 9~ \\ \\
\begin{matrix} \dots & 333333333 & | & 209876543298765432 & \dots 98765432987654321 \end{matrix} & ~B \equiv 3 \pmod 9~ \\ \\
\begin{matrix} \dots & 444444444 & | & 3109876543298765432 & \dots 98765432987654321 \end{matrix} & ~B \equiv 4 \pmod 9~ \\ \\
\begin{matrix} \dots & 555555555 & | & 42109876543298765432 & \dots 98765432987654321 \end{matrix} & ~B \equiv 5 \pmod 9~ \\ \\
\begin{matrix} \dots & 666666666 & | & 532109876543298765432 & \dots 98765432987654321 \end{matrix} & ~B \equiv 6 \pmod 9~ \\ \\
\begin{matrix} \dots & 777777777 & | & 6432109876543298765432 & \dots 98765432987654321 \end{matrix} & ~B \equiv 7 \pmod 9~ \\ \\
\begin{matrix} \dots & 888888888 & | & 75432109876543298765432 & \dots 98765432987654321 \end{matrix} & ~B \equiv 8 \pmod 9~ \\ \\
\end{cases}

Notice that the digit 1 is only produced when B \equiv 1 \pmod 9 and an extra 0 will take up once space. Thus, if B \equiv 0 \pmod 9, the middle sequence will produce (A - B + 1) - 1 = A - B ones. Otherwise, there will be no ones in the middle sequence.

FINALLY!!!

Adding up all three pieces, we get:

\displaystyle 
\begin{cases}
1 + \left\lfloor\dfrac{B + 7}{9}\right\rfloor + A - B & \text{if } B \equiv 1 \pmod{9} \\
1 + \left\lfloor\dfrac{B + 7}{9}\right\rfloor & \text{otherwise}
\end{cases}

assuming A \ge B.

Time Complexity: \mathcal{O}(1)


Comments

There are no comments at the moment.