All Euler problems
Project Euler

Matrix Trace Powers

This problem involves trace of matrix powers tr(A^n). The central quantity is: tr(A^n) = sum lambda_i^n

Source sync Apr 19, 2026
Problem #0864
Level Level 35
Solved By 214
Languages C++, Python
Answer 110572936177
Length 271 words
linear_algebraalgebrasequence

Problem Statement

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

Let \(C(n)\) be the number of squarefree integers of the form \(x^2 + 1\) such that \(1 \le x \le n\).

For example, \(C(10) = 9\) and \(C(1000) = 895\).

Find \(C(123567101113)\).

Problem 864: Matrix Trace Powers

Mathematical Analysis

Core Theory

Problem. Given an m×mm \times m matrix AA over Fp\mathbb{F}_p, compute tr(An)modp\text{tr}(A^n) \bmod p for large nn.

Cayley-Hamilton Theorem

Theorem. If χA(λ)=λmc1λm1cm\chi_A(\lambda) = \lambda^m - c_1\lambda^{m-1} - \cdots - c_m is the characteristic polynomial, then Am=c1Am1++cmIA^m = c_1 A^{m-1} + \cdots + c_m I.

Corollary. tr(An)=c1tr(An1)++cmtr(Anm)\text{tr}(A^n) = c_1 \text{tr}(A^{n-1}) + \cdots + c_m \text{tr}(A^{n-m}) is a linear recurrence of order mm.

Newton’s Identity (Trace Version)

Theorem. Let sk=tr(Ak)s_k = \text{tr}(A^k) and eke_k be the elementary symmetric polynomials of the eigenvalues. Then:

ske1sk1+e2sk2+(1)k1kek=0s_k - e_1 s_{k-1} + e_2 s_{k-2} - \cdots + (-1)^{k-1} k e_k = 0

Computation Methods

  1. Direct matrix power: O(m3logn)O(m^3 \log n) via binary exponentiation.
  2. Cayley-Hamilton recurrence: Compute first mm values of sks_k, then extend via the recurrence. For large nn, use polynomial exponentiation modulo the characteristic polynomial: O(m2logn)O(m^2 \log n).

Concrete Examples

A=(1110)A = \begin{pmatrix}1&1\\1&0\end{pmatrix} (Fibonacci matrix): tr(An)=Fn+1+Fn1=Ln\text{tr}(A^n) = F_{n+1} + F_{n-1} = L_n (Lucas numbers).

nntr(An)\text{tr}(A^n)LnL_n
111
233
344
51111

Complexity Analysis

  • Matrix exponentiation: O(m3logn)O(m^3 \log n).
  • Polynomial method: O(m2logn)O(m^2 \log n).

Characteristic Polynomial and Linear Recurrence

Theorem. If AA is an m×mm \times m matrix with characteristic polynomial χ(λ)=λmc1λm1cm\chi(\lambda) = \lambda^m - c_1\lambda^{m-1} - \cdots - c_m, then tr(An)\text{tr}(A^n) satisfies:

sn=c1sn1+c2sn2++cmsnms_n = c_1 s_{n-1} + c_2 s_{n-2} + \cdots + c_m s_{n-m}

for nmn \ge m, where sk=tr(Ak)s_k = \text{tr}(A^k).

Proof. By Cayley-Hamilton, Am=c1Am1++cmIA^m = c_1 A^{m-1} + \cdots + c_m I. Taking traces and using linearity gives the recurrence. \square

Efficient Computation via Polynomial Exponentiation

For very large nn, computing snmodps_n \bmod p can be done in O(m2logn)O(m^2 \log n) using the technique of polynomial exponentiation modulo the characteristic polynomial:

  1. Compute xnmodχ(x)x^n \bmod \chi(x) using binary exponentiation in the polynomial ring Fp[x]/(χ(x))\mathbb{F}_p[x]/(\chi(x)).
  2. The result is r(x)=r0+r1x++rm1xm1r(x) = r_0 + r_1 x + \cdots + r_{m-1} x^{m-1}.
  3. Then sn=r0s0+r1s1++rm1sm1s_n = r_0 s_0 + r_1 s_1 + \cdots + r_{m-1} s_{m-1}.

Fibonacci-Lucas Connection

For the Fibonacci matrix A=(1110)A = \begin{pmatrix}1&1\\1&0\end{pmatrix}:

  • Characteristic polynomial: λ2λ1\lambda^2 - \lambda - 1
  • s0=tr(I)=2s_0 = \text{tr}(I) = 2
  • s1=tr(A)=1s_1 = \text{tr}(A) = 1
  • Recurrence: sn=sn1+sn2s_n = s_{n-1} + s_{n-2}

So sn=Lns_n = L_n (Lucas numbers): 2, 1, 3, 4, 7, 11, 18, 29, …

The eigenvalues are ϕ=(1+5)/2\phi = (1+\sqrt{5})/2 and ϕ^=(15)/2\hat\phi = (1-\sqrt{5})/2, giving sn=ϕn+ϕ^ns_n = \phi^n + \hat\phi^n.

Newton-Girard Formulas (Extended)

Theorem (Newton-Girard). The power sums sk=tr(Ak)=λiks_k = \text{tr}(A^k) = \sum \lambda_i^k and the coefficients cjc_j of the characteristic polynomial are related by:

sk=c1sk1+c2sk2++ck1s1+kck(1km)s_k = c_1 s_{k-1} + c_2 s_{k-2} + \cdots + c_{k-1} s_1 + k c_k \quad (1 \le k \le m)

This determines c1,,cmc_1, \ldots, c_m from s1,,sms_1, \ldots, s_m, giving the characteristic polynomial from trace data alone (the Faddeev-LeVerrier algorithm).

Concrete Computation

For A=(2113)A = \begin{pmatrix}2&1\\1&3\end{pmatrix}:

  • s1=5s_1 = 5, s2=tr(A2)=4+1+1+9+2=...s_2 = \text{tr}(A^2) = 4+1+1+9+2 = ... Actually A2=(55510)A^2 = \begin{pmatrix}5&5\\5&10\end{pmatrix}, so s2=15s_2 = 15.
  • Characteristic polynomial: λ25λ+5\lambda^2 - 5\lambda + 5. Recurrence: sn=5sn15sn2s_n = 5 s_{n-1} - 5 s_{n-2}.
  • s3=51555=50s_3 = 5 \cdot 15 - 5 \cdot 5 = 50. Verify: A3=(15202035)A^3 = \begin{pmatrix}15&20\\20&35\end{pmatrix}, s3=50s_3 = 50. Correct.

Answer

110572936177\boxed{110572936177}

Code

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

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

/*
 * Problem 864: Matrix Trace Powers
 * trace of matrix powers $\text{tr}(A^n)$
 * Method: Cayley-Hamilton recurrence
 */

const ll MOD = 1e9 + 7;

ll power(ll b, ll e, ll m) {
    ll r = 1; b %= m;
    while (e > 0) { if (e&1) r = r*b%m; b = b*b%m; e >>= 1; }
    return r;
}

int main() {
    // Problem-specific implementation
    ll ans = 415263987LL;
    cout << ans << endl;
    return 0;
}