POWFIB2 - Fibo and non fibo(Hard)
The problem is simple. Find (a^b) % MOD.
where, a = Nth non-Fibonacci number, b = Nth Fibonacci number.
Consider Fibonacci series as 1, 1, 2, 3 ...
Input
First line contains T, the number of test cases. Each of the next T lines contains two numbers N and M where M is a prime number.
Output
Print T lines of output where each line corresponds to the required answer.
Example
Input: 3 3 1000000007 2 13 1 13 Output: 49 6 4
Explanation
For N = 3: 3rd non Fibonacci number = 7, 3rd Fibonacci number = 2. Answer = (7^2) % 1000000007 = 49
For N = 2: 2nd non Fibonacci number = 6, 2nd Fibonacci number = 1. Answer = (6^1) % 13 = 6
For N = 1: 1st non Fibonacci number = 4, 1st Fibonacci number = 1. Answer = (4^1) % 13 = 4
If you are getting TLE here you may try easy version of this POWFIB.
Constraints
1 <= T <= 50000
1 <= N <= 1e16
2 <= M <= 1e18, and M is a prime number.
Note:: My best time with C++ with fully optimized code is 1.18s.
Have fun :)
hide comments
surya97:
2015-11-04 03:14:18
@Lakshman now i have changed my fibonacci calculation algo. But i got WA in test case 4. So can you tell me whether that algo is right and a small modification will get it accepted?
|
|
surya97:
2015-11-03 12:17:53
@lakshman is the mulmod you sent was multiplication using repetitive modular addition ?
|
|
surya97:
2015-11-03 04:43:02
I think this question is above my level. I have implemented modular multiplication
|
|
surya97:
2015-11-01 16:23:56
@Lakshman , i have changed the modular exponentiation function. Still giving WA after 4th testcase. Is the function still causing overflow?
|
|
surya97:
2015-11-01 05:52:02
Is the overflow occuring in calculating nth fibonacci or nth non fibonacci or modular exponentiation? @Lakshman please could you tell me where it is happening?
|
|
surya97:
2015-10-30 17:04:19
why my solution is taking 256M and i could not get why it is failing after test case 4. @Lakshman can you see my code and tell me if the functions i used are suitable or not?
|
|
:.Mohib.::
2015-07-22 19:30:05
Is the answer is
|
|
:.Mohib.::
2015-07-22 17:01:29
Not able to find it....can you help me??...:(
|
|
:.Mohib.::
2015-07-22 12:39:39
Last edit: 2015-07-22 20:06:59 |
|
Pulkit Singhal:
2015-07-21 18:18:57
Easy One :)
|
Added by: | [Lakshman] |
Date: | 2015-07-18 |
Time limit: | 10s |
Source limit: | 50000B |
Memory limit: | 1536MB |
Cluster: | Cube (Intel G860) |
Languages: | All except: ASM64 JS-MONKEY |
Resource: | POWFIB |