Open Source · 7 Languages · 900+ Problems

Project Euler Solutions

Complete solutions in C++, Python & Java — with step-by-step mathematical explanations

All Problems

Problem 326: Modulo Summations

View on Project Euler

Project Euler Problem 326 Solution

EulerSolve provides an optimized solution for Project Euler Problem 326, Modulo Summations, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary The sequence is defined by $a_1=1,\qquad a_n\equiv \sum_{k=1}^{n-1}k\,a_k\pmod n\qquad(n\ge 2).$ For given \(N\) and \(M\), we must count how many subarrays of $a_1,a_2,\dots,a_N$ have sum divisible by \(M\). The final instance is $f(10^{12},10^6).$ A direct \(O(N)\) pass is impossible, so the key is to expose the hidden periodic structure. Mathematical Approach 1) Prefix sums turn divisibility into equal residues. Define prefix sums modulo \(M\): $P_t\equiv \sum_{i=1}^{t}a_i\pmod M,\qquad P_0=0.$ Then for any \(0\le i \lt j\le N\), $\sum_{k=i+1}^{j}a_k\equiv 0\pmod M \iff P_i\equiv P_j\pmod M.$ So if residue \(r\) appears \(c_r\) times among $P_0,P_1,\dots,P_N,$ then it contributes $\binom{c_r}{2}$ valid subarrays. Therefore $f(N,M)=\sum_{r=0}^{M-1}\binom{c_r}{2}.$ 2) Introduce the weighted running sum. Let $W_n=\sum_{k=1}^{n}k\,a_k.$ Then the recurrence is simply $a_n=W_{n-1}\bmod n,\qquad W_n=W_{n-1}+n\,a_n.$ This is exactly the pair of variables tracked in the C++ code. 3) The first terms reveal a 6-step block pattern. The code checks the first ten values $1,1,0,3,0,3,5,4,1,9,$ and if we continue a little further, the sequence groups naturally into blocks of length 6: $[1,1,0,3,0,3],$ $[5,4,1,9,1,6],$ $[9,7,2,15,2,9],\dots$ This is not an accident....

Detailed mathematical approach

Problem Summary

The sequence is defined by

$a_1=1,\qquad a_n\equiv \sum_{k=1}^{n-1}k\,a_k\pmod n\qquad(n\ge 2).$

For given \(N\) and \(M\), we must count how many subarrays of

$a_1,a_2,\dots,a_N$

have sum divisible by \(M\). The final instance is

$f(10^{12},10^6).$

A direct \(O(N)\) pass is impossible, so the key is to expose the hidden periodic structure.

Mathematical Approach

1) Prefix sums turn divisibility into equal residues.

Define prefix sums modulo \(M\):

$P_t\equiv \sum_{i=1}^{t}a_i\pmod M,\qquad P_0=0.$

Then for any \(0\le i \lt j\le N\),

$\sum_{k=i+1}^{j}a_k\equiv 0\pmod M \iff P_i\equiv P_j\pmod M.$

So if residue \(r\) appears \(c_r\) times among

$P_0,P_1,\dots,P_N,$

then it contributes

$\binom{c_r}{2}$

valid subarrays. Therefore

$f(N,M)=\sum_{r=0}^{M-1}\binom{c_r}{2}.$

2) Introduce the weighted running sum.

Let

$W_n=\sum_{k=1}^{n}k\,a_k.$

Then the recurrence is simply

$a_n=W_{n-1}\bmod n,\qquad W_n=W_{n-1}+n\,a_n.$

This is exactly the pair of variables tracked in the C++ code.

3) The first terms reveal a 6-step block pattern.

The code checks the first ten values

$1,1,0,3,0,3,5,4,1,9,$

and if we continue a little further, the sequence groups naturally into blocks of length 6:

$[1,1,0,3,0,3],$

$[5,4,1,9,1,6],$

$[9,7,2,15,2,9],\dots$

This is not an accident. For every integer \(m\ge 0\), the exact formulas are

$a_{6m+1}=4m+1,\qquad a_{6m+2}=3m+1,\qquad a_{6m+3}=m,$

$a_{6m+4}=6m+3,\qquad a_{6m+5}=m,\qquad a_{6m+6}=3m+3.$

These identities can be proved by induction from the definition of \(W_n\), and they are the real reason the problem becomes tractable.

4) Why this implies a period of \(6M\).

Each formula above is affine in \(m\). Therefore, modulo \(M\), replacing \(m\) by \(m+M\) leaves every value unchanged. So

$a_{n+6M}\equiv a_n\pmod M.$

That gives periodicity of the term sequence modulo \(M\).

5) Why the prefix residues also repeat with period \(6M\).

We still need the prefix sums \(P_t\), not just the terms \(a_t\). For one full period, the block sum is

$a_{6m+1}+\cdots+a_{6m+6}=18m+8.$

Summing over \(m=0,1,\dots,M-1\) gives

$\sum_{n=1}^{6M} a_n = \sum_{m=0}^{M-1}(18m+8)=M(9M-1),$

which is divisible by \(M\). Therefore one whole period changes the prefix sum by

$0\pmod M,$

and hence the prefix-residue sequence \((P_t)\) itself has period \(6M\).

6) Histogram over one period plus a tail.

Write

$N=q(6M)+t,\qquad 0\le t \lt 6M.$

Let \(h_r\) be the number of times residue \(r\) appears in one full period of

$P_0,P_1,\dots,P_{6M-1},$

and let \(u_r\) be the number of appearances in the tail

$P_0,P_1,\dots,P_t.$

Then the total count of residue \(r\) among \(P_0,\dots,P_N\) is

$c_r=q\,h_r+u_r.$

Once these counts are known, the answer is just

$f(N,M)=\sum_{r=0}^{M-1}\binom{c_r}{2}.$

7) Worked checkpoint.

The code checks

$f(10,10)=4,$

and also

$f(10^4,10^3)=97158.$

These are excellent sanity checks for both the sequence generator and the residue-histogram logic.

Algorithm

1) Simulate exactly one period of length \(6M\).

2) While simulating, record how often each prefix residue appears in the full period.

3) Also record the residue histogram for the first \(t=N\bmod 6M\) positions.

4) Combine the two histograms via \(c_r=q\,h_r+u_r\).

5) Sum \(\binom{c_r}{2}\) over all residues.

Complexity Analysis

Only one period is simulated, so the running time is

$O(M)$

up to the constant factor 6, and the memory usage is

$O(M).$

The huge value of \(N\) never appears as a loop bound.

Checks And Final Result

The checkpoints are

$a_1,\dots,a_{10}=1,1,0,3,0,3,5,4,1,9,$

$f(10,10)=4,\qquad f(10^4,10^3)=97158.$

For the target input, the final answer is

$f(10^{12},10^6)=1966666166408794329.$

Further Reading

  1. Problem page: https://projecteuler.net/problem=326
  2. Prefix sums modulo \(M\): https://en.wikipedia.org/wiki/Prefix_sum
  3. Modular periodicity ideas: https://en.wikipedia.org/wiki/Modular_arithmetic

Mathematical approach · C++ solution · Python solution · Java solution

Previous: Problem 325 · All Project Euler solutions · Next: Problem 327

Need help with a problem? Ask me! 💡
e
✦ Euler GLM 5.2
Hi! I'm Euler. Ask me anything about Project Euler problems, math concepts, or solution approaches! 🧮