Generalised Fibonacci numbers | Practice | GeeksforGeeks

Consider the generalized Fibonacci number G, which is dependent on a, b and c as follows :-
G(1) = 1, G(2) = 1. G(n) = aG(n-1) + bG(n-2) + c.
Your task is to calculate G(n)%m for given values of n and m.
 
Example 1:
Input:
a = 3, b = 3, c = 3, n = 3, m = 5
Output:
4
Explanation:
G(3) = 3*G(2) + 3*G(1) + 3 = 9%5 = 4
Example 2:
Input:
a = 2, b = 2, c = 2, n = 4, m = 100
Output:
16
Explanation:
G(3) = 2*G(2) + 2*G(1) + 2 = 6
G(4) = 2*G(3) + 2*G(2) + 2 = 16%100 = 16

 
Your Task:
You don't need to read input or print anything. Your task is to complete the function genFibNum() which takes 5 Integers a, b, c, n, and m as input and returns G(n)%m.
 
Expected Time Complexity: O(logn)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= a,b,c,n,m <= 109+7
-1
dattatraygujar771 month ago
static long genFibNum(Long a, Long b, Long c, long n, long m) {
        long fib1=1;
        long fib2=1;
        long fib3 =0;
        if(n<=2)
        {
           return (a*fib2+b*fib1+c)%m;
        }
        for(int i=3;i<=n;i++)
        {
           fib3 =(a*fib2+b*fib1+c);
           fib1=fib2;
           fib2=fib3;
        }
        return fib3%m;
    }
0
Lakshay Thakur1 year ago
Lakshay Thakur
kya bekaar question hai
0
Jainav2 years ago
Jainav
Why segmentation fault:
 https://uploads.disquscdn.c...
0
RAJ PATEL2 years ago
RAJ PATEL
Simple C++ solhttps://ide.geeksforgeeks.o...
0
Kartikk Garg3 years ago
Kartikk Garg
is my code coorect for all inputs it gives correct ansere for the given two inputs but it exceeds the time limit
#include <stdio.h>
int main() {    //code    int T,i,a,b,c,n,l,m,t,t1,t2;    scanf("%d\n",&T);    for(l=1;l<=T;l++)    {        scanf("%d\t%d\t%d\t%d\t%d\n",&a,&b,&c,&n,&m);        t1=1;        t2=1;        for(i=3;i<=n;i++)        {            t=a*t1+b*t2+c;            t2=t1;            t1=t;        }
        printf("%d\n",t%m);    }    return 0;}
0
Sharvesh. S3 years ago
Sharvesh. S
the same code works in 0.02 sec in gfg ide. it shows tle here.
https://ide.geeksforgeeks.o...
0
john wick3 years ago
john wick
c++ code https://ide.geeksforgeeks.o...
0
Pratik Das Baghel3 years ago
Pratik Das Baghel
https://ide.geeksforgeeks.o...Help me, the code is running on codeblocks but here it's showing segementation fault.
0
Richi4 years ago
Richi
Can someone pls pls pls tell me why is my code not working for large inputs even when I took care of %m ??:
https://ide.geeksforgeeks.o...
0
Krishna Kothoor4 years ago
Krishna Kothoor
I am getting Runtime ErrorException in thread "main" java.lang.OutOfMemoryError: Java heap space. Below is my code in Java. Could someone please help me understand how to resolve this issue?https://ide.geeksforgeeks.o...
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.