Submit | All submissions | Best solutions | Back to list |
FIBTWIST - Fibonacci With a Twist |
Fibonacci numbers are given by
- f(n) = f(n-1) + f(n-2)
with f(0) = 0 and f(1) = 1.
first number of series — 0 1 1 2 3 5 8 13
Now let's have a new series called "Fibonacci Twist" which is given by
- ft(n) = ft(n-1) + ft(n-2) + (n-1)
with ft(0) = 0 and ft(1) = 1.
with first few number in the series — 0 1 2 5 10 19 34 59
Now your task is to find ft(n).
Since the number can be big you have to find the result mod M.
Input
first line having single number 't' — number of test cases.
Next t lines have 2 number each 'n' and 'M'
Output
Single number given the n-th term mod M
Example
Input: 3 5 20 10 77 15 111 Output: 19 45 69
Constraints
- 10 ≤ t ≤ 100
- 0 ≤ n ≤ 109
- 100 ≤ M ≤ 109
Explanation
- ft(5) is 19. 19 % 20 = 19
- ft(10) is 276. 276 % 77 = 45
- ft(15) is 3177. 3177 % 111 = 69
Added by: | Devil D |
Date: | 2012-04-19 |
Time limit: | 0.100s-1s |
Source limit: | 20000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 |
Resource: | Own |
hide comments
|
||||||||
2015-03-03 16:30:19 #include
got wa for n=0. AC now |
||||||||
2014-12-31 10:42:51 Abhishek
For having WA at (7) , Check your test case against big numbers like 1 1000000 1000007 check overflow! |
||||||||
2014-12-29 13:45:56 Manraj Singh
O(log(N)) solution.Check for n=0 and n=1.Costed me 2 WAs. |
||||||||
2014-12-06 07:00:54 Pawankumar P
4 WAs for MOD. Fibonacci sequence have so many secrets, good problem. :) |
||||||||
2014-10-03 20:54:15 NOVICE
very similar to FIBOSUM/// |
||||||||
2014-09-03 14:54:45 Devashish
@shreya sahu ... 1000000000 you declaring a array of long long this size it is 8 GB!!!!! btw normal method(even with dp) will lead to memory overflow. The pyramid cluster 733MHz can approx. do 10^6 0r 10^7 calculations per sec. .. |
||||||||
2014-08-08 12:49:52 shreya sahu
runtime error. :( <snip> Last edit: 2023-05-16 20:33:00 |
||||||||
2014-07-01 12:03:03 sobriquet
Learned a lot of new things.If getting WA, use MOD carefully.Very good problem. |
||||||||
2014-05-31 18:05:26 Rahul Ranjan
getting WA at test case 7....help pls |