how recurrence solved using matrix(nth number) FN = FN-1 + FN-2 + FN-1×FN-2
the function is given as above 
I want to know how this one works for finding the nth element because the complexity of this algorithm is log(base2)N? SO far fastest for this question. Two matrix of 2*2 size is used and mod is some number since answer is asked in modulus of that given number.
long long int A[2][2] = {{1,1},{1,0}};#this is a matrix
n = n-1;
long long int B[2][2] = {{1,0},{0,1}};#this is a matrix
while(n>0)#repeat the following procedure until n becomes 0
{
    if(n%2==1)#when is odd
        mult(B,A);
    n = n/2;
    mult(A,A);
}
 long long int result = ((power(pp+1,B[0][1])*power(p+1,B[0][0]))%mod -     1 + mod)%mod;
 printf("%lld\n",result);
void mult(long long int A[2][2],long long int B[2][2])
{
 long long int C[2][2];
 C[0][0] = A[0][0]*B[0][0] + A[0][1]*B[1][0];
 C[0][1] = A[0][0]*B[0][1] + A[0][1]*B[1][1];
 C[1][0] = A[1][0]*B[0][0] + A[1][1]*B[1][0];
 C[1][1] = A[1][0]*B[0][1] + A[1][1]*B[1][1];
 A[0][0] = C[0][0]%(mod-1);
 A[0][1] = C[0][1]%(mod-1); #mod is some no since answer is asked in mod 
 A[1][0] = C[1][0]%(mod-1);
 A[1][1] = C[1][1]%(mod-1);

}
 A: Here is part of the answer to your question. Suppose you want to compute the $n^{th}$ term of the recursive relation
$$
f(n)=f(n-1)+f(n-2)\quad \mbox{with}\quad f(0)=0,f(1)=1
$$ 
Then the following procedure achieves this:

Input: $n$
$A:=\pmatrix{0& 1\\1&1}$
$B:=\pmatrix{1& 0\\0&1}$
While $n>0$ do:
$\quad$ If $n$ is odd:
$\quad$$\quad$ $B:=BA$
$\quad$ $A:=A^2$
$\quad$ $n:=\left \lfloor\frac{n}{2} \right \rfloor$
Return $b_{22}$ (i.e., the bottom right element of matrix $B$)

To show this you can proceed in two steps:


*

*First, notice that that you can rewrite the recursive equation in a matrix form:
$$
\pmatrix{f(n-1)\\f(n)}=\pmatrix{0&1\\1&1}\pmatrix{f(n-2)\\f(n-1)}
$$
Indeed, if you expand the right hand side, you will get 
$$
\pmatrix{f(n-1)\\\color{red}{f(n)}}=\pmatrix{0\cdot f(n-2)+1\cdot f(n-1)\\\color{red}{1\cdot f(n-2)+1\cdot f(n-1)}}
$$
You can rewrite this equation as 
$$
F_n=AF_{n-1}
$$
where $F_n=\pmatrix{f(n-1)\\f(n)}$. 

*Then, show by induction on $n$ (straightforward) that:
$$
F_n=A^nF_0=\pmatrix{0&1\\1&1}^n\pmatrix{0\\1}
$$
In other words, the bottom right element of $A^n$ equals $f(n)$.

*Finally, show that the above procedure computes $A^n$, and thus returns the bottom right element of $A^n$ (or $f(n)$). Note that at each iteration $BA^n$ is actually constant. Indeed, if $n$ is odd: 
$$n:=\left \lfloor\frac{n}{2} \right \rfloor=\frac{n-1}{2},\;B:=BA,\;A:=A^2\quad \Rightarrow\quad BA^n:=(BA)(A^2)^{\frac{n-1}{2}}=BA^n
$$
And if $n$ is even:
$$
n:=\left \lfloor\frac{n}{2} \right \rfloor=\frac{n}{2},\;B:=B,\;A:=A^2\quad \Rightarrow\quad BA^n:=(B)(A^2)^{\frac{n}{2}}=BA^n
$$
We can now show that at the end of the procedure, $B=A^n$. When entering the While loop for the first time, $BA^n = A^n$. And for the last time, $BA^n=BA^0=B$. Since we showed that $BA^n$ is constant, we have $B=A^n=\pmatrix{0&1\\1&1}^n$. 

A: The code shown is based on a well-known algorithm for taking arbitrary positive integer powers of anything that can be multiplied and added, such as real numbers or matrices.
The algorithm in turn is based on the binary representation of an integer:
$$n=d_k 2^k + d_{k-1} 2^{k-1} + \cdots 
+ d_2 2^2 + d_1 2 + d_0,$$
where each "digit" $d_i$ is either $0$ or $1$.
The algorithm also uses the fact that
$x^{p+q}=x^px^q.$
Hence
$$x^n=x^{d_k 2^k}x^{d_{k-1} 2^{k-1}}\cdots 
x^{d_2 2^2}x^{d_1 2}x^{d_0}.$$
In order to compute this product, we start with the identity and multiply the terms of the product from right to left.
For each term, to get $x^{2^i}$ we simply square $x^{2^{i-1}}$. 
Then if $d_i=1,$ we multiply by $x^{2^i}$, but if $d_i=0$, then $x^{d_i 2^i}$ is the identity so we do nothing (which gives the same result as multiplying by the identity).
Repeatedly dividing $n$ by $2$ and testing whether the result is even or odd is how we determine whether each of the digits $d_i$ is $0$ or $1.$
