题目链接

https://odzkskevi.qnssl.com/d474b5dd1cebae1d617e6c48f5aca598?v=1524578553

题意

给出一个表达式 算法 f(n)

思路

n 很大 自然想到是 矩阵快速幂

那么问题就是 怎么构造矩阵

我们想到的一种构造方法是

n = 2 时

n = 3 时

然后大概就能够发现规律了吧 。。

AC代码

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <ctype.h>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <climits>
  7. #include <ctime>
  8. #include <iostream>
  9. #include <algorithm>
  10. #include <deque>
  11. #include <vector>
  12. #include <queue>
  13. #include <string>
  14. #include <map>
  15. #include <stack>
  16. #include <set>
  17. #include <list>
  18. #include <numeric>
  19. #include <sstream>
  20. #include <iomanip>
  21. #include <limits>
  22. #define CLR(a, b) memset(a, (b), sizeof(a))
  23. #define pb push_back
  24. #define bug puts("***bug***");
  25. #define fi first
  26. #define se second
  27. #define stack_expand #pragma comment(linker, "/STACK:102400000,102400000")
  28. #define syn_close ios::sync_with_stdio(false);cin.tie(0);
  29. #define sp system("pause");
  30. //#define bug
  31. //#define gets gets_s
  32. using namespace std;
  33. typedef long long ll;
  34. typedef long double ld;
  35. typedef unsigned long long ull;
  36. typedef pair <int, int> pii;
  37. typedef pair <ll, ll> pll;
  38. typedef pair <string, int> psi;
  39. typedef pair <string, string> pss;
  40. typedef pair <double, int> pdi;
  41. const double PI = acos(-1.0);
  42. const double E = exp(1.0);
  43. const double eps = 1e-8;
  44. const int INF = 0x3f3f3f3f;
  45. const int maxn = 1e2 + 10;
  46. const int MOD = 142857;
  47. int d, n, m;
  48. ll a[20], b[20];
  49. struct Matrix
  50. {
  51. ll a[20][20];
  52. Matrix() {}
  53. Matrix operator * (Matrix const &b)const
  54. {
  55. Matrix res;
  56. CLR(res.a, 0);
  57. for (int i = 0; i < d; i++)
  58. for (int j = 0; j < d; j++)
  59. for (int k = 0; k < d; k++)
  60. res.a[i][j] = (res.a[i][j] + this->a[i][k] * b.a[k][j]) % m;
  61. return res;
  62. }
  63. };
  64. Matrix pow_mod(Matrix ans, int n)
  65. {
  66. Matrix base;
  67. CLR(base.a, 0);
  68. for (int i = 0; i < d; ++i)
  69. {
  70. base.a[i][0] = a[i];
  71. }
  72. for (int i = 0; i < d; ++i)
  73. {
  74. base.a[i][i + 1] = 1;
  75. }
  76. while (n > 0)
  77. {
  78. if (n & 1)
  79. ans = ans * base;
  80. base = base * base;
  81. n >>= 1;
  82. }
  83. return ans;
  84. }
  85. int main()
  86. {
  87. while (scanf("%d %d %d", &d, &n, &m) && (d || n || m))
  88. {
  89. for (int i = 0; i < d; i++)
  90. scanf("%lld", &a[i]);
  91. for (int i = 0; i < d; i++)
  92. scanf("%lld", &b[i]);
  93. if (n <= d)
  94. {
  95. printf("%lld\n", b[n - 1] % m);
  96. continue;
  97. }
  98. Matrix ans;
  99. for (int i = 0; i < d; i++)
  100. for (int j = 0; j < d; j++)
  101. ans.a[i][j] = b[d - j - 1];
  102. ans = pow_mod(ans, n - d);
  103. printf("%lld\n", ans.a[0][0]);
  104. }
  105. return 0;
  106. }

UVA - 10870 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. uva 10870 递推关系矩阵快速幂模

    Recurrences Input: standard input Output: standard output Consider recurrent functions of the follow ...

  3. UVA 10870 - Recurrences(矩阵高速功率)

    UVA 10870 - Recurrences 题目链接 题意:f(n) = a1 f(n - 1) + a2 f(n - 2) + a3 f(n - 3) + ... + ad f(n - d), ...

  4. UVA10870 Recurrences —— 矩阵快速幂

    题目链接:https://vjudge.net/problem/UVA-10870 题意: 典型的矩阵快速幂的运用.比一般的斐波那契数推导式多了几项而已. 代码如下: #include <bit ...

  5. POJ-3070Fibonacci(矩阵快速幂求Fibonacci数列) uva 10689 Yet another Number Sequence【矩阵快速幂】

    典型的两道矩阵快速幂求斐波那契数列 POJ 那是 默认a=0,b=1 UVA 一般情况是 斐波那契f(n)=(n-1)次幂情况下的(ans.m[0][0] * b + ans.m[0][1] * a) ...

  6. uva 10518 - How Many Calls?(矩阵快速幂)

    题目链接:uva 10518 - How Many Calls? 公式f(n) = 2 * F(n) - 1, F(n)用矩阵快速幂求. #include <stdio.h> #inclu ...

  7. Tribonacci UVA - 12470 (简单的斐波拉契数列)(矩阵快速幂)

    题意:a1=0;a2=1;a3=2; a(n)=a(n-1)+a(n-2)+a(n-3);  求a(n) 思路:矩阵快速幂 #include<cstdio> #include<cst ...

  8. UVA - 11149 (矩阵快速幂+倍增法)

    第一道矩阵快速幂的题:模板题: #include<stack> #include<queue> #include<cmath> #include<cstdio ...

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

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

随机推荐

  1. SDUTOJ 2804求二叉树的深度

    #include<iostream> #include<stdlib.h> #include<string.h> using namespace std; char ...

  2. jenkins调用shell脚本 输出带颜色字体

    jenkins需要安装AnsiColor插件在构建环境项选择“color ansi console output” 安装插件AnsiColor shell 脚本相关颜色设置 echo -e " ...

  3. Tomcat Https配置

    一.生成KeyStore 打开命令行,输入:keytool -genkey -alias tomcat_server -keyalg RSA -storepass jimmypwd -validity ...

  4. IIS5.1、IIS6.0、IIS7.5中安装配置MVC 3

    本文主要介绍在IIS5.1.IIS6.0.IIS7.5中安装配置MVC 3的具体办法! 正文: IIS5.1 1. 安装Microsoft .net FrameWork 4.0安装包; 2. 安装AS ...

  5. MongoDB 的聚集操作

    聚合引言 聚集操作就是出来数据记录并返回计算结果的操作.MongoDB提供了丰富的聚集操作.可以检測和执行数据集上的计算.执行在mongod上的数据聚集简化了代码和资源限制. 像查询一样,在Mongo ...

  6. CentOS yum 命令出现 [Errno 14] curl#6 - &quot;Couldn&#39;t resolve host ...&quot; 的解决方法

    安装svn的时候,发现报错说一个地址无法訪问. # yum list | grep subversion http://opensource.wandisco.com/centos/7/svn-1.8 ...

  7. 玩转JPA(一)---异常:Repeated column in mapping for entity/should be mapped with insert=&quot;false&quot; update=&quot;fal

    近期用JPA遇到这样一个问题:Repeated column in mapping for entity: com.ketayao.security.entity.main.User column: ...

  8. ubuntu安装中文man手册

    1.安装manpages-zh包 sudo apt-get install manpages-zh 2.修改manpath文件 执行如下命令: vi /etc/manpath.config %s+/u ...

  9. 为什么要用markdown写作

    无论是 EPUB, mobi,还是 Kindle 用的专有格式 .azw,都只是把一堆 `HTML 文件打包`而已.如果你写的是书,用 Markdown 标注格式之后,可以很方便地转为以上格式 使用W ...

  10. Dijkstra 算法——计算有权最短路径(边有权值)

    [0]README 0.1) 本文总结于 数据结构与算法分析, 源代码均为原创, 旨在理解 Dijkstra 的思想并用源代码加以实现: 0.2)最短路径算法的基础知识,参见 http://blog. ...