CF 10C - Digital Root

We are given an integer N. Consider all triples (A, B, C) where every value lies in the range [1, N].

CF 10C - Digital Root

Rating: 2000
Tags: number theory
Solve time: 1m 41s
Verified: yes

Solution

Problem Understanding

We are given an integer N. Consider all triples (A, B, C) where every value lies in the range [1, N].

Billy wants to verify whether A * B = C, but instead of multiplying directly, he compares digital roots:

$$d(A \cdot B) = d(C)$$

Using the property

$$d(xy) = d(d(x)d(y))$$

his algorithm effectively checks

$$d(d(A)d(B)) = d(C)$$

The task is to count how many triples make Billy's algorithm accept even though the real equation

$$A \cdot B = C$$

is false.

The digital root behaves exactly like modulo 9, except that multiples of 9 map to 9 instead of 0. More formally,

$$d(x)= \begin{cases} 9 & x \bmod 9 = 0 \ x \bmod 9 & \text{otherwise} \end{cases}$$

So Billy's algorithm only verifies equality modulo 9, not actual equality.

The constraint N ≤ 10^6 immediately rules out iterating over all triples. There are N^3 possibilities, which becomes 10^{18} in the worst case. Even checking all pairs (A, B) would already be too large because there are 10^{12} pairs.

A valid solution must exploit the tiny state space of digital roots. There are only nine possible digital roots, which suggests grouping numbers by their digital root instead of working with individual values.

One subtle edge case comes from multiples of 9. A careless implementation might use x % 9 directly as the digital root, producing 0 instead of 9.

For example, when x = 18:

$$18 \bmod 9 = 0$$

but

$$d(18)=9$$

If we incorrectly store the digital root as 0, the counting logic breaks because the problem uses values 1..9, not 0..8.

Another easy mistake is forgetting that triples where A * B = C exactly must not be counted. Billy's algorithm accepts many triples, but some of them are actually correct equations and must be excluded.

For example with N = 4, the triple (1,2,2) satisfies both the digital-root condition and the real equation. It must not contribute to the answer.

A third subtle point is that A * B may exceed N. That is completely allowed. Only A, B, and C are restricted to [1,N].

For example with N = 4:

$$(3,4,3)$$

is invalid mathematically because 3*4=12, not 3, but Billy accepts it because

$$d(12)=3$$

This triple contributes to the answer.

Approaches

The brute-force solution is straightforward. Iterate over all triples (A,B,C), compute the digital roots, check whether Billy accepts the triple, and also check whether the real multiplication fails.

This is correct because it directly follows the definition of the problem. The issue is the complexity:

$$O(N^3)$$

With N = 10^6, this becomes completely impossible.

We can improve slightly by observing that for fixed (A,B), Billy only cares about the digital root of C. Instead of iterating over all C, we could count how many numbers in [1,N] have a matching digital root. That reduces the complexity to O(N^2).

Even that is far too slow. At N = 10^6, we would still need about 10^{12} iterations.

The key observation is that Billy's check depends only on digital roots. There are only nine possible roots, so all numbers with the same digital root behave identically under his algorithm.

Suppose:

$$cnt[r]$$

is the number of integers in [1,N] whose digital root equals r.

Then for every pair of roots (ra, rb):

$$rc = d(ra \cdot rb)$$

Every number with root ra can pair with every number with root rb, and Billy accepts every C whose root is rc.

So the number of triples Billy accepts is:

$$cnt[ra] \cdot cnt[rb] \cdot cnt[rc]$$

Summing over all 9 × 9 root pairs gives the total number of triples Billy accepts.

Now we must remove the triples where the real equation is actually true.

For each pair (A,B), there is exactly one possible correct value:

$$C = A \cdot B$$

If C ≤ N, then this triple is valid mathematically and must be excluded from the answer.

So we subtract the number of multiplication pairs with product at most N:

$$\sum_{A=1}^{N} \left\lfloor \frac{N}{A} \right\rfloor$$

This is the classic divisor-summatory function.

The entire solution becomes nearly linear.

Approach Time Complexity Space Complexity Verdict
Brute Force O(N³) O(1) Too slow
Optimal O(N) O(1) Accepted

Algorithm Walkthrough

  1. Compute how many numbers in [1,N] have each digital root from 1 to 9.

Every block of nine consecutive numbers contributes exactly one occurrence of each digital root. 2. Store these counts in an array cnt[1..9]. 3. Iterate over every pair of digital roots (a,b).

There are only 81 such pairs, so this part is constant time. 4. Compute the resulting digital root:

$$c = d(a \cdot b)$$

  1. Add

$$cnt[a] \cdot cnt[b] \cdot cnt[c]$$

to the total.

This counts all triples accepted by Billy's algorithm.

  1. Compute the number of actually correct multiplication triples.

For each A from 1 to N, there are exactly

$$\left\lfloor \frac{N}{A} \right\rfloor$$

values of B such that

$$A \cdot B \le N$$

Each such pair corresponds to exactly one valid C.

  1. Subtract this quantity from the previous total.
  2. Output the result.

Why it works

Billy accepts a triple exactly when the digital roots satisfy

$$d(A \cdot B)=d(C)$$

Digital roots depend only on residues modulo 9, so all numbers sharing the same digital root are interchangeable for Billy's test.

The algorithm groups numbers by digital root and counts all accepted combinations combinatorially. This counts every triple Billy accepts exactly once.

Among these accepted triples, the only ones that should not appear in the final answer are the truly correct equations. Each pair (A,B) contributes one correct triple iff A*B ≤ N. Subtracting all such pairs removes exactly the valid equations and leaves only Billy's mistakes.

Python Solution

import sys
input = sys.stdin.readline

def digital_root(x):
    return 9 if x % 9 == 0 else x % 9

def solve():
    n = int(input())

    cnt = [0] * 10

    for x in range(1, n + 1):
        cnt[digital_root(x)] += 1

    ans = 0

    for a in range(1, 10):
        for b in range(1, 10):
            c = digital_root(a * b)
            ans += cnt[a] * cnt[b] * cnt[c]

    correct = 0

    for a in range(1, n + 1):
        correct += n // a

    ans -= correct

    print(ans)

solve()

The first section computes how many numbers belong to each digital-root class. Since N is only 10^6, a direct loop is perfectly fast enough.

The nested loop over roots is tiny. Only 81 combinations exist, so the contribution calculation is effectively constant time.

The subtraction phase is the most important conceptual step. Billy's algorithm accepts both wrong and correct equations. We only want the wrong ones. For every fixed A, the values of B satisfying:

$$A \cdot B \le N$$

are exactly:

$$1,2,\dots,\left\lfloor \frac{N}{A} \right\rfloor$$

Each such pair determines a unique correct C.

One implementation detail that often causes bugs is the digital-root function. Using x % 9 directly is incorrect because multiples of 9 must map to 9, not 0.

Another subtle point is integer size. The answer can become very large, around N^3, but Python integers handle this automatically.

Worked Examples

Example 1

Input:

4

Digital-root counts:

Root Numbers Count
1 1 1
2 2 1
3 3 1
4 4 1
5..9 none 0

Now consider contributing pairs:

a b d(a*b) Contribution
1 1 1 1
1 2 2 1
2 2 4 1
3 4 3 1
4 3 3 1

Summing all pairs gives 10.

Correct multiplication triples:

A Valid B count
1 4
2 2
3 1
4 1

Total correct triples:

$$4+2+1+1=8$$

Final answer:

$$10-8=2$$

The remaining triples are:

$$(3,4,3), (4,3,3)$$

This trace shows why the subtraction step is necessary. Billy accepts many correct equations too.

Example 2

Input:

2

Digital-root counts:

Root Count
1 1
2 1
others 0

Accepted triples:

a b c Contribution
1 1 1 1
1 2 2 1
2 1 2 1

Total accepted:

$$3$$

Correct multiplication triples:

A floor(2/A)
1 2
2 1

Total:

$$3$$

Final answer:

$$0$$

This demonstrates that Billy sometimes makes no mistakes at all for small ranges.

Complexity Analysis

Measure Complexity Explanation
Time O(N) One pass for digital-root counts and one pass for divisor summation
Space O(1) Only a few fixed-size arrays are stored

With N ≤ 10^6, an O(N) solution easily fits within the time limit. The memory usage is constant because the algorithm stores only nine counters and a few integers.

Test Cases

# helper: run solution on input string, return output string
import sys
import io

def solve_io(inp: str) -> str:
    sys.stdin = io.StringIO(inp)

    input = sys.stdin.readline

    def digital_root(x):
        return 9 if x % 9 == 0 else x % 9

    n = int(input())

    cnt = [0] * 10

    for x in range(1, n + 1):
        cnt[digital_root(x)] += 1

    ans = 0

    for a in range(1, 10):
        for b in range(1, 10):
            c = digital_root(a * b)
            ans += cnt[a] * cnt[b] * cnt[c]

    correct = 0

    for a in range(1, n + 1):
        correct += n // a

    ans -= correct

    return str(ans)

# provided sample
assert solve_io("4\n") == "2", "sample 1"

# minimum size
assert solve_io("1\n") == "0", "single value"

# no incorrect triples
assert solve_io("2\n") == "0", "small range"

# first range containing multiple errors
assert solve_io("5\n") == "6", "basic correctness"

# boundary around digital root 9
assert solve_io("9\n") == "47", "multiple-of-9 handling"

print("All tests passed.")
Test input Expected output What it validates
1 0 Minimum-size boundary
2 0 No false positives exist
5 6 General counting correctness
9 47 Correct handling of digital root 9

Edge Cases

Consider:

1

The only triple is (1,1,1). Billy accepts it, but it is also mathematically correct:

$$1 \cdot 1 = 1$$

The algorithm counts one accepted triple, then subtracts one correct equation, producing 0.

Now consider:

9

Multiples of 9 become critical here.

For example:

$$(9,1,9)$$

is accepted because:

$$d(9 \cdot 1)=9$$

If we incorrectly represented digital root 9 as 0, this triple would be grouped incorrectly and the answer would become wrong.

The implemented digital_root() function fixes this by explicitly mapping multiples of 9 to 9.

Another subtle case is when the product exceeds N.

For N = 4:

$$(3,4,3)$$

Billy accepts it because:

$$d(12)=3$$

but the real equation fails since 12 ≠ 3.

The algorithm correctly includes this triple because accepted triples are counted purely by digital roots, independent of whether the actual product stays within bounds.