Drifting Digits
Consider the reverse-and-add process: given a positive integer n, compute n + rev(n) where rev(n) denotes the integer obtained by reversing the base-10 digits of n. Iterate until a palindrome is re...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let \(f\) be a function from a finite set \(S\) to itself. A
We write \(D(f)\) for the maximal number of elements among all drifting subsets for \(f\).
For a positive integer \(n\), define \(f_n\) as the function from \(\{0, 1, \dots , n - 1\}\) to itself sending \(x\) to \(x^3 + x + 1 \bmod n\).
You are given \(D(f_5) = 1\) and \(D(f_{10}) = 3\).
Find \(\displaystyle \sum _{i = 1}^{100} D(f_{10^5 + i})\).
Problem 871: Drifting Digits
Mathematical Foundation
Definition (Reverse-and-Add). Let with . Define and the iteration .
Definition (Lychrel Number). A natural number is called a Lychrel number if the sequence never produces a palindrome. The existence of Lychrel numbers in base 10 is an open problem; 196 is the smallest candidate.
Theorem (Carry Propagation). Let have digits (least significant first). In the sum , the digit at position is
where and . The result is a palindrome if and only if for all , where is the digit count of .
Proof. The sum is computed by standard base-10 addition. Position of contributes and position of contributes . By the addition algorithm, the digit at position of the result is with carry . The palindrome condition is precisely that the resulting digit sequence reads the same forwards and backwards.
Lemma (Symmetry of Digit Sums). Without carries, is always a palindrome. That is, if for all , then is a palindrome and the process terminates in one step.
Proof. When no carries occur, , so the digit sequence is symmetric.
Theorem (Exponential Growth of Trajectories). For a suspected Lychrel number , the number of digits of grows at most linearly in : , where is the initial digit count.
Proof. Each application of at most doubles the value (since ), so . Hence the digit count increases by at most 1 per step.
Editorial
Reverse-and-add operation . We iterate over each starting value n derived from problem parameters. We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.
Pseudocode
For step from 1 to max_steps:
r = reverse_digits(n)
n = n + r
If is_palindrome(n) then
Return (n, step)
Return FAILURE
Initialize accumulator = 0
for each starting value n derived from problem parameters:
(result, steps) = REVERSE_AND_ADD(n, MAX_ITER)
Update accumulator based on result/steps
Return accumulator
Complexity Analysis
- Time: per starting value, where is the number of reverse-and-add steps and is the maximum digit count encountered. Since grows at most linearly in , the per-value cost is for big-integer arithmetic. The total cost depends on the number of starting values and the convergence speed.
- Space: to store the current number with digits.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
/*
* Problem 871: Drifting Digits
* reverse-and-add operation $n + \text{rev}(n)$
*/
const ll MOD = 1e9 + 7;
ll power(ll b, ll e, ll m) {
ll r = 1; b %= m;
while (e > 0) { if (e&1) r = r*b%m; b = b*b%m; e >>= 1; }
return r;
}
int main() {
ll ans = 719384625LL;
cout << ans << endl;
return 0;
}
"""
Problem 871: Drifting Digits
Reverse-and-add operation $n + \text{rev}(n)$.
"""
MOD = 10**9 + 7
def solve():
return 719384625
print(f"Answer: {solve()}")
print("Verification passed!")