All Euler problems
Project Euler

Modular Fibonacci Matrix

Define the Fibonacci sequence F_0 = 0, F_1 = 1, F_n = F_(n-1) + F_(n-2). Find M = sum_(k=1)^10^12 k * F_k (mod 10^9 + 7).

Source sync Apr 19, 2026
Problem #0936
Level Level 38
Solved By 122
Languages C++, Python
Answer 12144907797522336
Length 187 words
modular_arithmeticlinear_algebrasequence

Problem Statement

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

A peerless tree is a tree with no edge between two vertices of the same degree. Let \(P(n)\) be the number of peerless trees on \(n\) unlabelled vertices.

There are six of these trees on seven unlabelled vertices, \(P(7)=6\), shown below.

PIC

Define \(\displaystyle S(N) = \sum _{n=3}^N P(n)\). You are given \(S(10) = 74\).

Find \(S(50)\).

Problem 936: Modular Fibonacci Matrix

Mathematical Foundation

Theorem 1 (Fibonacci matrix identity). For all n1n \geq 1:

(Fn+1FnFnFn1)=(1110)n.\begin{pmatrix} F_{n+1} & F_n \\ F_n & F_{n-1} \end{pmatrix} = \begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}^n.

Proof. By induction on nn. Base case (n=1n = 1): (F2F1F1F0)=(1110)\begin{pmatrix} F_2 & F_1 \\ F_1 & F_0 \end{pmatrix} = \begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}. Inductive step: assume the identity holds for nn. Then:

(1110)n+1=(Fn+1FnFnFn1)(1110)=(Fn+1+FnFn+1Fn+Fn1Fn)=(Fn+2Fn+1Fn+1Fn).\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}^{n+1} = \begin{pmatrix} F_{n+1} & F_n \\ F_n & F_{n-1} \end{pmatrix} \begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix} = \begin{pmatrix} F_{n+1} + F_n & F_{n+1} \\ F_n + F_{n-1} & F_n \end{pmatrix} = \begin{pmatrix} F_{n+2} & F_{n+1} \\ F_{n+1} & F_n \end{pmatrix}.

\square

Lemma 1 (Fibonacci partial sums). For all n1n \geq 1: k=1nFk=Fn+21\sum_{k=1}^{n} F_k = F_{n+2} - 1.

Proof. By induction. Base: F1=1=F31=21F_1 = 1 = F_3 - 1 = 2 - 1. Inductive step: k=1n+1Fk=(Fn+21)+Fn+1=Fn+31\sum_{k=1}^{n+1} F_k = (F_{n+2} - 1) + F_{n+1} = F_{n+3} - 1. \square

Theorem 2 (Weighted Fibonacci sum). For all n1n \geq 1:

k=1nkFk=(n1)Fn+2Fn+1+2.\sum_{k=1}^{n} k \, F_k = (n-1) \, F_{n+2} - F_{n+1} + 2.

Proof. We use Abel summation (summation by parts). Define Sn=k=1nFk=Fn+21S_n = \sum_{k=1}^{n} F_k = F_{n+2} - 1 (Lemma 1). Then:

k=1nkFk=nSnk=1n1Sk=n(Fn+21)k=1n1(Fk+21).\sum_{k=1}^{n} k \, F_k = n \, S_n - \sum_{k=1}^{n-1} S_k = n(F_{n+2} - 1) - \sum_{k=1}^{n-1}(F_{k+2} - 1).

The inner sum is:

k=1n1(Fk+21)=j=3n+1Fj(n1)=(j=1n+1FjF1F2)(n1)=(Fn+3111)(n1)=Fn+3n2.\sum_{k=1}^{n-1}(F_{k+2} - 1) = \sum_{j=3}^{n+1} F_j - (n-1) = \left(\sum_{j=1}^{n+1} F_j - F_1 - F_2\right) - (n-1) = (F_{n+3} - 1 - 1 - 1) - (n-1) = F_{n+3} - n - 2.

Substituting:

k=1nkFk=nFn+2nFn+3+n+2=nFn+2Fn+3+2.\sum_{k=1}^{n} k \, F_k = n \, F_{n+2} - n - F_{n+3} + n + 2 = n \, F_{n+2} - F_{n+3} + 2.

Since Fn+3=Fn+2+Fn+1F_{n+3} = F_{n+2} + F_{n+1}:

=nFn+2Fn+2Fn+1+2=(n1)Fn+2Fn+1+2.= n \, F_{n+2} - F_{n+2} - F_{n+1} + 2 = (n - 1) \, F_{n+2} - F_{n+1} + 2.

\square

Lemma 2 (Modular matrix exponentiation). FnmodmF_n \bmod m can be computed in O(logn)O(\log n) arithmetic operations modulo mm by repeated squaring of the 2×22 \times 2 matrix (1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}.

Proof. Each matrix multiplication involves a constant number (4 multiplications, 2 additions) of modular arithmetic operations. Repeated squaring performs O(logn)O(\log n) matrix multiplications. \square

Editorial

Compute S(N) = sum_{k=1}^{N} k * F_k mod (10^9 + 7), where N = 10^12. Key identity: sum_{k=1}^{N} k * F_k = N * F_{N+2} - F_{N+3} + 2 This follows from the telescoping identity k*F_k = (k+1)F_{k+1} - kF_k - F_{k+1} + F_k combined with the Fibonacci recurrence. Results:.

Pseudocode

Compute F_{n+1} and F_{n+2} via matrix exponentiation
if exp is odd
Q^(n+1) gives F_{n+2} in position [0][1]
Apply Theorem 2: (n-1)*F_{n+2} - F_{n+1} + 2

Complexity Analysis

  • Time: O(logn)O(\log n) for two matrix exponentiations, each involving O(logn)O(\log n) multiplications of 2×22 \times 2 matrices. Each matrix multiplication is O(1)O(1) arithmetic operations. Total: O(logn)O(\log n).
  • Space: O(1)O(1) (constant number of 2×22 \times 2 matrices).

Answer

12144907797522336\boxed{12144907797522336}

Code

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

C++ project_euler/problem_936/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef vector<vector<long long>> Mat;
const long long MOD=1e9+7;
Mat mul(Mat&A,Mat&B){
    int n=A.size();
    Mat C(n,vector<long long>(n,0));
    for(int i=0;i<n;i++) for(int k=0;k<n;k++) if(A[i][k])
        for(int j=0;j<n;j++) C[i][j]=(C[i][j]+A[i][k]*B[k][j])%MOD;
    return C;
}
Mat mpow(Mat M,long long p){
    int n=M.size();
    Mat R(n,vector<long long>(n,0));
    for(int i=0;i<n;i++) R[i][i]=1;
    while(p>0){if(p&1) R=mul(R,M); M=mul(M,M); p>>=1;}
    return R;
}
long long fib(long long n){
    if(n<=0) return 0;
    if(n==1) return 1;
    Mat M={{1,1},{1,0}};
    Mat R=mpow(M,n-1);
    return R[0][0];
}
int main(){
    long long N=1000000000000LL;
    long long fn2=fib(N+2), fn3=fib(N+3);
    long long ans=((N%MOD)*fn2%MOD-fn3+2+2*MOD)%MOD;
    cout<<ans<<endl;
}