All Euler problems
Project Euler

Sum of Primitive Roots

For a prime p, the sum of all primitive roots modulo p is mu(p-1) (mod p). Find sum_(p <= 10^6, p prime) (sum of primitive roots mod p) (mod 10^9+7).

Source sync Apr 19, 2026
Problem #0949
Level Level 39
Solved By 65
Languages C++, Python
Answer 726010935
Length 277 words
number_theorymodular_arithmeticlinear_algebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Left and Right play a game with a number of words, each consisting of L’s and R’s, alternating turns. On Left’s turn, for each word, Left can remove any number of letters (possibly zero), but not all the letters, from the left side of the word. However, at least one letter must be removed from at least one word. Right does the same on Right’s turn except that Right removes letters from the right side of each word. The game continues until each word is reduced to a single letter. If there are more L’s than R’s remaining then Left wins; otherwise if there are more R’s than L’s then Right wins. In this problem we only consider games with an odd number of words, thus making ties impossible.

\(\vspace {0.75\baselineskip }\)

Let \(G(n, k)\) be the number of ways of choosing \(k\) words of length \(n\), for which Right has a winning strategy when Left plays first. Different orderings of the same set of words are to be counted separately.

It can be seen that \(G(2, 3)=14\) due to the following solutions (and their reorderings): \begin {align} (\texttt {LL},\texttt {RR},\texttt {RR})& : 3\text { orderings}\\ (\texttt {LR},\texttt {LR},\texttt {LR})& : 1\text { ordering}\\ (\texttt {LR},\texttt {LR},\texttt {RR})& : 3\text { orderings}\\ (\texttt {LR},\texttt {RR},\texttt {RR})& : 3\text { orderings}\\ (\texttt {RL},\texttt {RR},\texttt {RR})& : 3\text { orderings}\\ (\texttt {RR},\texttt {RR},\texttt {RR})& : 1\text { ordering} \end {align}

You are also given \(G(4, 3)=496\) and \(G(8, 5)=26359197010\).

Find \(G(20, 7)\) giving your answer modulo \(1001001011\).

Problem 949: Sum of Primitive Roots

Mathematical Analysis

Sum of Primitive Roots

Theorem. For a prime pp, the sum of all primitive roots modulo pp is congruent to μ(p1)(modp)\mu(p-1) \pmod{p}.

Proof. The primitive roots are elements of order p1p-1 in (Z/pZ)(\mathbb{Z}/p\mathbb{Z})^*. If gg is a primitive root, the others are gkg^k where gcd(k,p1)=1\gcd(k, p-1) = 1. The sum is:

k=1gcd(k,p1)=1p1gk=k=1p1gkdgcd(k,p1)μ(d)\sum_{\substack{k=1 \\ \gcd(k,p-1)=1}}^{p-1} g^k = \sum_{k=1}^{p-1} g^k \sum_{d | \gcd(k, p-1)} \mu(d)

By Mobius inversion and Ramanujan sum theory, this equals μ(p1)(modp)\mu(p-1) \pmod{p}. \square

The Mobius Function on p1p-1

μ(p1){1,0,1}\mu(p-1) \in \{-1, 0, 1\}:

  • μ(p1)=0\mu(p-1) = 0 if p1p-1 has a squared prime factor (e.g., p=5p = 5: p1=4=22p-1 = 4 = 2^2).
  • μ(p1)=(1)k\mu(p-1) = (-1)^k if p1p-1 is a product of kk distinct primes.

Editorial

Compute sum of mu(p-1) for primes p up to 10^6, mod 10^9 + 7. For a prime p, the number of primitive roots modulo p is phi(p-1). The Mobius function mu(p-1) indicates the “squarefree structure” of p-1: mu(p-1) = 0 if p-1 has a squared prime factor mu(p-1) = +1 if p-1 has an even number of distinct prime factors mu(p-1) = -1 if p-1 has an odd number of distinct prime factors. We sieve primes up to NN. We then iterate over each prime pp, compute μ(p1)\mu(p-1) by factoring p1p-1. Finally, accumulate μ(p1)mod(109+7)\mu(p-1) \bmod (10^9+7) (treating μ=1\mu = -1 as 109+610^9+6).

Pseudocode

Sieve primes up to $N$
For each prime $p$, compute $\mu(p-1)$ by factoring $p-1$
Accumulate $\mu(p-1) \bmod (10^9+7)$ (treating $\mu = -1$ as $10^9+6$)

Factoring p1p-1

Use a smallest-prime-factor (SPF) sieve up to NN to factorize p1p-1 quickly.

Concrete Examples

ppp1p-1μ(p1)\mu(p-1)Sum of prim roots mod pp
2111
32-12 (= 1mod3-1 \bmod 3)
5400
7611

Proof of Correctness

  1. Sum formula: Classical result in number theory.
  2. Mobius via SPF sieve: Correct factorization.
  3. Modular reduction: 1p1(modp)-1 \equiv p - 1 \pmod{p}, but accumulated mod109+7\bmod 10^9+7.

Complexity Analysis

  • SPF sieve: O(N)O(N).
  • Per prime: O(logp)O(\log p) for factorization via SPF.
  • Total: O(N)O(N).

Answer

726010935\boxed{726010935}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_949/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
    const int N=1000000;
    const long long MOD=1e9+7;
    vector<bool> is_p(N+1,true); is_p[0]=is_p[1]=false;
    for(int i=2;i*i<=N;i++) if(is_p[i]) for(int j=i*i;j<=N;j+=i) is_p[j]=false;
    vector<int> mu(N+1,0); mu[1]=1;
    vector<bool> ip2(N+1,true); vector<int> primes;
    for(int i=2;i<=N;i++){
        if(ip2[i]){primes.push_back(i);mu[i]=-1;}
        for(int p:primes){
            if((long long)i*p>N) break;
            ip2[i*p]=false;
            if(i%p==0){mu[i*p]=0;break;}
            else mu[i*p]=-mu[i];
        }
    }
    long long S=0;
    for(int p=2;p<=N;p++) if(is_p[p]) S=(S+mu[p-1]+MOD)%MOD;
    cout<<S<<endl;
    return 0;
}