第26节.pptx

斐波那契矩阵20 斐波那契数列矩阵O(logN)解及其推广 - 图2的算法

1)斐波那契数列的线性求解(O(N))的方式非常好理解
2)同时利用线性代数,也可以改写出另一种表示
| F(N) , F(N-1) | = | F(2), F(1) | * 某个二阶矩阵的N-2次方
3)求出这个二阶矩阵,进而最快求出这个二阶矩阵的N-2次方

  1. // 斐波那契数列算法 f(1)=1,f(2)=1,f(3)=2,f(4)=3,f(5)=5…… o(n)
  2. public static int f(int n) {
  3. if (n < 1) {
  4. return 0;
  5. }
  6. if (n == 1 || n == 2) {
  7. return 1;
  8. }
  9. int res = 1;
  10. int pre = 1;
  11. int tmp = 0;
  12. for (int i = 3; i <= n; i++) {
  13. tmp = res;
  14. res = res + pre;
  15. pre = tmp;
  16. }
  17. return res;
  18. }
  1. // O(logN)的算法 |f(n),f(n-1)| = |f(2),f(1)|*|1,1| ^ (n-2)
  2. // |1,0|
  3. public static int f2(int n) {
  4. if (n < 1) {
  5. return 0;
  6. }
  7. if (n == 1 || n == 2) {
  8. return 1;
  9. }
  10. int[][] baseMatrix = {{1, 1}, {1, 0}};
  11. int[] coefficient = {1, 1};
  12. int[][] res = matrixPower(baseMatrix, n - 2);
  13. return coefficient[0] * res[0][0] + coefficient[1] * res[1][0];
  14. }
  15. public static int[][] matrixPower(int[][] m, int p) {
  16. int[][] res = new int[m.length][m[0].length];
  17. for (int i = 0; i < res.length; i++) {
  18. res[i][i] = 1;
  19. }
  20. // res = 矩阵中的1,矩阵1次方
  21. int[][] t = m;
  22. for (; p != 0; p >>= 1) {
  23. if ((p & 1) != 0) {
  24. res = multiMatrix(res, t);
  25. }
  26. t = multiMatrix(t, t);
  27. }
  28. return res;
  29. }
  30. public static int[][] multiMatrix(int[][] m1, int[][] m2) {
  31. int[][] res = new int[m1.length][m2[0].length];
  32. for (int i = 0; i < m1.length; i++) {
  33. for (int j = 0; j < m2[0].length; j++) {
  34. for (int k = 0; k < m2.length; k++) {
  35. res[i][j] += m1[i][k] * m2[k][j];
  36. }
  37. }
  38. }
  39. return res;
  40. }

类似斐波那契数列的递归优化(没有条件转移)

如果某个递归,除了初始项之外,具有如下的形式
F(N) = C1 F(N) + C2 F(N-1) + … + Ck * F(N-k) ( C1…Ck 和k都是常数)
并且这个递归的表达式是严格的、不随条件转移的
那么都存在类似斐波那契数列的优化,时间复杂度都能优化成O(logN)

题目

题目1:母牛数量

第一年农场有1只成熟的母牛A,往后的每年:
1)每一只成熟的母牛都会生一只母牛
2)每一只新出生的母牛都在出生的第三年成熟
3)每一只母牛永远不会死
返回N年后牛的数量

  1. public static int cowNumber(int n) {
  2. if (n == 1) {
  3. return 1;
  4. }
  5. if (n == 2) {
  6. return 2;
  7. }
  8. if (n == 3) {
  9. return 3;
  10. }
  11. int[][] base = {{1, 1, 0}, {0, 0, 1}, {1, 0, 0}};
  12. int[] coefficient = {3, 2, 1};
  13. int[][] res = matrixPower(base, n - 3);
  14. return coefficient[0] * res[0][0] + coefficient[1] * res[1][0] + coefficient[2] * res[2][0];
  15. }
  16. public static int[][] matrixPower(int[][] m, int p) {
  17. int[][] res = new int[m.length][m[0].length];
  18. for (int i = 0; i < res.length; i++) {
  19. res[i][i] = 1;
  20. }
  21. // res = 矩阵中的1,矩阵1次方
  22. int[][] t = m;
  23. for (; p != 0; p >>= 1) {
  24. if ((p & 1) != 0) {
  25. res = multiMatrix(res, t);
  26. }
  27. t = multiMatrix(t, t);
  28. }
  29. return res;
  30. }
  31. public static int[][] multiMatrix(int[][] m1, int[][] m2) {
  32. int[][] res = new int[m1.length][m2[0].length];
  33. for (int i = 0; i < m1.length; i++) {
  34. for (int j = 0; j < m2[0].length; j++) {
  35. for (int k = 0; k < m2.length; k++) {
  36. res[i][j] += m1[i][k] * m2[k][j];
  37. }
  38. }
  39. }
  40. return res;
  41. }