Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6380 |

@mb1973 | 5642 |

@Quandray | 5233 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3413 |

Complete Leaderboard | |

Handle | Score |

@sanskar94511 | 543 |

@akashverma4 | 335 |

@swastik8750 | 237 |

@nishunishuraj18 | 223 |

@nayan2709 | 211 |

@ajaybhardwaj1999 | 196 |

@surajmamgai | 193 |

@AnshikaPandey | 192 |

@gurshehzadsingh | 188 |

@shardul08 | 185 |

@Rajpra786 | 185 |

Complete Leaderboard |

Consider the generalized Fibonacci number G, which is dependent on a, b and c as follows :-

G(1) = 1, G(2) = 1. G(n) = **a**G(n-1) + **b**G(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 =5Output:4Explanation:G(3) = 3*G(2) + 3*G(1) + 3 = 9%5 = 4

**Example 2:**

Input:a =2,b =2,c =2,n =4,m =100Output:16Explanation: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 <= 10^{9}+7

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Generalised Fibonacci numbers

...