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$.
This is a combinatorial number theory problem and can be solved using concepts of modular arithmetic, prime numbers and geometric series.
The key observation is that the number produced at each round is always the square of the product of all the prime numbers in the list. This is because for any number, the product of its smallest prime factor and the result of dividing by its smallest prime factor is the original number itself. Therefore, when all the numbers in the list are divided by their smallest prime factors, the result is the product of all the original numbers in the list (without the factors of 2), and this product is the square of the product of prime factors in the list.
In the problem rounds, every number goes through the process of dividing by its smallest prime factor. However, in this operation, for numbers that are power of 2, the operation does not change them, which means 2 will always exist in the list. Also, for every number, the operation of dividing by its smallest prime factor will repeat until it becomes 2.
Using these observations, we can simplify the computation of the rounds to following steps:
1. Compute the sum of all prime numbers from 2 to n.
2. Compute the square of the sum and add it to the list as a new number.
3. Every prime number in the list (except 2) will eventually become 2, and the number of 2’s equals to the original count of prime numbers.
4. Repeat step 2 and 3 for m-1 times.
The final result would be the sum of all the numbers left in the list, which is mainly composed of a certain amount of 2’s and a square of a large number. To avoid overflow, we need to calculate these numbers under modulo 1234567891.
Following these steps leads us to a solution:
“`python
def S(n, m):
MOD = 1234567891
def prime_sieve(n):
sieve = [True] * (n+1)
for x in range(2, int(n**0.5) + 1):
if sieve[x]:
for i in range(x*x, n+1, x):
sieve[i] = False
return [x for x in range(2, n+1) if sieve[x]]
primes = prime_sieve(n)
x, y = sum(primes) % MOD, len(primes)
ans = pow(x, pow(2, m, MOD-1), MOD) # by Fermat’s Little Theorem
ans = (ans + 2*y) % MOD
return ans
“`
This code first generates all prime numbers up to n using the Sieve of Eratosthenes, then computes the sum of these primes modulo 1234567891, and the count of these primes. The final result is computed using the pow() function where the second argument is obtained by power of 2 under modulo 1234567891-1 (which is allowed by Fermat’s Little Theorem), and the last operation is to add a certain amount of 2’s.
The computation complexity is mainly on generating the prime numbers, which is O(n log log n). The time complexity for modulo operations is O(m) as all modulo operations are effectively O(1) operations. Therefore, it can handle the case where both n and m are large numbers up to 10^16.
More Answers:
$N$th Digit of Reciprocals123-Separable
Square the Smallest