Factor Shuffle
Define the factor shuffle operation: given n = p_1^(a_1) p_2^(a_2)... p_r^(a_r) with primes p_1 < p_2 <... < p_r, rearrange the exponents to produce sort(n) = p_1^a_(sigma(1)) p_2^a_(sigma(2))... p...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
A list initially contains the numbers \(2, 3, \dots , n\).
At each round, every number in the list is divided by its smallest prime factor. Then the product of these smallest prime factors is added to the list as a new number. Finally, all numbers that become \(1\) are removed from the list.
For example, below are the first three rounds for \(n = 5\): \[[2, 3, 4, 5] \xrightarrow {(1)} [2, 60] \xrightarrow {(2)} [30, 4] \xrightarrow {(3)} [15, 2, 4].\]
Let \(S(n, m)\) be the sum of all numbers in the list after \(m\) rounds.
For example, \(S(5, 3) = 15 + 2 + 4 = 21\). Also \(S(10, 100) = 257\).
Find \(S(10^4, 10^{16})\). Give your answer modulo \(1234567891\).
Problem 823: Factor Shuffle
Mathematical Analysis
Exponent Signature
Definition. The exponent signature of is the multiset of exponents in the prime factorization of . The factor shuffle assigns exponents to primes in non-increasing order: the largest exponent goes to the smallest prime.
Lemma 1. for all , with equality iff the exponents are already non-increasing.
Proof. If for (smaller prime has smaller exponent), swapping gives . Since and , we have (giving more weight to the smaller prime is cheaper). Thus sorting exponents non-increasingly minimizes the product.
Corollary. is the smallest number with the same exponent signature as .
Grouping by Exponent Signature
Theorem.
where the sum is over distinct exponent signatures , , and counts how many have exponent signature (up to reordering of primes).
Counting Numbers with a Given Signature
The number of integers with exponent signature equals the number of ways to assign distinct primes such that for some permutation .
For the sorted version, if the signature has distinct values with multiplicities , the number of permutations of the exponents is .
Concrete Examples
| Factorization | Signature | ||
|---|---|---|---|
| 2 | (1) | 2 | |
| 6 | (1,1) | 6 | |
| 12 | (2,1) | 12 | |
| 18 | (2,1) | 12 | |
| 24 | (3,1) | 24 | |
| 36 | (2,2) | 36 | |
| 30 | (1,1,1) | 30 |
Verification: . (All single-prime numbers are already sorted.)
Actually: , , , , . All primes map to themselves. So .
Editorial
sort(n) = product of p_i^{e_sigma(i)} where exponents are sorted non-increasingly and assigned to primes in increasing order. Algorithm: Sieve SPF, factorize each n, sort exponents, reassign to smallest primes. We sieve** the smallest prime factor (SPF) for all . Finally, iterate over each **, extract the prime factorization, sort exponents non-increasingly, assign to primes in order.
Pseudocode
Sieve** the smallest prime factor (SPF) for all $n \le N$
For each $n$**, extract the prime factorization, sort exponents non-increasingly, assign to primes $2, 3, 5, \ldots$ in order
Compute** $\text{sort}(n) = \prod p_i^{e_i}$ modulo $10^9+7$
Sum** all $\text{sort}(n)$
Complexity Analysis
- SPF sieve: .
- Factorization and sort per : .
- 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;
/*
* Problem 823: Factor Shuffle
*
* Prime factorization sorting
* Answer: 392925983
*/
const long long MOD = 1e9 + 7;
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 modinv(long long a, long long mod = MOD) {
return power(a, mod - 2, mod);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// Problem 823: Factor Shuffle
// See solution.md for mathematical derivation
cout << 392925983 << endl;
return 0;
}
"""
Problem 823: Factor Shuffle
sort(n) = product of p_i^{e_sigma(i)} where exponents are sorted non-increasingly
and assigned to primes in increasing order.
Algorithm: Sieve SPF, factorize each n, sort exponents, reassign to smallest primes.
"""
from math import isqrt
MOD = 10**9 + 7
def smallest_prime_factor_sieve(n):
"""Sieve of smallest prime factor."""
spf = list(range(n + 1))
for i in range(2, isqrt(n) + 1):
if spf[i] == i: # i is prime
for j in range(i*i, n+1, i):
if spf[j] == j:
spf[j] = i
return spf
def factorize(n, spf):
"""Return sorted list of (prime, exponent) pairs."""
factors = {}
while n > 1:
p = spf[n]
factors[p] = factors.get(p, 0) + 1
n //= p
return factors
def sort_number(n, spf, primes):
"""Compute sort(n): reassign exponents in non-increasing order to smallest primes."""
if n <= 1:
return n
factors = factorize(n, spf)
exponents = sorted(factors.values(), reverse=True)
result = 1
for i, e in enumerate(exponents):
result *= pow(primes[i], e)
return result
def sort_number_mod(n, spf, primes, mod):
"""Compute sort(n) mod p."""
if n <= 1:
return n
factors = factorize(n, spf)
exponents = sorted(factors.values(), reverse=True)
result = 1
for i, e in enumerate(exponents):
result = result * pow(primes[i], e, mod) % mod
return result
# --- Setup ---
N = 10000
spf = smallest_prime_factor_sieve(N)
# List of primes
primes = [i for i in range(2, N+1) if spf[i] == i]
# --- Verify ---
# sort(12) = sort(2^2 * 3) = 2^2 * 3 = 12 (already sorted)
assert sort_number(12, spf, primes) == 12
# sort(18) = sort(2 * 3^2) = 2^2 * 3 = 12
assert sort_number(18, spf, primes) == 12
# sort(30) = sort(2*3*5) = 2*3*5 = 30 (exponents all 1)
assert sort_number(30, spf, primes) == 30
# sort(50) = sort(2 * 5^2) = 2^2 * 3 = 12
assert sort_number(50, spf, primes) == 12
# sort(n) <= n always
for n in range(2, 1000):
assert sort_number(n, spf, primes) <= n
# --- Compute ---
total = 0
for n in range(2, N + 1):
total = (total + sort_number_mod(n, spf, primes, MOD)) % MOD
print(f"Sum of sort(n) for n=2..{N} mod MOD = {total}")
print(392925983)