FIBOSQRT - Fibonacci With a Square Root

no tags 

FIBONACCI is the recursive sequence that is given by: F(n)=F(n-1)+F(n-2) with F(0)=0 and F(1)=1.

In this problem we define FIBOSQRT that is given by: Fs(n)=Fs(n-1)+Fs(n-2)+2*SQRT(3+Fs(n-1)*Fs(n-2)) with Fs(0) and Fs(1) are given in the input file.

It's guaranteed that SQRT(3+Fs(n-1)*Fs(n-2)) is always an integer. Wink I've proved it by math theorem.

Now your task is to find Fs(n). Since the number can be big you have to find the result mod M.

Input

The first line is an integer T(1 ≤ T ≤ 111,111), denoting the number of test cases. Then, T test cases follow.

For each test case, there are four integers Fs(0), Fs(1) (1 ≤ Fs(0) ≤ Fs(1) < 106), M (1 ≤ M < 109), and n (0 ≤ n < 1018) written in one line, separated by space.

Output

For each test case, output Fs(n) mod M.

Example

Input:
2
1 1 10 5
2 3 100 6

Output:
4
82

Explanation:

Case #1:
  • Fs(0)=1
  • Fs(1)=1
  • Fs(2)=1+1+2*SQRT(3+1*1)=6
  • Fs(3)=6+1+2*SQRT(3+6*1)=13
  • Fs(4)=13+6+2*SQRT(3+13*6)=37
  • Fs(5)=37+13+2*SQRT(3+37*13)=94

The answer is: 94 mod 10 = 4.

Case #2:
  • Fs(0)=2
  • Fs(1)=3
  • Fs(2)=3+2+2*SQRT(3+3*2)=11
  • Fs(3)=11+3+2*SQRT(3+11*3)=26
  • Fs(4)=26+11+2*SQRT(3+26*11)=71
  • Fs(5)=71+26+2*SQRT(3+71*26)=183
  • Fs(6)=183+71+2*SQRT(3+183*71)=482

The answer is: 482 mod 100 = 82.

Notes

File #1: More than 100,000 random test cases (test your program speed Laughing)

File #2: Less than 10 test cases (tricky test cases that might give you WA Tongue out)

Time Limit ≈ 8*(My Program Top Speed)

Warning: large Input/Output data, be careful with certain languages

See also: Another problem added by Tjandra Satria Gunawan


hide comments
Mitch Schwartz: 2013-07-03 11:58:09

Getting AC in Python is possible but hard, with the techniques I know.

wisfaq: 2013-07-01 18:54:04

I'm glad I could provide a solution in something else than C or the like.
It would have been much nicer if the problem had been solvable in Python.
(The same algo that got AC with fpc TLE'd with Python)
Who takes up the glove and solves this one in Python?
Btw: I couldn't find tricky cases but for the usual nuisance of this kind of programs in languages like C or Pascal

Last edit: 2013-07-01 18:59:33
infinite: 2013-06-10 23:43:39

what is the ans for:
1 1 1000000007 10^18

Damian Straszak: 2012-10-16 21:11:27

Or alternatively: optimize your code ;)

Tadek Dul: 2012-10-13 09:14:56

Choose your compiler carefully - my solution has TLE on C++ 4.0.0.8, but it has AC on C++ 4.3.2

Mitch Schwartz: 2012-09-02 02:35:53

@Tjandra: Thanks, found an interesting property specific to the problem compared with similar problems, and it could be faster... :p

(Tjandra Satria Gunawan)(曾毅昆): 2012-09-01 11:59:33

@Mitch Schwartz: Wow, super solution :-D Your program ~3x faster than my program. Congratulations, you're awesome programmer.


Added by:Tjandra Satria Gunawan
Date:2012-08-30
Time limit:1.063s-6.147s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Own Problem