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...
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 , 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 where :
- If : (a repdigit of nines)
- If : (the digit followed by nines)
In both cases: where when , but more cleanly:
Let and . Then:
- If :
- If :
Uniformly: where we define if , else handle the case by writing .
Computing S(k)
Group terms by blocks of 9. Let with .
For a complete block from to (i.e., for , and for ):
Therefore:
For the remaining terms ( to ):
Wait, .
So the remainder sum is:
Combining:
where and .
Simplifying:
Editorial
We iterate over each Fibonacci number, compute using the closed-form formula with modular exponentiation. Finally, sum all results modulo .
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.
Complexity Analysis
- Computing each : for modular exponentiation, where .
- Total: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
MOD = 1_000_000_007
def power(base, exp, mod):
return pow(base, exp, mod)
def S(k):
if k <= 0:
return 0
Q = k // 9
R = k % 9
p10 = power(10, Q, MOD)
coeff = (5 + (R + 1) * (R + 2) // 2) % MOD
result = p10 * coeff % MOD
result = (result - 9 * (Q % MOD) % MOD + MOD) % MOD
result = (result - R % MOD + MOD) % MOD
result = (result - 6 + MOD) % MOD
return result
# Compute Fibonacci numbers
fib = [0] * 91
fib[1] = 1
for i in range(2, 91):
fib[i] = fib[i-1] + fib[i-2]
ans = 0
for i in range(2, 91):
ans = (ans + S(fib[i])) % MOD
print(ans)