Alice and Bob have enjoyed playing Nim every day. However, they finally got bored of playing ordinary three-heap Nim.
So, they added an extra rule:
– Must not make two heaps of the same size.
The triple $(a, b, c)$ indicates the size of three heaps.
Under this extra rule, $(2,4,5)$ is one of the losing positions for the next player.
To illustrate:
– Alice moves to $(2,4,3)$
– Bob moves to $(0,4,3)$
– Alice moves to $(0,2,3)$
– Bob moves to $(0,2,1)$
Unlike ordinary three-heap Nim, $(0,1,2)$ and its permutations are the end states of this game.
For an integer $N$, we define $F(N)$ as the sum of $a + b + c$ for all the losing positions for the next player, with $0 \lt a \lt b \lt c \lt N$.
For example, $F(8) = 42$, because there are $4$ losing positions for the next player, $(1,3,5)$, $(1,4,6)$, $(2,3,6)$ and $(2,4,5)$.
We can also verify that $F(128) = 496062$.
Find the last $9$ digits of $F(10^{18})$.
To evaluate $Q(10^{12})$ using Python code, we need to find a method to calculate $E(m, n)$ efficiently. Let’s break down the problem and go step by step.
Step 1: Generate the list of prime numbers up to $m$
To calculate $p_m\# = \prod_{i=1}^{m} p_i$, we first need to generate the list of prime numbers up to the $m$th prime number. We can use the sieve of Eratosthenes algorithm to generate prime numbers efficiently.
“`python
def generate_prime_numbers(n):
primes = [True] * (n+1)
primes[0] = primes[1] = False
p = 2
while p * p <= n:
if primes[p] == True:
for i in range(p * p, n+1, p):
primes[i] = False
p += 1
return [index for index, value in enumerate(primes) if value]
m = 904961
primes = generate_prime_numbers(m)
```
Now, we have a list `primes` that contains all prime numbers up to $m$.
Step 2: Calculate $p_m\#$
Now that we have the list of prime numbers, we can calculate $p_m\# = \prod_{i=1}^{m} p_i$.
```python
pm_hash = 1
for prime in primes:
pm_hash *= prime
```
Step 3: Calculate $S((p_m\#)^n)$ efficiently
To calculate $S((p_m\#)^n)$ efficiently, we can leverage the prime factorization of $(p_m\#)^n$. We can find the prime factorization of $(p_m\#)^n$ by finding the power of each prime number in $p_m\#$.
```python
factors = {}
for prime in primes:
if prime > n:
break
count = 0
p = prime
while p <= n:
count += n // p
p *= prime
factors[prime] = count
```
Now, we have a dictionary `factors` where the key is the prime number and the value is the highest power that divides $(p_m\#)^n$.
Step 4: Calculate $E(m, n)$
To calculate $E(m, n)$, we need to find the highest power of 2 that divides $S((p_m\#)^n)$. We can use the prime factorization from Step 3 to obtain this.
```python
def calculate_e(factors):
if 2 not in factors:
return 0
return factors[2]
e = calculate_e(factors)
```
Step 5: Calculate $Q(n)$
Finally, we can calculate $Q(n)$ as the sum of $E(904961, i)$ for $i$ in the range from 1 to $n$.
```python
def calculate_q(n):
q = 0
for i in range(1, n+1):
factors = {}
for prime in primes:
if prime > i:
break
count = 0
p = prime
while p <= i:
count += i // p
p *= prime
factors[prime] = count
e = calculate_e(factors)
q += e
return q
q = calculate_q(10**12)
print(q)
```
Now, you can run the above code snippet to obtain the value of $Q(10^{12})$.
Note: The code provided is a general implementation. For larger values of $n$ and $m$, the code might take longer to execute. Consider optimizing the code using memoization or other techniques to improve performance if needed.
More Answers:
Arithmetic DerivativeMaximum Number of Divisors
Sums of Power Sums