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);
}