hdu3802 Ipad,IPhone

本文解析了一道ACM_DIY竞赛中的数学问题,涉及矩阵乘法、斐波那契数列及模运算等内容。文章给出了详细的解题思路与步骤,并提供了完整的代码实现。

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 的矩阵可能系数为负的,不能直接取模……

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

 

评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值