All Euler problems
Project Euler

Inverse Digit Sum

Define s(n) as the smallest number that has a digit sum of n. For example, s(10) = 19. Let S(k) = sum_(n=1)^k s(n). We are given that S(20) = 1074. The Fibonacci sequence is defined as f_0 = 0, f_1...

Source sync Apr 19, 2026
Problem #0684
Level Level 08
Solved By 3,174
Languages C++, Python
Answer 922058210
Length 246 words
modular_arithmeticsequencebrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Define \(s(n)\) to be the smallest number that has a digit sum of \(n\). For example \(s(10) = 19\).

Let \(\displaystyle S(k) = \sum _{n=1}^k s(n)\). You are given \(S(20) = 1074\).

Further let \(f_i\) be the Fibonacci sequence defined by \(f_0=0, f_1=1\) and \(f_i=f_{i-2}+f_{i-1}\) for all \(i \ge 2\).

Find \(\displaystyle \sum _{i=2}^{90} S(f_i)\). Give your answer modulo \(1\,000\,000\,007\).

Problem 684: Inverse Digit Sum

Mathematical Analysis

Finding s(n): The Smallest Number with Digit Sum n

For a given digit sum nn, the smallest number is constructed by maximizing the use of 9s (which pack the most digit sum per digit) and placing the remainder in the leading digit.

Write n=9q+rn = 9q + r where 0r80 \le r \le 8:

  • If r=0r = 0: s(n)=999qs(n) = \underbrace{99\ldots9}_{q} (a repdigit of qq nines)
  • If r>0r > 0: s(n)=r999qs(n) = r \underbrace{99\ldots9}_{q} (the digit rr followed by qq nines)

In both cases: s(n)=(r+1)10q1s(n) = (r+1) \cdot 10^q - 1 where r=((n1)mod9)+1r = ((n-1) \bmod 9) + 1 when n>0n > 0, but more cleanly:

Let q=n/9q = \lfloor n/9 \rfloor and r=nmod9r = n \bmod 9. Then:

  • If r=0r = 0: s(n)=10q1s(n) = 10^q - 1
  • If r>0r > 0: s(n)=r10q+(10q1)=(r+1)10q1s(n) = r \cdot 10^q + (10^q - 1) = (r+1) \cdot 10^q - 1

Uniformly: s(n)=(r+1)10q1s(n) = (r' + 1) \cdot 10^q - 1 where we define r=nmod9r' = n \bmod 9 if nmod90n \bmod 9 \ne 0, else handle the r=0r = 0 case by writing n=9(q1)+9n = 9(q-1) + 9.

Computing S(k)

S(k)=n=1ks(n)S(k) = \sum_{n=1}^{k} s(n)

Group terms by blocks of 9. Let k=9Q+Rk = 9Q + R with 0R80 \le R \le 8.

For a complete block from n=9m+1n = 9m+1 to n=9m+9n = 9m+9 (i.e., q=mq = m for n=9m+1,,9m+8n = 9m+1, \ldots, 9m+8, and q=m+1q = m+1 for n=9(m+1)n = 9(m+1)):

n=9m+19(m+1)s(n)=r=18[(r+1)10m1]+(10m+11)\sum_{n=9m+1}^{9(m+1)} s(n) = \sum_{r=1}^{8} \left[(r+1) \cdot 10^m - 1\right] + (10^{m+1} - 1) =10mr=18(r+1)+10m+19=10m44+10m+19=5410m9= 10^m \sum_{r=1}^{8}(r+1) + 10^{m+1} - 9 = 10^m \cdot 44 + 10^{m+1} - 9 = 54 \cdot 10^m - 9

Therefore:

m=0Q1(5410m9)=5410Q199Q=6(10Q1)9Q\sum_{m=0}^{Q-1} (54 \cdot 10^m - 9) = 54 \cdot \frac{10^Q - 1}{9} - 9Q = 6(10^Q - 1) - 9Q

For the remaining RR terms (n=9Q+1n = 9Q+1 to n=9Q+Rn = 9Q+R):

r=1R[(r+1)10Q1]=10Qr=1R(r+1)R=10Q(R+2)(R+1)210QR\sum_{r=1}^{R} \left[(r+1) \cdot 10^Q - 1\right] = 10^Q \sum_{r=1}^{R}(r+1) - R = 10^Q \cdot \frac{(R+2)(R+1)}{2} - 10^Q - R

Wait, r=1R(r+1)=j=2R+1j=(R+1)(R+2)21\sum_{r=1}^{R}(r+1) = \sum_{j=2}^{R+1} j = \frac{(R+1)(R+2)}{2} - 1.

So the remainder sum is:

10Q((R+1)(R+2)21)R10^Q \left(\frac{(R+1)(R+2)}{2} - 1\right) - R

Combining:

S(k)=6(10Q1)9Q+10Q((R+1)(R+2)21)RS(k) = 6(10^Q - 1) - 9Q + 10^Q \left(\frac{(R+1)(R+2)}{2} - 1\right) - R

where Q=k/9Q = \lfloor k/9 \rfloor and R=kmod9R = k \bmod 9.

Simplifying:

S(k)=610Q69Q+10Q(R+1)(R+2)210QRS(k) = 6 \cdot 10^Q - 6 - 9Q + 10^Q \cdot \frac{(R+1)(R+2)}{2} - 10^Q - R =10Q(5+(R+1)(R+2)2)9QR6= 10^Q \left(5 + \frac{(R+1)(R+2)}{2}\right) - 9Q - R - 6

Editorial

We iterate over each Fibonacci number, compute S(fi)S(f_i) using the closed-form formula with modular exponentiation. Finally, sum all results modulo 109+710^9 + 7.

Pseudocode

Precompute Fibonacci numbers $f_2, f_3, \ldots, f_{90}$
For each Fibonacci number, compute $S(f_i)$ using the closed-form formula with modular exponentiation
Sum all results modulo $10^9 + 7$

Correctness

Theorem. The method described above computes exactly the quantity requested in the problem statement.

Proof. The preceding analysis identifies the admissible objects and derives the formula, recurrence, or exhaustive search carried out by the algorithm. The computation evaluates exactly that specification, so every valid contribution is included once and no invalid contribution is counted. Therefore the returned value is the required answer. \square

Complexity Analysis

  • Computing each S(fi)S(f_i): O(logfi)O(\log f_i) for modular exponentiation, where f902.88×1018f_{90} \approx 2.88 \times 10^{18}.
  • Total: O(89log(f90))=O(8960)=O(5340)O(89 \cdot \log(f_{90})) = O(89 \cdot 60) = O(5340).

Answer

922058210\boxed{922058210}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_684/solution.cpp
#include <bits/stdc++.h>
using namespace std;

const long long MOD = 1000000007;

long long power(long long base, long long exp, long long mod) {
    long long result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

long long mod_inv(long long a, long long mod) {
    return power(a, mod - 2, mod);
}

// S(k) = 10^Q * (5 + (R+1)(R+2)/2) - 9Q - R - 6
// where Q = k/9, R = k%9
long long S(long long k) {
    if (k <= 0) return 0;
    long long Q = k / 9;
    long long R = k % 9;

    long long p10 = power(10, Q, MOD);

    // coefficient: 5 + (R+1)(R+2)/2
    long long coeff = (5 + (R + 1) * (R + 2) / 2) % MOD;

    long long result = p10 % MOD * coeff % MOD;
    result = (result - 9 % MOD * (Q % MOD) % MOD + MOD) % MOD;
    result = (result - R % MOD + MOD) % MOD;
    result = (result - 6 + MOD) % MOD;

    return result;
}

int main() {
    // Compute Fibonacci numbers
    // f_90 fits in unsigned 64-bit? f_90 ~ 2.88e18, yes
    vector<long long> fib(91);
    fib[0] = 0; fib[1] = 1;
    for (int i = 2; i <= 90; i++) {
        fib[i] = fib[i-1] + fib[i-2];
    }

    long long ans = 0;
    for (int i = 2; i <= 90; i++) {
        ans = (ans + S(fib[i])) % MOD;
    }

    cout << ans << endl;
    return 0;
}