FIBOFUN - Fun with Fibonacci Series
Fibonacci series is a series in which every element is sum of previous 2 elements.
The first 2 elements are 0, 1 and the series goes like 0, 1, 1, 2, 3, 5, 8, 13...
What if you were given 2 random numbers as the starting of the series and you follow the same rule as the Fibonacci rule?
For example, if you were given 2 and 2 .. the series would become:
2 2 4 6 10 16 26...
Now your task is simple...
You will be given 2 numbers a and b - the first and second term of the series.
You need to calculate the sum of first n numbers of the series so formed.
Since the numbers can be big you need to print the result mod some number 'M' provided in the input.
Input
The first line will have single number 't' - number of test cases.
Each test case will have 4 numbers a, b, n and M.
a - first number of the series.
b - second number of the series.
n - calculate the sum for n terms.
M - give the result mod M.
Output
single number for each case - sum of n terms mod M.
Example
Input: 2 2 2 10 21 1 3 10 21 Output: 13 4
Explanation
The first case series is 2 2 4 6 10 16 26 42 68 110, sum is 286, result is 286 % 21 = 13.
Constraints
Number of test cases ≤ 100.
0 ≤ a, b, m ≤ 108.
1 ≤ n ≤ 108.
hide comments
|
Shubham Jadhav:
2017-05-12 04:10:06
AC in one go :) |
|
Ankur Singh:
2016-07-17 08:38:03
Don't forget to take the MOD, even for small cases of n like 1, 2. |
|
Satyam Mishra:
2015-06-13 10:05:20
Nice problem..
|
|
__hk__:
2015-06-09 11:57:26
Finally AC..!!
|
|
ALISHA:
2014-12-24 21:30:20
Never matters whether it's in tutorial or classical section. All that matters is the bliss of getting green band in first attempt.Rest is all greed my friend.:P Last edit: 2014-12-24 21:31:14 |
|
Francky:
2014-01-24 18:33:28
I very well know Fibonacci variation's problems here at spoj, and I can confirm that this one don't give new materials. It is a tutorial problem for several reasons. I don't say it is an easy problem. Please trust EB members in their actions ; stop asking for classical. |
|
Somesh Maurya™:
2014-01-24 17:01:23
C'mon this isn't a tutorial problem... Move it to classical please... |
|
Somesh Maurya™:
2014-01-24 17:00:31
What if m=0?? |
|
Arika Saputro:
2013-05-09 13:47:29
why my code giving WA?
|
|
:D:
2012-08-28 08:29:04
I agree that there are other similar groups of problems, but Fibonacci really been done to death, often in harder variations (just search for Fibonacci). On top of that there's a challenge version:
|
Added by: | Devil D |
Date: | 2012-03-15 |
Time limit: | 1s |
Source limit: | 10000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | Own |