题意:求菲波那切数列的第n项。

分析:矩阵快速幂。

右边的矩阵为a0 ,a1,,,

然后求乘一次,就进一位,求第n项,就是矩阵的n次方后,再乘以b矩阵后的第一行的第一列。

  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. typedef long long ll;
  7.  
  8. const int maxn = ;
  9. const int maxm = ;
  10.  
  11. struct Matrix {
  12. int n,m;
  13. int a[maxn][maxm];
  14.  
  15. void clear() {
  16. n = m = ;
  17. memset(a,,sizeof(a));
  18. }
  19.  
  20. Matrix operator + (const Matrix &b) const {
  21. Matrix tmp;
  22. tmp.n = n;
  23. tmp.m = m;
  24.  
  25. for(int i=;i<n;++i)
  26. for(int j=;j<m;++j)
  27. tmp.a[i][j] = a[i][j] + b.a[i][j];
  28.  
  29. return tmp;
  30. }
  31.  
  32. Matrix operator - (const Matrix &b) const {
  33. Matrix tmp;
  34. tmp.n = n;
  35. tmp.m = m;
  36.  
  37. for(int i=;i<n;++i)
  38. for(int j=;j<m;++j)
  39. tmp.a[i][j] = a[i][j] - b.a[i][j];
  40.  
  41. return tmp;
  42. }
  43.  
  44. Matrix operator * (const Matrix & b) const {
  45. Matrix tmp;
  46. tmp.clear();
  47. tmp.n = n;
  48. tmp.m = b.m;
  49.  
  50. for(int i=;i<n;++i)
  51. for(int j=;j<b.m;++j)
  52. for(int k=;k<m;++k)
  53. tmp.a[i][j] +=(a[i][k]*b.a[k][j])%;
  54.  
  55. return tmp;
  56. }
  57.  
  58. Matrix operator ^ (const int& k) const {
  59. Matrix tmp,t = *this;
  60.  
  61. int p = k;
  62. tmp.clear();
  63.  
  64. tmp.m = tmp.n = this->n;
  65.  
  66. for(int i=;i<n;++i)
  67. tmp.a[i][i] = ;
  68.  
  69. while(p) {
  70. if(p&) tmp = tmp*t;
  71. p>>=;
  72. t = t*t;
  73. }
  74. return tmp;
  75. }
  76. };
  77.  
  78. int main(int argc, char const *argv[])
  79. {
  80. int n;
  81. while(true) {
  82. scanf("%d",&n);
  83. if(n==-) break;
  84. Matrix f;
  85. f.clear();
  86. f.n = f.m = ;
  87. f.a[][] = ;
  88. f.a[][] = ;
  89. f.a[][] = ;
  90. f.a[][] = ;
  91.  
  92. f = f^n;
  93. Matrix x;
  94. x.clear();
  95. x.n = ;
  96. x.m = ;
  97. x.a[][] = ;
  98. x.a[][] = ;
  99. f = f*x;
  100.  
  101. printf("%d\n", f.a[][]);
  102.  
  103. }
  104. return ;
  105. }

POJ 3070 矩阵快速幂的更多相关文章

  1. POJ 3070 矩阵快速幂解决fib问题

    矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...

  2. 解题报告:poj 3070 - 矩阵快速幂简单应用

    2017-09-13 19:22:01 writer:pprp 题意很简单,就是通过矩阵快速幂进行运算,得到斐波那契数列靠后的位数 . 这是原理,实现部分就是矩阵的快速幂,也就是二分来做 矩阵快速幂可 ...

  3. poj 3070 矩阵快速幂模板

    题意:求fibonacci数列第n项 #include "iostream" #include "vector" #include "cstring& ...

  4. poj 3233 矩阵快速幂

    地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方  结果模m的相加和是多少 Given a n × n matrix A and a positive i ...

  5. poj 3734 矩阵快速幂+YY

    题目原意:N个方块排成一列,每个方块可涂成红.蓝.绿.黄.问红方块和绿方块都是偶数的方案的个数. sol:找规律列递推式+矩阵快速幂 设已经染完了i个方块将要染第i+1个方块. a[i]=1-i方块中 ...

  6. POJ 3233 矩阵快速幂&二分

    题意: 给你一个n*n的矩阵 让你求S: 思路: 只知道矩阵快速幂 然后nlogn递推是会TLE的. 所以呢 要把那个n换成log 那这个怎么搞呢 二分! 当k为偶数时: 当k为奇数时: 就按照这么搞 ...

  7. poj 3744 矩阵快速幂+概率dp

    题目大意: 输入n,代表一位童子兵要穿过一条路,路上有些地方放着n个地雷(1<=n<=10).再输入p,代表这位童子兵非常好玩,走路一蹦一跳的.每次他在 i 位置有 p 的概率走一步到 i ...

  8. Blocks(POJ 3734 矩阵快速幂)

    Blocks Input The first line of the input contains an integer T(1≤T≤100), the number of test cases. E ...

  9. Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板

    题目链接  请猛戳~ Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 ...

随机推荐

  1. myeclipse更改后台代码不用重启tomcat的方法

    myeclipse更改后台代码不用重启tomcat的方法   方法1:在WebRoot下的META-INF文件夹中新建一个名为context.xml文件,里面添加如下内容(要区分大小写): <C ...

  2. Proguard breaking audio file in assets or raw

    http://stackoverflow.com/questions/21440572/proguard-breaking-audio-file-in-assets-or-raw Issue: I h ...

  3. SQLAlchemy安装和使用

    1.SQLAlchemy安装 SQLAlchemy依赖mysql-python驱动,mysql-python目前只有支持py2的版本和mysql5.5的版本 点我:mysql-python链接 版本: ...

  4. OpenLayers 案例一

    序 OpenLayers 是一个专为Web GIS 客户端开发提供的JavaScript 类库包,用于实现标准格式发布的地图数据访问. 例子 <!doctype html> <htm ...

  5. React.js 小书 Lesson8 - 组件的组合、嵌套和组件树

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson8 转载请注明出处,保留原文链接和作者信息. 继续拓展前面的例子,现在我们已经有了 Heade ...

  6. URAL 1252 ——Sorting the Tombstones——————【gcd的应用】

    Sorting the Tombstones Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  7. [转]Oracle ROWNUM用法和分页查询总结

    本文转自:http://blog.csdn.net/fw0124/article/details/42737671 ****************************************** ...

  8. [转]Using NLog for ASP.NET Core to write custom information to the database

    本文转自:https://github.com/NLog/NLog/issues/1366 In the previous versions of NLog it was easily possibl ...

  9. springboot+mybatis实现登录功能,返回json

    1.新建maven项目(pom) <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  10. python发送邮件(带附件)

    python通过stmp发送qq邮件,带附件 import smtplib from email.mime.multipart import MIMEMultipart from email.mime ...