Summation of a Modular Formula
For an odd prime p, define f(p) = floor(frac2^(2^p)p) mod 2^p. Let g(p) = f(p) and define G(N) = sum_(p < N, p odd prime) g(p). Given G(100) = 474 and G(10^4) = 2,819,236, find G(10^7).
Problem Statement
This archive keeps the full statement, math, and original media on the page.
For an odd prime \(p\), define \(f(p) = \left \lfloor \frac {2^{(2^p)}}{p}\right \rfloor \bmod {2^p}\)
For example, when \(p=3\), \(\lfloor 2^8/3\rfloor = 85 \equiv 5 \pmod 8\) and so \(f(3) = 5\).
Further define \(g(p) = f(p)\bmod p\). You are given \(g(31) = 17\).
Now define \(G(N)\) to be the summation of \(g(p)\) for all odd primes less than \(N\).
You are given \(G(100) = 474\) and \(G(10^4) = 2819236\).
Find \(G(10^7)\).
Problem 717: Summation of a Modular Formula
Mathematical Foundation
Lemma 1. For an odd prime , we have
Proof. Write where . Then . We want . Note that gives us for some integer , which equals . Dividing by and taking the integer part gives (since ). More precisely, .
Theorem 1. The value can be computed by successive squarings modulo , starting from .
Proof. We compute , for . Then . Each squaring preserves the residue modulo , and after squarings the exponent is .
Lemma 2. The modulus fits in bits. For up to , this requires arbitrary-precision arithmetic or a decomposition using the Chinese Remainder Theorem (CRT).
Proof. The modulus has bits, which for is about bits. Direct computation is infeasible.
Theorem 2 (CRT Decomposition). Since for odd , by CRT:
Now can be computed via using Fermat’s little theorem (). And since for all .
Proof. CRT applies because . For the first component, Fermat’s little theorem gives , so . The exponent is computed by fast modular exponentiation. For the second component, is divisible by since (the exponent ), so the residue is 0.
Theorem 3. Combining via CRT, let . Then
requires reconstruction. Since and , by CRT … More directly:
where is the inverse of modulo .
Proof. Write in the integers (here we slightly abuse notation: ). Then . Modulo : since , we get . The inverse exists since is odd.
Editorial
We iterate over each odd prime p in primes. Finally, since p is odd, use extended Euclidean or lifting. We first generate the primes required by the search, then enumerate the admissible combinations and retain only the values that satisfy the final test.
Pseudocode
for each odd prime p in primes
Compute e = 2^p mod (p-1)
Compute r = 2^e mod p
Compute p_inv = p^{-1} mod 2^p
Since p is odd, use extended Euclidean or lifting
f(p) = (-r * p_inv) mod 2^p
Complexity Analysis
- Time: For each prime : for modular exponentiation (Steps 1—2), bits for the inverse computation (Step 3). Summing over all primes : the dominant cost is bit operations, which is too slow for without further optimization. With word-level operations ( bits), this becomes .
- Space: for storing primes, plus for the largest inverse computation.
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 717: Summation of a Modular Formula
*
* 1. Implement the mathematical framework described above.
* 2. Optimize for the target input size.
* 3. Verify against known test values.
*/
int main() {
printf("Problem 717: Summation of a Modular Formula\n");
// Modular exponentiation: 2^{2^p} mod (p * 2^p)
int N = 100;
long long total = 0;
for (int n = 1; n <= N; n++) {
total += n; // Replace with problem-specific computation
}
printf("Test sum(1..%d) = %lld\n", N, total);
printf("Full implementation needed for target input.\n");
return 0;
}
"""
Problem 717: Summation of a Modular Formula
"""
print("Problem 717: Summation of a Modular Formula")
# Core computation
N = 100 # Small test case
values = list(range(1, N + 1)) # Placeholder for problem-specific computation
# The full solution implements: Modular exponentiation: 2^{2^p} mod (p * 2^p)
print(f"Computed {len(values)} values")
print(f"Sum = {sum(values)}")
plot_data = [values, values, values, values]