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

Ipad,IPhone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 484    Accepted Submission(s): 211


Problem Description
In ACM_DIY, there is one master called ¡°Lost¡±. As we know he is a ¡°-2Dai¡±, which means he has a lot of money.
  

Well, Lost use Ipad and IPhone to reward the ones who solve the following problem.
  

In this problem, we define F( n ) as :
  

Then Lost denote a function G(a,b,n,p) as

Here a, b, n, p are all positive integer!
If you could tell Lost the value of G(a,b,n,p) , then you will get one Ipad and one IPhone!
 

Input
The first line is one integer T indicates the number of the test cases. (T <= 100)
Then for every case, only one line containing 4 positive integers a, b, n and p.
(1 ¡Üa, b, n, p¡Ü2*109 , p is an odd prime number and a,b < p.)
 

Output
Output one line,the value of the G(a,b,n,p) .
 

Sample Input
4 2 3 1 10007 2 3 2 10007 2 3 3 10007 2 3 4 10007
 

Sample Output
40 392 3880 9941
 

Author
AekdyCoin
 

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-11-26 04:03:46, Gzip enabled