现在的位置: 首页 > 综合 > 正文

hdu3802 Ipad,IPhone

2018年04月23日 ⁄ 综合 ⁄ 共 2937字 ⁄ 字号 评论关闭

Ipad,IPhone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Total Submission(s): 0    Accepted Submission(s): 0



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*10
9
 , 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

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

ACM-DIY Group Contest 2011 Spring

Recommend

notonlysuccess

先膜拜下AC

1004
……

这题错了好多次……orz
,没有注意到负数的取余,表示遗憾。

首先应该知道这么个公式

感谢zsasuke提出的错误,后来查了一下,这个公式的使用是有前提条件的:“c>phi(p)”,所以需要加个判断,如果c<=phi(p),说明c次方在O(logc)的范围内可以得出结果,因此可以直接暴力求解,大于的话才使用这个公式,对于这个公式的正确性我这几天试着证明下,是在别人的解题报告里看的这个公式,做FOJ1759的时候记下来的,并没有证明……

还有p是质数(题目给定的,没发现,感谢bstw111提出)所以phi(p)=p-1。

 

所以Fib(n)
次方就可以处理了。

对于后面那坨……



注意对于zn
的矩阵可能系数为负的,不能直接取模……

代码如下,表示写的很丑- -|||


 

【上篇】
【下篇】

抱歉!评论已关闭.