[

]

I

V

[矩阵乘法]裴波拉契数列IV

[矩阵乘法]裴波拉契数列IV

Description

求数列f[n]=f[n-2]+f[n-1]+n+1的第N项,其中f[1]=1,f[2]:=1.


Input

n(1<n<231-1)


Output

一个数为裴波拉契数列的第n项mod 9973;


Sample Input

10000


Sample Output

4399


题目解析

对于为什么用矩阵乘法来做,详见博客斐波那契数列II

关于递推式略, 详见博客斐波那契数列III,并请独自尝试通过类比来推递推式。

然后可以构造出一个

4

4

4 * 4

4∗4的矩阵

T

T

T

0

1

0

0

1

1

0

0

0

1

1

0

0

1

1

1

\begin{vmatrix} 0 & 1 & 0 & 0\\ 1 & 1 & 0 & 0\\ 0 & 1 & 1 & 0\\ 0 & 1 & 1 & 1\\ \end{vmatrix}

∣∣∣∣∣∣∣∣​0100​1111​0011​0001​∣∣∣∣∣∣∣∣​


Code

  1. #include <cmath>
  2. #include <cstdio>
  3. #include <iostream>
  4. using namespace std;
  5. int nt;
  6. const int MOD = 9973;
  7. struct matrix
  8. {
  9. int n, m;
  10. int t[10][10];
  11. }t1, t2, t3;
  12. matrix operator *(matrix t, matrix r)
  13. {
  14. matrix c;
  15. c.n = t.n, c.m = r.m;
  16. for (int i = 1; i <= c.n; ++ i)
  17. for (int j = 1; j <= c.m; ++ j)
  18. c.t[i][j]=0;
  19. for (int k = 1; k <= t.m; ++ k)
  20. for (int i = 1; i <= t.n; ++ i)
  21. for (int j = 1; j <= r.m; ++ j)
  22. c.t[i][j] = (c.t[i][j] + t.t[i][k] * r.t[k][j] % MOD) % MOD;
  23. return c;
  24. }
  25. void rt (int k)
  26. {
  27. if (k == 1)
  28. {
  29. t2 = t1;
  30. return;
  31. }
  32. rt (k / 2);
  33. t2 = t2 * t2;
  34. if (k & 1) t2 = t2 * t1;
  35. }
  36. int main()
  37. {
  38. scanf ("%d", &nt);
  39. if (nt == 1)
  40. {
  41. printf ("1");
  42. return 0;
  43. }
  44. t3.n = 1;
  45. t1.n = t1.m = t3.m = 4;
  46. t1.t[1][1] = 0, t1.t[1][2] = 1, t1.t[1][3] = 0, t1.t[1][4] = 0;
  47. t1.t[2][1] = 1, t1.t[2][2] = 1, t1.t[2][3] = 0, t1.t[2][4] = 0;
  48. t1.t[3][1] = 0, t1.t[3][2] = 1, t1.t[3][3] = 1, t1.t[3][4] = 0;
  49. t1.t[4][1] = 0, t1.t[4][2] = 1, t1.t[4][3] = 1, t1.t[4][4] = 1;
  50. t3.t[1][1] = t3.t[1][2] = t3.t[1][4] = 1; t3.t[1][3] = 3;
  51. rt (nt - 1);
  52. t3 = t3 * t2;
  53. printf ("%d", t3.t[1][1]);
  54. return 0;
  55. }

[矩阵乘法]斐波那契数列IV的更多相关文章

  1. [矩阵乘法]裴波拉契数列III

    [ 矩 阵 乘 法 ] 裴 波 拉 契 数 列 I I I [矩阵乘法]裴波拉契数列III [矩阵乘法]裴波拉契数列III Description 求数列f[n]=f[n-1]+f[n-2]+1的第N ...

  2. [矩阵乘法]裴波拉契数列II

    [ 矩 阵 乘 法 ] 裴 波 拉 契 数 列 I I [矩阵乘法]裴波拉契数列II [矩阵乘法]裴波拉契数列II Description 形如 1 1 2 3 5 8 13 21 34 55 89 ...

  3. 矩阵乘法&&矩阵快速幂&&最基本的矩阵模型——斐波那契数列

    矩阵,一个神奇又令人崩溃的东西,常常用来优化序列递推 在百度百科中,矩阵的定义: 在数学中,矩阵(Matrix)是一个按照长方阵列排列的复数或实数集合 ,最早来自于方程组的系数及常数所构成的方阵.这一 ...

  4. poj3070_斐波那契数列(Fibonacci)

    用矩阵求斐波那契数列,快速幂log(n),只用求最后4位(加和乘的运算中前面的位数无用) #include <stdio.h> #include <stdlib.h> int ...

  5. 斐波那契数列的生成 %1e8 后的结果

    方法一  用数组开,一般开到1e7,1e8 左右的数组就是极限了   对时间也是挑战 #include<bits/stdc++.h> using namespace std; ; int ...

  6. 【poj3070】矩阵乘法求斐波那契数列

    [题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...

  7. Luogu P1962 斐波那契数列(矩阵乘法模板)

    传送门(其实就是求斐波那契数列....) 累了 明天再解释 做这道题需要一些关于矩阵乘法的基础知识. 1. 矩阵乘法的基础运算 只有当矩阵A的列数等于矩阵B的行数时,A与B可以相乘(A的行数不一定等于 ...

  8. P1349 广义斐波那契数列(矩阵乘法)

    题目 P1349 广义斐波那契数列 解析 把普通的矩阵乘法求斐波那契数列改一改,随便一推就出来了 \[\begin{bmatrix}f_2\\f_1 \end{bmatrix}\begin{bmatr ...

  9. 斐波那契数列 矩阵乘法优化DP

    斐波那契数列 矩阵乘法优化DP 求\(f(n) \%1000000007​\),\(n\le 10^{18}​\) 矩阵乘法:\(i\times k\)的矩阵\(A\)乘\(k\times j\)的矩 ...

随机推荐

  1. switchable css dark theme in js & html custom element

    switchable css dark theme in js & html custom element dark theme / dark mode https://codepen.io/ ...

  2. Visual Studio Online & Web 版 VS Code

    Visual Studio Online & Web 版 VS Code https://online.visualstudio.com https://devblogs.microsoft. ...

  3. npx & yarn & npm

    npx & yarn & npm React Redux App https://reactjs.org/ https://github.com/facebook/create-rea ...

  4. npm & cli & node.js

    npm & cli & node.js https://www.npmjs.com/ https://www.npmjs.com/settings/xgqfrms/packages h ...

  5. base 64 & blob & image url

    base 64 & blob & image url base 64 image & e.clipboardData.items[1] https://codepen.io/x ...

  6. infinite auto load more & infinite scroll & load more

    infinite auto load more & infinite scroll & load more https://codepen.io/xgqfrms/pen/NZVvGM ...

  7. css delete line text & html del

    css delete line text & html del html <del>¥720</del> demo <span class="ticke ...

  8. Android 比较好看的注册登录界面

    各位看官姥爷: 对于一款android手机app而言,美观的界面使得用户有好的使用体验,而一款好看的注册登录界面也会给用户好的用户体验,那么话不多说,直接上代码 首先是一款简单的界面展示 1.登陆界面 ...

  9. LoveWord

    个人喜欢的句子汇总! 我告诉你我喜欢你,并不是一定要和你在一起,只是希望今后的你,在遭遇人生低谷的时候,不要灰心,至少曾经有人被你的魅力所吸引,曾经是,以后也会是----村上村树

  10. vue高级

    1.nrm nrm提供了一些最常用的npm包镜像地址,可以快速切换服务器地址下载资源.它只是提供了地址,并不是装包工具.如果没有安装npm,需要安装node,然后直接安装即可.node下载链接:htt ...