Sequence

 Accepts: 59
 Submissions: 650
 Time Limit: 2000/1000 MS (Java/Others)
 Memory Limit: 65536/65536 K (Java/Others)
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.f​n​​=​⎩​⎨​⎧​​​1,​a​b​​,​a​b​​f​n−1​c​​f​n−2​​,​​​n=1​n=2​otherwise​​

\ \ \ \    He gives you 5 numbers n,a,b,c,p,and he will eat f_nf​n​​ foods.But there are only p foods,so you should tell him f_nf​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    1≤T≤10,1≤n≤10​18​​,1≤a,b,c≤10​9​​,pp is a prime number,and p\le 10^9+7p≤10​9​​+7.

Output

\ \ \ \    Output one number for each case,which is f_nf​n​​ mod p.

Sample Input
  1. 1
  2. 5 3 3 3 233
Sample Output
  1. 190
  1. /*
  2. hdu 5667 BestCoder Round #80 矩阵快速幂
  3.  
  4. F[n] = 1 (n == 1)
  5. F[n] = a^b (n == 2)
  6. F[n] = a^b * F[n-1]^c *F [n-2]
  7.  
  8. 最开始试了下化简公式,但是无果. 也从矩阵快速幂上面考虑过(毕竟 F[n]与 F[n-1],F[n-2]有关)
  9. 但是发现是 乘法运算不知道怎么弄了(2b了)
  10.  
  11. 能够发现运算时基于a的次方的,当a的次方相乘时就变成了他们的次方相加 (好气 TAT)
  12. 于是乎 a^g[n] = a^(b + c*g[n-1] * g[n-2])
  13. 然后用类似快速幂求斐波那契数的方法即可
  14.  
  15. F[n] F[n-1] 1 C 1 0
  16. F[n-1] F[n-2] 1 * 1 0 0
  17. b 0 1
  18. hhh-2016-04-18 20:36:40
  19. */
  20. #include <iostream>
  21. #include <cstdio>
  22. #include <cstring>
  23. #include <vector>
  24. #include <map>
  25. #include <algorithm>
  26. #include <functional>
  27. #include <math.h>
  28. using namespace std;
  29. #define lson (i<<1)
  30. #define rson ((i<<1)|1)
  31. typedef long long ll;
  32. const int maxn = ;
  33.  
  34. struct Matrix
  35. {
  36. ll ma[][];
  37. Matrix()
  38. {
  39. memset(ma,,sizeof(ma));
  40. }
  41. };
  42.  
  43. Matrix mult(Matrix ta,Matrix tb, ll mod)
  44. {
  45. Matrix tc;
  46. for(int i = ; i < ; i++)
  47. {
  48. for(int j = ; j < ; j++)
  49. {
  50. tc.ma[i][j] = ;
  51. for(int k = ; k < ; k++){
  52. tc.ma[i][j] += (ta.ma[i][k] * tb.ma[k][j])%mod;
  53. tc.ma[i][j] %= mod;
  54. }
  55. }
  56. }
  57. return tc;
  58. }
  59.  
  60. Matrix Mat_pow(Matrix ta,ll n,ll mod)
  61. {
  62. Matrix t;
  63. for(int i = ; i < ; i++)
  64. t.ma[i][i] = ;
  65. while(n)
  66. {
  67. if(n & ) t = mult(t,ta,mod);
  68. ta = mult(ta,ta,mod);
  69. n >>= ;
  70. }
  71. return t;
  72. }
  73.  
  74. ll pow_mod(ll a,ll n,ll mod)
  75. {
  76. ll t = ;
  77. a %= mod ;
  78. while(n)
  79. {
  80. if(n & ) t = t*a%mod;
  81. a = a*a%mod;
  82. n >>= ;
  83. }
  84. return t;
  85. }
  86.  
  87. Matrix mat;
  88. Matrix an;
  89. ll a,b,c;
  90. void ini(ll mod)
  91. {
  92. mat.ma[][] = c,mat.ma[][] = ,mat.ma[][] = ;
  93. mat.ma[][] = ,mat.ma[][] = ,mat.ma[][] = ;
  94. mat.ma[][] = b,mat.ma[][] = ,mat.ma[][] = ;
  95.  
  96. an.ma[][] = (b+b*c%mod)%mod,an.ma[][] = b,an.ma[][] = ;
  97. an.ma[][] = b,an.ma[][] = ,an.ma[][] = ;
  98. }
  99. ll mod,n;
  100.  
  101. int main()
  102. {
  103. int T;
  104. scanf("%d",&T);
  105. while(T--)
  106. {
  107. scanf("%I64d%I64d%I64d%I64d%I64d",&n,&a,&b,&c,&mod);
  108. a%=mod,b%=mod,c%=mod;
  109. ini(mod-);
  110. if(n == )
  111. {
  112. printf("1\n");
  113. }
  114. else if(n == )
  115. printf("%I64d\n",pow_mod(a,b,mod));
  116. else
  117. {
  118. mat = Mat_pow(mat,n-,mod-);
  119. mat = mult(an,mat,mod-);
  120. ll ci = mat.ma[][];
  121. //cout << ci <<endl;
  122. printf("%I64d\n",pow_mod(a,ci,mod));
  123. }
  124. }
  125. return ;
  126. }

hdu 5667 BestCoder Round #80 矩阵快速幂的更多相关文章

  1. hdu 5607 BestCoder Round #68 (矩阵快速幂)

    graph  Accepts: 9 Submissions: 61  Time Limit: 8000/4000 MS (Java/Others)  Memory Limit: 65536/65536 ...

  2. hdu 4686 Arc of Dream(矩阵快速幂)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY ...

  3. HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...

  4. HDU - 4990 Reading comprehension 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...

  5. HDU 1005 Number Sequence:矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意: 数列{f(n)}: f(1) = 1, f(2) = 1, f(n) = ( A*f(n ...

  6. HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...

  7. HDU 6470:Count(矩阵快速幂)

    Count Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  8. hdu 1575 Tr A(矩阵快速幂)

    今天做的第二道矩阵快速幂题,因为是初次接触,各种奇葩错误整整调试了一下午.废话不说,入正题.该题应该属于矩阵快速幂的裸题了吧,知道快速幂原理(二进制迭代法,非递归版)后,剩下的只是处理矩阵乘法的功夫了 ...

  9. hdu 4565 So Easy!(矩阵+快速幂)

    题目大意:就是给出a,b,n,m:让你求s(n); 解题思路:因为n很可能很大,所以一步一步的乘肯定会超时,我建议看代码之前,先看一下快速幂和矩阵快速幂,这样看起来就比较容易,这里我直接贴别人的推导, ...

随机推荐

  1. 1013团队Beta冲刺day1

    项目进展 李明皇 今天解决的进度 点击首页list相应条目将信息传到详情页 明天安排 优化信息详情页布局 林翔 今天解决的进度 前后端连接成功 明天安排 开始微信前端+数据库写入 孙敏铭 今天解决的进 ...

  2. 201621123031 《Java程序设计》第3周学习总结

    Week03-面向对象入门 1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联系 ...

  3. java连接jdbc Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by defa

    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp_db","root",& ...

  4. .NET Core/.NET之Stream简介

    之前写了一篇C#装饰模式的文章提到了.NET Core的Stream, 所以这里尽量把Stream介绍全点. (都是书上的内容) .NET Core/.NET的Streams 首先需要知道, Syst ...

  5. django报错Manager isn't accessible via UserInfo instances

    出现这种错误是因为调用模型对象时使用了变量名,而不是对象名(模型类),例如: user = UserInfo()user_li = user.objects.filter(uname=username ...

  6. 怎么用DreamWare新建立一个静态网站的站点

    可以上面的图可以看出首先需要用F8确定这个文件是勾选的,然后在D盘建立"华为"文件夹,然后在里面建js,css,image文件夹,然后在DW里面点击站点 然后点击管理站点,有一个新 ...

  7. Python内置函数(32)——all

    英文文档: all(iterable) Return True if all elements of the iterable are true (or if the iterable is empt ...

  8. restful架构风格设计准则(五)用户认证和session管理

    读书笔记,原文链接:http://www.cnblogs.com/loveis715/p/4669091.html,感谢作者! Authentication REST提倡无状态约束,这就要求:用户状态 ...

  9. Django 相关

    Web框架本质 其实所有的Web应用本质就是一个socket服务端,而用户的浏览器就是一个socket客户端.简单的socket代码如下: import socket sk = socket.sock ...

  10. spring data redis template GenericJackson2JsonRedisSerializer的使用

    配置 <!-- redis template definition --> <bean id="myRedisTemplate" class="org. ...