• Tutorials
  • DSA
  • Data Science
  • Web Tech
  • Courses
March 13, 2024 |1.4K Views

PROBLEM OF THE DAY : 12/03/2024 | Generalised Fibonacci numbers

Description
Discussion

Welcome to the daily solving of our PROBLEM OF THE DAY with Devashish Khare. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Number Theory but also build up problem-solving skills.

In this problem, we are given the generalized Fibonacci number g, which is dependent on a, b and c as follows :-
g(1) = 1, g(2) = 1. For any other number n, g(n) = a*g(n-1) + b*g(n-2) + c.

For a given value of m, determine g(n)%m.

Example :

Input:
a = 3
b = 3
c = 3
n = 3
m = 5
Output:
4

Explanation:
g(1) = 1 and g(2) = 1 
g(3) = 3*g(2) + 3*g(1) + 3 = 3*1 + 3*1 + 3 = 9
We need to return answer modulo 5, so 9%5 = 4, is the answer.

Give the problem a try before going through the video. All the best!!!
Problem Link: https://www.geeksforgeeks.org/problems/generalised-fibonacci-numbers1820/1