All Euler problems
Project Euler

Sequences with Nice Divisibility Properties

Let S(n, k, m) denote the number of n -tuples (a_1, a_2,..., a_n) of positive integers satisfying: 1. 1 <= a_i <= m for all 1 <= i <= n. 2. a_1 + a_2 +... + a_n equiv 0 (mod k). Compute S(n, k, m)...

Source sync Apr 19, 2026
Problem #0511
Level Level 23
Solved By 504
Languages C++, Python
Answer 935247012
Length 381 words
modular_arithmeticalgebranumber_theory

Problem Statement

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

Let $Seq(n,k)$ be the number of positive-integer sequences $\{a_i\}_{1 \le i \le n}$ of length $n$ such that:

  • $n$ is divisible by $a_i$ for $1 \le i \le n$, and

  • $n + a_1 + a_2 + \cdots + a_n$ is divisible by $k$.

Example:

$Seq(3,4) = 4$, and the $4$ sequences are: \begin{align*} \{1, 1, 3\} \\ \{1, 3, 1\} \\ \{3, 1, 1\} \\ \{3, 3, 3\} \end{align*}

$Seq(4,11) = 8$, and the $8$ sequences are: \begin{align*} \{1, 1, 1, 4\} \\ \{1, 1, 4, 1\} \\ \{1, 4, 1, 1\} \\ \{4, 1, 1, 1\} \\ \{2, 2, 2, 1\} \\ \{2, 2, 1, 2\} \\ \{2, 1, 2, 2\} \\ \{1, 2, 2, 2\} \end{align*}

The last nine digits of $Seq(1111,24)$ are $840643584$.

Find the last nine digits of $Seq(1234567898765,4321)$.

Problem 511: Sequences with Nice Divisibility Properties

Mathematical Foundation

Definition 1. Let ω=e2πi/k\omega = e^{2\pi i / k} denote a fixed primitive kk-th root of unity. For 0jk10 \leq j \leq k-1, define the character sum

gj  =  a=1mωja.g_j \;=\; \sum_{a=1}^{m} \omega^{ja}.

Theorem 1 (Roots of Unity Filter). For any integer ss,

[ks]  =  1kj=0k1ωjs,[k \mid s] \;=\; \frac{1}{k}\sum_{j=0}^{k-1} \omega^{js},

where [P][P] denotes the Iverson bracket (1 if PP is true, 0 otherwise).

Proof. We distinguish two cases.

Case 1: ksk \mid s. Then ωs=1\omega^s = 1, so ωjs=1\omega^{js} = 1 for every jj. The sum equals kk, and k/k=1k/k = 1.

Case 2: ksk \nmid s. Then ωs1\omega^s \neq 1. The sum is a geometric series with ratio ωs\omega^s:

j=0k1ωjs=(ωs)k1ωs1=(ωk)s1ωs1=11ωs1=0.\sum_{j=0}^{k-1} \omega^{js} = \frac{(\omega^s)^k - 1}{\omega^s - 1} = \frac{(\omega^k)^s - 1}{\omega^s - 1} = \frac{1 - 1}{\omega^s - 1} = 0.

In both cases the identity holds. \square

Theorem 2 (Counting Formula). With the notation above,

S(n,k,m)=1kj=0k1gjn.S(n, k, m) = \frac{1}{k}\sum_{j=0}^{k-1} g_j^{\,n}.

Proof. By definition,

S(n,k,m)=(a1,,an)1aim[ka1++an].S(n, k, m) = \sum_{\substack{(a_1,\ldots,a_n) \\ 1 \leq a_i \leq m}} [k \mid a_1 + \cdots + a_n].

Substituting the identity from Theorem 1:

S(n,k,m)=(a1,,an)1aim1kj=0k1ωj(a1++an).S(n, k, m) = \sum_{\substack{(a_1,\ldots,a_n) \\ 1 \leq a_i \leq m}} \frac{1}{k}\sum_{j=0}^{k-1} \omega^{j(a_1+\cdots+a_n)}.

Since all sums are finite, we may exchange the order of summation (Fubini’s theorem for finite sums):

S(n,k,m)=1kj=0k1(a1,,an)1aimi=1nωjai=1kj=0k1i=1n(a=1mωja)=1kj=0k1gjn.S(n, k, m) = \frac{1}{k}\sum_{j=0}^{k-1} \sum_{\substack{(a_1,\ldots,a_n) \\ 1 \leq a_i \leq m}} \prod_{i=1}^{n} \omega^{j a_i} = \frac{1}{k}\sum_{j=0}^{k-1} \prod_{i=1}^{n}\left(\sum_{a=1}^{m}\omega^{ja}\right) = \frac{1}{k}\sum_{j=0}^{k-1} g_j^{\,n}.

The factorization into a product holds because the exponent j(a1++an)j(a_1 + \cdots + a_n) separates multiplicatively, and the constraint on each aia_i is identical and independent. \square

Lemma 1 (Closed Form for gjg_j). For 0jk10 \leq j \leq k - 1:

gj={mif j=0,ωj1ωjm1ωjif 1jk1.g_j = \begin{cases} m & \text{if } j = 0, \\ \displaystyle \omega^j \cdot \frac{1 - \omega^{jm}}{1 - \omega^j} & \text{if } 1 \leq j \leq k - 1. \end{cases}

Proof. When j=0j = 0, every term ω0a=1\omega^{0 \cdot a} = 1, so g0=a=1m1=mg_0 = \sum_{a=1}^{m} 1 = m.

When 1jk11 \leq j \leq k - 1, we have ωj1\omega^j \neq 1 (since ω\omega is a primitive kk-th root). The sum gj=ωj+ω2j++ωmjg_j = \omega^j + \omega^{2j} + \cdots + \omega^{mj} is a finite geometric series with first term ωj\omega^j, common ratio ωj\omega^j, and mm terms. By the standard geometric series formula:

gj=ωj(ωj)m1ωj1=ωj1ωjm1ωj.g_j = \omega^j \cdot \frac{(\omega^j)^m - 1}{\omega^j - 1} = \omega^j \cdot \frac{1 - \omega^{jm}}{1 - \omega^j}. \qquad \square

Corollary 1 (Special Case m=km = k). When m=km = k, we have S(n,k,k)=kn1S(n, k, k) = k^{n-1}.

Proof. For j=0j = 0: g0=kg_0 = k. For 1jk11 \leq j \leq k - 1: a=1kωja=ωja=0k1ωjaωj0+ωjk=ωj01+1\sum_{a=1}^{k}\omega^{ja} = \omega^j \sum_{a=0}^{k-1}\omega^{ja} - \omega^{j \cdot 0} + \omega^{jk} = \omega^j \cdot 0 - 1 + 1. More directly, a=0k1ωja=0\sum_{a=0}^{k-1}\omega^{ja} = 0 for j≢0(modk)j \not\equiv 0 \pmod{k} (by Theorem 1 with s=js = j), so a=1kωja=a=0k1ωja+ωjkωj0=0+11=0\sum_{a=1}^{k}\omega^{ja} = \sum_{a=0}^{k-1}\omega^{ja} + \omega^{jk} - \omega^{j \cdot 0} = 0 + 1 - 1 = 0. Therefore S(n,k,k)=kn/k=kn1S(n,k,k) = k^n / k = k^{n-1}.

A combinatorial verification: choose a1,,an1{1,,k}a_1, \ldots, a_{n-1} \in \{1,\ldots,k\} freely (kn1k^{n-1} ways), then ana_n is uniquely determined as the element of {1,,k}\{1,\ldots,k\} satisfying an(a1++an1)(modk)a_n \equiv -(a_1 + \cdots + a_{n-1}) \pmod{k}. \square

Proposition 1 (Modular Arithmetic Realization). Suppose pp is a prime with k(p1)k \mid (p-1). Let gg be a primitive root modulo pp and set ω^=g(p1)/kmodp\hat{\omega} = g^{(p-1)/k} \bmod p. Then ω^\hat{\omega} is a primitive kk-th root of unity in Fp\mathbb{F}_p, and the formula from Theorem 2 can be evaluated entirely in Fp\mathbb{F}_p:

S(n,k,m)k1j=0k1g^jn(modp),S(n, k, m) \equiv k^{-1} \sum_{j=0}^{k-1} \hat{g}_j^{\,n} \pmod{p},

where g^j\hat{g}_j is the modular analogue of gjg_j, computed using ω^\hat{\omega} in place of ω\omega.

Proof. Since gg has order p1p - 1 in Fp\mathbb{F}_p^*, the element ω^=g(p1)/k\hat{\omega} = g^{(p-1)/k} has order exactly kk, hence is a primitive kk-th root of unity in Fp\mathbb{F}_p. The algebraic identities in Theorems 1, 2, and Lemma 1 hold over any field containing such a root, in particular over Fp\mathbb{F}_p. Division by kk is well-defined since pkp \nmid k (as k(p1)k \mid (p-1) and pp is prime). \square

Editorial

Count n-tuples (a_1,…,a_n) with 1 <= a_i <= m and sum divisible by k. Uses the roots-of-unity filter (discrete Fourier transform technique). We precondition: p prime, k | (p - 1). Finally, else.

Pseudocode

Precondition: p prime, k | (p - 1)
else

Complexity Analysis

  • Time: O(klogn)O(k \log n). The loop iterates kk times; within each iteration, computing gjnmodpg_j^n \bmod p costs O(logn)O(\log n) via binary exponentiation.
  • Space: O(1)O(1) auxiliary beyond the input.

Answer

935247012\boxed{935247012}

Code

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

C++ project_euler/problem_511/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll mod_pow(ll base, ll exp, ll mod) {
    ll result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = (__int128)result * base % mod;
        base = (__int128)base * base % mod;
        exp >>= 1;
    }
    return result;
}

ll mod_inv(ll a, ll mod) {
    return mod_pow(a, mod - 2, mod);
}

// Find primitive root of prime p
ll primitive_root(ll p) {
    ll phi = p - 1;
    // Factor phi
    vector<ll> factors;
    ll n = phi;
    for (ll d = 2; d * d <= n; d++) {
        if (n % d == 0) {
            factors.push_back(d);
            while (n % d == 0) n /= d;
        }
    }
    if (n > 1) factors.push_back(n);

    for (ll g = 2; g < p; g++) {
        bool ok = true;
        for (ll f : factors) {
            if (mod_pow(g, phi / f, p) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
    return -1;
}

// S(n, k, m) mod p using roots of unity filter
// Requires k | (p-1)
ll S_mod(ll n, ll k, ll m, ll p) {
    assert((p - 1) % k == 0);

    ll g = primitive_root(p);
    ll omega = mod_pow(g, (p - 1) / k, p);
    ll inv_k = mod_inv(k, p);

    ll total = 0;
    for (ll j = 0; j < k; j++) {
        ll gj;
        if (j == 0) {
            gj = m % p;
        } else {
            ll w = mod_pow(omega, j, p);
            ll wm = mod_pow(w, m, p);
            // gj = w * (1 - w^m) / (1 - w)
            ll num = w % p * ((1 - wm % p + p) % p) % p;
            ll den = (1 - w % p + p) % p;
            gj = num % p * mod_inv(den, p) % p;
        }
        total = (total + mod_pow(gj, n, p)) % p;
    }

    return total % p * inv_k % p;
}

int main() {
    // Simple case: S(n, k) = k^(n-1) when m = k
    cout << "Simple case verification:" << endl;
    for (int n = 1; n <= 5; n++) {
        for (int k : {2, 3, 5}) {
            ll result = 1;
            for (int i = 0; i < n - 1; i++) result *= k;
            cout << "  S(" << n << ", " << k << ") = " << result << endl;
        }
    }

    // General case with modular arithmetic
    ll p = 1000000007;  // 10^9 + 7
    ll k = 7;
    // Check k | (p-1): (10^9 + 6) % 7 = ?
    if ((p - 1) % k == 0) {
        ll n = 100, m = 20;
        ll result = S_mod(n, k, m, p);
        cout << "\nS(" << n << ", " << k << ", " << m << ") mod " << p
             << " = " << result << endl;
    }

    return 0;
}