51nod 1126 求递推序列的第N项【矩阵快速幂】
来源:互联网 发布:淘宝优站排名 编辑:程序博客网 时间:2024/06/07 16:21
#include<iostream>#include<string.h>using namespace std;const int mod=7;typedef struct node{ long long a[2][2]; void init(){ //初始化单位矩阵 memset(a,0,sizeof(a)); for(int i=0;i<2;i++) a[i][i]=1; }}matrix;matrix mul(matrix a,matrix b){ matrix ans; for(int i=0;i<2;i++) for(int j=0;j<2;j++){ ans.a[i][j]=0; for(int k=0;k<2;k++) ans.a[i][j]+=(a.a[i][k]*b.a[k][j])%mod; ans.a[i][j]=(ans.a[i][j]+mod)%mod; } return ans;}matrix add(matrix a,matrix b){ //矩阵加法 matrix ans; for(int i=0;i<2;i++) for(int j=0;j<2;j++) ans.a[i][j]=(a.a[i][j]+b.a[i][j])%mod; return ans;}matrix pow(matrix a,int n){ //a的n次方 matrix ans; ans.init(); //调用函数初始化 while(n){ if(1&n) ans=mul(ans,a); n/=2; a=mul(a,a); } return ans;}int main(){ int a,b,n; matrix s,ans; // cin>>a>>b>>n; s.a[0][0]=0;s.a[0][1]=1; s.a[1][0]=b;s.a[1][1]=a; ans.a[0][0]=1;ans.a[0][1]=0; ans.a[1][0]=1;ans.a[1][1]=0; ans=mul(pow(s,n-1),ans); cout<<ans.a[0][0]<<endl; return 0;}
阅读全文
0 0
- 51nod 1126 求递推序列的第N项 (矩阵快速幂)
- 51 nod 1126 求递推序列的第N项(矩阵快速幂)
- 51nod 1126 求递推序列的第N项(矩阵快速幂、循环)
- 51nod 1126 求递推序列的第N项(矩阵快速幂)
- 51nod 1126 求递推序列的第N项 矩阵快速幂
- 51 nod 1126 求递推序列的第N项 矩阵快速幂
- 51nod 1126 求递推序列的第N项【矩阵快速幂】
- 51Nod 1126 求递推序列的第N项 矩阵快速幂
- 51nod 求递推序列的第N项(矩阵快速幂、模拟求循环长度)
- 【51nod 1126】求递推序列的第N项 【矩阵快速幂】or【枚举找循环节】
- 51Nod Problem 1126 求递推序列的第N项(构造矩阵)
- [51NOD]1126 求递推序列的第N项 [线性递推关系与矩阵乘法]
- 1126 求递推序列的第N项(51nod)
- 51nod 1126 求递推序列的第N项
- 51nod 1126 求递推序列的第N项
- 51nod 1126 求递推序列的第N项
- 51nod-【1126 求递推序列的第N项】
- 51Nod 1126 求递推序列的第N项
- 快速幂
- 【java】Object类有哪些公用方法
- css的三大特性
- 【设计模式】--NULL OBJECT 模式
- 将menu设计为可重用的子系统
- 51nod 1126 求递推序列的第N项【矩阵快速幂】
- 树形结构 数据库表设计
- JAVA学习心得
- 设计模式(16)——迭代器 Iterator
- CHAR与WCHAR字符串的转化
- 51nod 1627 瞬间移动 组合数
- oracle可视化建表网站
- 并发和并行的区别
- bzoj 4345: [POI2016]Korale