Ensure that you are logged in and have the required permissions to access the test.
A linear recurrence relation is a function or a sequence such that each term is a linear combination of previous terms. Each term can be described as a function of the previous terms.
A famous example is the Fibonacci sequence: f(i) = f(i-1) + f(i-2)
Linear means that the previous terms in the definition are only multiplied by a constant (possibly zero) and nothing else. So, this sequence f(i) = f(i-1) * f(i-2) is not a linear recurrence.
Given a function f(x) defined as a linear recurrence relation. Compute f(N) ,where N is a large number .
The solution is broken down into 4 steps :
What remains is how to compute TN-1 efficiently. The most popular method is to use exponentiation by squaring method that works in O(log N) time, with this recurrence:
Multiplying two matrices takes O(K3) time using standard method, so the overall time complexity to solve a linear recurrence is O(K3 log N).
Here is a sample C++ code to compute the N-th term of Fibonacci sequence, modulo 1,000,000,007.
#include<iostream>
#include<vector>
#define REP(i,n) for (int i = 1; i <= n; i++)
using namespace std;
typedef long long ll;
typedef vector<vector<ll> > matrix;
const ll MOD = 1000000007;
const int K = 2;
// computes A * B
matrix mul(matrix A, matrix B)
{
matrix C(K+1, vector<ll>(K+1));
REP(i, K) REP(j, K) REP(k, K)
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
return C;
}
// computes A ^ p
matrix pow(matrix A, int p)
{
if (p == 1)
return A;
if (p % 2)
return mul(A, pow(A, p-1));
matrix X = pow(A, p/2);
return mul(X, X);
}
// returns the N-th term of Fibonacci sequence
int fib(int N)
{
// create vector F1
vector<ll> F1(K+1);
F1[1] = 1;
F1[2] = 1;
// create matrix T
matrix T(K+1, vector<ll>(K+1));
T[1][1] = 0, T[1][2] = 1;
T[2][1] = 1, T[2][2] = 1;
// raise T to the (N-1)th power
if (N == 1)
return 1;
T = pow(T, N-1);
// the answer is the first row of T . F1
ll res = 0;
REP(i, K)
res = (res + T[1][i] * F1[i]) % MOD;
return res;
}