Generalised Fibonacci numbers
Submissions: 1919   Accuracy:

12.57%

  Difficulty: Hard   Marks: 8

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.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains five space separated values a, b, c, n, m respectively.

Output:
For each test case in a new line print the required output denoting G(n).

Constraints:
1<=T<=100
1<=a, b, c, n, m<=10^9+7

Example:
Input:

2
3 3 3 3 5
2 2 2 4 100
Output:
4
16

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Sanket yadav
Author: Shubham Joshi 1


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.