Fibonacci in Log (n) without using Matrix Exponentiation

As for calculating the n we can express it as the product of n/2 and n/2 ((n/2+1) for n is odd) as they are independent ,now as there are the cases when we combining these two half values to get full length there may be occurrence of consecutive 1’s at the joining. So we have to subtract those cases to get the final result.

f(n)=f(n/2)*f(n/2) – (those values which has consecutive 1’s at the joining part)

Below is the implementation of the above approach:

C#




using System;
using System.Collections.Generic;
 
class Solution {
    // Dictionary to store memoized values
    static Dictionary<long, long> mp = new Dictionary<long, long>();
 
    // Recursive function to count strings
    public static long countStrings(long N) {
        // Base cases:
        if (N == 0) return 1;
        if (N == 1) return 2;
        if (N == 2) return 3;
         
        long mod = (long)1e9 + 7;
         
        // Check if the value for N is already memoized
        long a = mp.ContainsKey(N / 2 - 1) ? mp[N / 2 - 1] : countStrings(N / 2 - 1);
        long b = mp.ContainsKey(N / 2) ? mp[N / 2] : countStrings(N / 2);
        long c = mp.ContainsKey(N / 2 + 1) ? mp[N / 2 + 1] : countStrings(N / 2 + 1);
         
        // Calculations based on whether N is even or odd
        if (N % 2 == 1) {
            // Memoize and return result for odd N
            return mp[N] = ((b * c % mod) - ((c - b) * (b - a) % mod) + mod) % mod;
        } else {
            // Memoize and return result for even N
            return mp[N] = ((b * b % mod) - ((b - a) * (b - a) % mod) + mod) % mod;
        }
    }
}
 
class Program {
    static void Main(string[] args) {
        long N = 10;
        Console.WriteLine(Solution.countStrings(N));
    }
}


Output

144

Time Complexity: O(log N)
Auxiliary Space: O(log N)

Please refer complete article on Count number of binary strings without consecutive 1’s for more details!



C# Program to Count number of binary strings without consecutive 1’s

Write a C# program for a given positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1s.

Examples:

Input: N = 2
Output: 3
Explanation: The 3 strings are 00, 01, 10

Input: N = 3
Output: 5
Explanation: The 5 strings are 000, 001, 010, 100, 101

Similar Reads

C# Program to Count a number of binary strings without consecutive 1’s using Dynamic Programming:

...

Fibonacci in Log (n) without using Matrix Exponentiation:

Let a[i] be the number of binary strings of length i that do not contain any two consecutive 1s and which end in 0. Similarly, let b[i] be the number of such strings which end in 1. We can append either 0 or 1 to a string ending in 0, but we can only append 0 to a string ending in 1. This yields the recurrence relation: a[i] = a[i – 1] + b[i – 1]b[i] = a[i – 1] The base cases of above recurrence are a[1] = b[1] = 1. The total number of strings of length i is just a[i] + b[i]....

Contact Us