题目链接:https://vjudge.net/problem/UVA-10870

题意:

典型的矩阵快速幂的运用。比一般的斐波那契数推导式多了几项而已。

代码如下:

  1. #include <bits/stdc++.h>
  2. #define rep(i,s,t) for(int (i)=(s); (i)<=(t); (i)++)
  3. #define ms(a,b) memset((a),(b),sizeof((a)))
  4. using namespace std;
  5. typedef long long LL;
  6. const int INF = 2e9;
  7. const LL LNF = 9e18;
  8. const double eps = 1e-;
  9. const int mod = ;
  10. const int maxn = 2e5+;
  11.  
  12. struct MA
  13. {
  14. LL mat[][];
  15. void init()
  16. {
  17. rep(i,,) rep(j,,)
  18. mat[i][j] = (i==j);
  19. }
  20. };
  21.  
  22. LL n,d,m;
  23. LL a[],f[];
  24.  
  25. MA mul(MA x, MA y)
  26. {
  27. MA tmp;
  28. ms(tmp.mat,);
  29. rep(i,,d) rep(j,,d) rep(k,,d)
  30. tmp.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%m, tmp.mat[i][j] %= m;
  31. return tmp;
  32. }
  33.  
  34. MA qpow(MA x, LL y)
  35. {
  36. MA s;
  37. s.init();
  38. while(y)
  39. {
  40. if(y&) s = mul(s,x);
  41. x = mul(x,x);
  42. y >>= ;
  43. }
  44. return s;
  45. }
  46.  
  47. int main()
  48. {
  49. while(scanf("%lld%lld%lld",&d,&n,&m) && (d || n||m))
  50. {
  51. rep(i,,d) scanf("%lld",&a[i]);
  52. rep(i,,d) scanf("%lld",&f[i]);
  53.  
  54. if(n<=d)
  55. {
  56. printf("%lld\n", f[n]);
  57. continue;
  58. }
  59.  
  60. MA x;
  61. ms(x.mat,);
  62. rep(i,,d) x.mat[][i] = a[i];
  63. rep(i,,d) x.mat[i][i-] = ;
  64. x = qpow(x,n-d);
  65.  
  66. LL ans = ;
  67. rep(i,,d)
  68. ans += (1LL*x.mat[][i]*f[d-i+])%m, ans %= m;
  69. printf("%lld\n",ans);
  70. }
  71. }

UVA10870 Recurrences —— 矩阵快速幂的更多相关文章

  1. UVa 10870 Recurrences (矩阵快速幂)

    题意:给定 d , n , m (1<=d<=15,1<=n<=2^31-1,1<=m<=46340).a1 , a2 ..... ad.f(1), f(2) .. ...

  2. UVA10870—Recurrences(简单矩阵快速幂)

    题目链接:https://vjudge.net/problem/UVA-10870 题目意思: 给出a1,a2,a3,a4,a5………………ad,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第 ...

  3. UVA10870 Recurrences (矩阵快速幂及构造方法详解)

    题意: F(n) =  a1 * F(n-1) + a2 * F(n-2)+ ···· + ad * F(n-d). 求给你的n . 很明显这是一道矩阵快速幂的题目. 题解: [Fn-1, Fn-2, ...

  4. UVA - 10870 Recurrences 【矩阵快速幂】

    题目链接 https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553 题意 给出一个表达式 算法 f(n) 思路 ...

  5. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  6. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  7. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  8. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  9. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

随机推荐

  1. Codeforces Gym 100338B Spam Filter 字符串哈希+贝叶斯公式

    原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...

  2. Tarjan缩点+DAG图dp

    题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...

  3. 如何设置tomcat服务器编码为utf-8编码

    原文:http://blog.csdn.net/u014079773/article/details/52637057 在实际开发中我们经常遇到request请求的中文乱码,那么如何解决中文乱码问题? ...

  4. zip 压缩文件夹

    import java.io.*; import java.util.zip.*; /** * @author Dana·Li * <p> * 程序实现了ZIP压缩[compression ...

  5. cocos2d-x step by step(3) Double Kill

    喏,咱们已经调通hello world 了,然后呢,咱们做一些高大上的东西,那做什么呢,做一个打乒乓球的小东西,啊哈! 这就是最终界面了,没画一个球形  就用一个白色方框代替吧. 啊哈! public ...

  6. curl如何发送json数据?如何发送form数据?python的restfull又该如何获取这些数据?

    1.python使用flask+flask_restfull框架写的api接口,做为服务 2.curl 做为客户端发送数据 from flask import request curl发送json的方 ...

  7. caffe版faster-RCNN环境搭建

    faster-rcnn提出论文: <Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks& ...

  8. php 源码编译

    https://cyberpersons.com/2016/08/28/install-nginx-php-php-fpm-mysql-source-run-wordpress-site-ubuntu ...

  9. 使用HtmlUnit登录百度

    环境 java 1.7 HtmlUnit 2.18 eclipse 4.4.2 maven 配置: <dependency> <groupId>net.sourceforge. ...

  10. OpenCV for Python 学习笔记 二

    今天主要看了OpenCV中的事件以及回调函数,这么说可能不准确,主要是下面这两个函数(OpenCV中还有很多这些函数,可以在 http://docs.opencv.org/trunk/modules/ ...