Divisibility Multipliers
For each integer p > 1 coprime to 10, define the divisibility multiplier m(p) as the smallest positive integer m < p such that the function f(n) = floor(n/10) + (n mod 10) * m preserves divisibilit...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
For each integer \(p > 1\) coprime to \(10\) there is a positive
\(f(n) = (\text {all but the last digit of }n) + (\text {the last digit of }n) \cdot m\).
That is, if \(m\) is the divisibility multiplier for \(p\), then \(f(n)\) is divisible by \(p\) if and only if \(n\) is divisible by \(p\).
(When \(n\) is much larger than \(p\), \(f(n)\) will be less than \(n\) and repeated application of \(f\) provides a multiplicative divisibility test for \(p\).)
For example, the divisibility multiplier for \(113\) is \(34\).
\(f(76275) = 7627 + 5 \cdot 34 = 7797\): \(76275\) and \(7797\) are both divisible by \(113\).
\(f(12345) = 1234 + 5 \cdot 34 = 1404\): \(12345\) and \(1404\) are both not divisible by \(113\).
The sum of the divisibility multipliers for the primes that are coprime to \(10\) and less than \(1000\) is \(39517\). What is the sum of the divisibility multipliers for the primes that are coprime to \(10\) and less than \(10^7\)?
Problem 274: Divisibility Multipliers
Mathematical Foundation
Theorem 1 (Divisibility Multiplier is the Modular Inverse of 10). Let with . Then , i.e., the unique satisfying .
Proof. Write where and . Then . Suppose , i.e., . We require .
From , we get . Substituting:
For this to hold for all (since varying over multiples of produces all residues of modulo ), we need , i.e., .
Conversely, if , then for all , confirming that preserves divisibility.
Lemma 1 (Computation via Fermat’s Little Theorem). For prime , .
Proof. By Fermat’s little theorem, , so , meaning is the multiplicative inverse of modulo .
Lemma 2 (Extended Euclidean Alternative). For any with (not necessarily prime), can be computed in time via the extended Euclidean algorithm.
Proof. The extended GCD computes with , giving . The algorithm runs in steps.
Editorial
For each prime p coprime to 10, the divisibility multiplier m(p) is the smallest positive integer m < p such that for the operation f(n) = floor(n/10) + (n mod 10) * m, if p | n then p | f(n). This is equivalent to m(p) = 10^{-1} mod p (modular inverse of 10 mod p). Find the sum of m(p) for all primes p coprime to 10 with p < 10^7. We sieve of Eratosthenes. Finally, sum modular inverses.
Pseudocode
Sieve of Eratosthenes
Sum modular inverses
Compute 10^{-1} mod p via modular exponentiation
Complexity Analysis
- Time: The sieve of Eratosthenes runs in . For each of the primes (excluding 2 and 5), modular exponentiation costs . Total: . By the prime number theorem, , so the second term is . Overall: .
- Space: for the sieve array.
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 274: Divisibility Multipliers
//
// For each prime p coprime to 10 (i.e., p > 5 and p != 2),
// the divisibility multiplier m(p) is the modular inverse of 10 mod p.
// Find sum of m(p) for all primes 5 < p < 10^7.
int main() {
const int LIMIT = 10000000;
// Sieve of Eratosthenes
vector<bool> is_prime(LIMIT, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; (ll)i * i < LIMIT; i++) {
if (is_prime[i]) {
for (int j = i * i; j < LIMIT; j += i)
is_prime[j] = false;
}
}
ll total = 0;
for (int p = 3; p < LIMIT; p++) {
if (p == 5) continue;
if (!is_prime[p]) continue;
// Compute 10^{-1} mod p using Fermat's little theorem: 10^{p-2} mod p
// Or use extended GCD
ll inv = 1;
ll base = 10 % p;
ll exp = p - 2;
ll mod = p;
ll b = base;
ll e = exp;
ll r = 1;
while (e > 0) {
if (e & 1) r = r * b % mod;
b = b * b % mod;
e >>= 1;
}
total += r;
}
cout << total << endl;
return 0;
}
"""
Problem 274: Divisibility Multipliers
For each prime p coprime to 10, the divisibility multiplier m(p) is the
smallest positive integer m < p such that for the operation
f(n) = floor(n/10) + (n mod 10) * m, if p | n then p | f(n).
This is equivalent to m(p) = 10^{-1} mod p (modular inverse of 10 mod p).
Find the sum of m(p) for all primes p coprime to 10 with p < 10^7.
"""
def solve():
LIMIT = 10**7
# Sieve of Eratosthenes
is_prime = bytearray(b'\x01') * LIMIT
is_prime[0] = is_prime[1] = 0
for i in range(2, int(LIMIT**0.5) + 1):
if is_prime[i]:
is_prime[i*i::i] = bytearray(len(is_prime[i*i::i]))
total = 0
for p in range(3, LIMIT):
if p == 5:
continue
if is_prime[p]:
total += pow(10, -1, p)
print(total)
if __name__ == "__main__":
solve()