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
:.Mohib.::
2015-07-21 16:29:16
@Lakshman, long long is enough to store the value of b in c language?? Plzz help....
|
|
ivar.raknahs:
2015-07-19 17:42:46
@[Lakshman]: Could you please add a link to the powfib version so that the users who find this question hard at first ,may try the easier version? It's fully up to you. Last edit: 2015-07-20 09:05:58 |
|
[Lakshman]:
2015-07-19 11:58:06
Now Time Limit is 2* my slow python solution.
|
|
Francky:
2015-07-19 11:21:12
@Wisfaq : well explained. Thanks.
|
|
wisfaq:
2015-07-19 09:44:58
@Lakshman: as you don't state that M is prime it's necessary to factor M.
|
|
[Lakshman]:
2015-07-19 04:39:33
@Francky may I know the reason of hiding this problem and how this problem is related with FACT1. Please explain. Last edit: 2015-07-19 13:49:53 |
|
Francky:
2015-07-18 22:10:53
I think there's enough "(a^b) % M" problems, and here it's again a FACT1 dependant one, the other parts are quite obvious. Problem hidden. |
|
:.Mohib.::
2015-07-18 20:48:00
@Lakshman can you please give some hint about how to handle overflow for the value of b while calculating (a^b)% MOD??Thanks.... Last edit: 2015-07-18 21:08:45 |
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 |