F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3665    Accepted Submission(s): 1199


Problem Description
$\ \ \ \ $Holion August will eat every thing he has found.

$\ \ \ \ $Now there are many foods,but he does not want to eat all of them at once,so he find a sequence.

$f_n=\left\{\begin{matrix}
1 ,&n=1 \\
a^b,&n=2 \\
a^bf_{n-1}^cf_{n-2},&otherwise
\end{matrix}\right.$

$\ \ \ \ $He gives you 5 numbers n,a,b,c,p,and he will eat $f_n$ foods.But there are only p foods,so you should tell him $f_n$ mod p.
 

Input
$\ \ \ \ $The first line has a number,T,means testcase.

$\ \ \ \ $Each testcase has 5 numbers,including n,a,b,c,p in a line.

$\ \ \ \ 1\le T \le 10,1\le n\le 10^{18},1\le a,b,c\le 10^9$,$p$ is a prime number,and $p\le 10^9+7$.
 

Output
$\ \ \ \ $Output one number for each case,which is $f_n$ mod p.
 

Sample Input
1 5 3 3 3 233
 

Sample Output
190
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-04-27 10:59:44, Gzip enabled