矩阵快速幂优化递推式 例:斐波那契数列
来源:互联网 发布:哈工大 矩阵论 课件 编辑:程序博客网 时间:2024/06/04 20:06
矩阵快速幂 例:斐波那契数列
#include<cstdio>#include<algorithm>#include<cstring>#include<iostream>using namespace std;const int M = 1e9+7;struct Matrix {long long a[2][2];Matrix() {memset(a, 0, sizeof(a));}Matrix operator * (const Matrix y) {Matrix ans;for(int i = 0; i <= 1; i++)for(int j = 0; j <= 1; j++)for(int k = 0; k <= 1; k++)ans.a[i][j] += a[i][k]*y.a[k][j];for(int i = 0; i <= 1; i++)for(int j = 0; j <= 1; j++)ans.a[i][j] %= M;return ans;}void operator = (const Matrix b) {for(int i = 0; i <= 1; i++)for(int j = 0; j <= 1; j++)a[i][j] = b.a[i][j];}};int solve(long long x) {Matrix ans, trs;ans.a[0][0] = ans.a[1][1] = 1;trs.a[0][0] = trs.a[1][0] = trs.a[0][1] = 1;while(x) {if(x&1) ans = ans*trs;trs = trs*trs;x >>= 1;}return ans.a[0][0];}int main() {int n;scanf("%d", &n);cout << solve(n-1) << endl;return 0;}
阅读全文
0 0
- 矩阵快速幂优化递推式 例:斐波那契数列
- 矩阵快速幂优化递推式 例:斐波那契数列 poj 3070
- 矩阵快速幂优化递推式 例:斐波那契数列
- 51nod 巨大的斐波那契数列(矩阵快速幂),递推式优化的好模板!!!!!!!
- 矩阵快速幂优化菲波那切数列
- hdu3117(斐波那契数列+矩阵快速幂)
- 矩阵快速幂 斐波那契数列
- EOJ 1499 【斐波那契数列】【矩阵快速幂】
- hdu4549 M斐波那契数列(矩阵快速幂)
- hihoCoder1143 DP 斐波那契数列矩阵快速幂
- nod51-1242斐波那契数列-矩阵快速幂
- 斐波那契数列矩阵快速幂
- 斐波那契数列 打表+矩阵快速幂
- 【矩阵快速幂】 斐波那契数列求解。
- 斐波那契数列(矩阵快速幂)
- *矩阵快速幂(斐波那契数列模板)
- 矩阵快速幂(斐波那契数列)
- HDU4549 M斐波那契数列【矩阵快速幂】
- python里使用正则表达式的MULTILINE标志
- 记一次惊心动魄的数据恢复:rm -rf /usr
- 120. Triangle
- Codeforces Round #439 (Div. 2) A. The Artful Expedient
- Something about file's read and write (C++)
- 矩阵快速幂优化递推式 例:斐波那契数列
- 非公平锁 和 公平锁
- 想随意在评论区“撒野”,《纽约时报》联合谷歌开发的 AI 算法可不答应
- 旷视科技成立 6 周年,我们和孙剑聊了聊
- 在按一次返回键退出
- 在拥有一个“首席AI执行官”之前,企业领导者该回答的四个问题
- MySQL 第七篇:数据库设计、视图与触发器
- codevs1501 二叉树最大宽度和高度
- vivo X20黑金旗舰版图赏,外观精致且内涵丰富