2017-09-13 19:22:01

writer:pprp

题意很简单,就是通过矩阵快速幂进行运算,得到斐波那契数列靠后的位数

.

这是原理,实现部分就是矩阵的快速幂,也就是二分来做

矩阵快速幂可以用来解决线性递推方程,难点在于矩阵的构造

代码如下:

  1. /*
  2. @theme:用矩阵快速幂解决线性递推公式-斐波那契数列
  3. @writer:pprp
  4. @begin:21:17
  5. @end:19:10
  6. @error:注意mod的位置,不能连用,要加括号来用
  7. @date:2017/9/13
  8. */
  9.  
  10. #include <iostream>
  11. #include <cstdio>
  12. #include <algorithm>
  13. #include <cstring>
  14.  
  15. using namespace std;
  16. typedef long long ll;
  17. const int mod=;
  18.  
  19. struct Mat
  20. {
  21. ll a[][];
  22. };
  23.  
  24. Mat mat_mul(Mat x, Mat y)
  25. {
  26. Mat res;
  27. memset(res.a,,sizeof(res.a));
  28. for(int i = ; i < ; i++)
  29. for(int j = ; j < ; j++)
  30. for(int k = ; k < ; k++)
  31. {
  32. res.a[i][j] += x.a[i][k] * y.a[k][j];
  33. res.a[i][j] %= mod;
  34. }
  35. return res;
  36. }
  37.  
  38. void quick_pow(ll n)
  39. {
  40. Mat E,res;
  41. E.a[][] = E.a[][] = E.a[][] = ;
  42. E.a[][] = ;
  43. memset(res.a,,sizeof(res.a));
  44.  
  45. for(int i = ; i < ; i++)//二阶单位矩阵
  46. res.a[i][i] = ;
  47.  
  48. while(n)
  49. {
  50. if(n&)
  51. res = mat_mul(res,E);
  52. E = mat_mul(E,E);
  53. n >>= ;
  54. }
  55. cout << res.a[][] << endl;
  56. }
  57.  
  58. int main()
  59. {
  60. ios::sync_with_stdio(false);
  61. ll n;
  62. while(cin >> n && n != -)
  63. {
  64. quick_pow(n);
  65. }
  66. return ;
  67. }
  1. /*
  2. @theme:用矩阵快速幂解决线性递推公式-斐波那契数列
  3. @writer:pprp
  4. @begin:21:17
  5. @end:19:10
  6. @error:注意mod的位置,不能连用,要加括号来用
  7. @date:2017/9/13
  8. */
  9.  
  10. #include <iostream>
  11. #include <cstdio>
  12. #include <algorithm>
  13. #include <cstring>
  14.  
  15. using namespace std;
  16. typedef long long ll;
  17. const int mod=;
  18.  
  19. struct Mat
  20. {
  21. ll a[][];
  22. };
  23.  
  24. Mat mat_mul(Mat x, Mat y)
  25. {
  26. Mat res;
  27. memset(res.a,,sizeof(res.a));
  28. for(int i = ; i < ; i++)
  29. for(int j = ; j < ; j++)
  30. for(int k = ; k < ; k++)
  31. {
  32. res.a[i][j] += x.a[i][k] * y.a[k][j];
  33. res.a[i][j] %= mod;
  34. }
  35. return res;
  36. }
  37.  
  38. void quick_pow(ll n)
  39. {
  40. Mat E,res;
  41. E.a[][] = E.a[][] = E.a[][] = ;
  42. E.a[][] = ;
  43. memset(res.a,,sizeof(res.a));
  44.  
  45. for(int i = ; i < ; i++)//二阶单位矩阵
  46. res.a[i][i] = ;
  47.  
  48. while(n)
  49. {
  50. if(n&)
  51. res = mat_mul(res,E);
  52. E = mat_mul(E,E);
  53. n >>= ;
  54. }
  55. cout << res.a[][] << endl;
  56. }
  57.  
  58. int main()
  59. {
  60. ios::sync_with_stdio(false);
  61. ll n;
  62. while(cin >> n && n != -)
  63. {
  64. quick_pow(n);
  65. }
  66. return ;
  67. }

解题报告:poj 3070 - 矩阵快速幂简单应用的更多相关文章

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

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

  2. POJ 3070 矩阵快速幂

    题意:求菲波那切数列的第n项. 分析:矩阵快速幂. 右边的矩阵为a0 ,a1,,, 然后求乘一次,就进一位,求第n项,就是矩阵的n次方后,再乘以b矩阵后的第一行的第一列. #include <c ...

  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. POJ3070矩阵快速幂简单题

    题意:       求斐波那契后四位,n <= 1,000,000,000. 思路:        简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键 ...

  6. poj 3734 矩阵快速幂+YY

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

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

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

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

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

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

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

随机推荐

  1. Top 10 Algorithms of 20th and 21st Century

    Top 10 Algorithms of 20th and 21st Century MATH 595 (Section TTA) Fall 2014 TR 2:00 pm - 3:20 pm, Ro ...

  2. 并发编程 - 线程 - 1.互斥锁/2.GIL解释器锁/3.死锁与递归锁/4.信号量/5.Event事件/6.定时器

    1.互斥锁: 原理:将并行变成串行 精髓:局部串行,只针对共享数据修改 保护不同的数据就应该用不用的锁 from threading import Thread, Lock import time n ...

  3. django之页面缓存

    一  全站缓存(全局缓存) 在settings中配置,主要就是两个中间件,需要注意的就是两个中间件的前后位置 MIDDLEWARE_CLASSES = ( ‘django.middleware.cac ...

  4. yii2美化url

    http://blog.csdn.net/xundh/article/details/45418265

  5. HomeBrew的安装详细步骤

    1)终端输入:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master ...

  6. Vue组件的定义方式

    1.使用template标签定义组件 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  7. Mac使用操作

    快捷键退出程序:Command + Q 快捷键关闭窗口:Command + W(关闭程序的窗口不一定是退出程序) 单击左上角黑苹果,菜单里面有强制退出 Finder菜单的偏好设置,高级设置菜单里面可以 ...

  8. PKU 2002 Squares(二维点哈希+平方求余法+链地址法)

    题目大意:原题链接 给定平面上的N个点,求出这些点一共可以构成多少个正方形. 解题思路: 若正方形为ABCD,A坐标为(x1, y1),B坐标为(x2, y2),则很容易可以推出C和D的坐标.对于特定 ...

  9. Gym - 101875I I Will Go (dfs序)

    题意:N个人要参加一个局,每个人有自己的好朋友,如果他的好朋友来,他才有可能来.N个人的关系不够成环.Q次查询,问若x来了,y是否肯定来. 分析:若点y是x的祖先,则y肯定回来.一次dfs确定每个点覆 ...

  10. JMS API(二)

    JMS 公共API 接口共7个: 1.ConnectionFactory 2.Destination 3.Connection 4.Session 5.Message 6.MessageProduce ...