算法竞赛——进阶指南——acwing205. 斐波那契 矩阵快速幂

Xylona ·
更新时间:2024-09-20
· 899 次阅读

利用了矩阵结合律,先算出构造递推矩阵自乘的结果,再与初始矩阵相乘。

#include using namespace std; typedef long long ll; #define ls (o<<1) #define rs (o<<1|1) #define pb push_back //#define a(i,j) a[(i)*(m+2)+(j)] //m是矩阵的列数 const double PI= acos(-1.0); const int M = 1e5+7; const int mod =10000; /* int head[M],cnt; void init(){cnt=0,memset(head,0,sizeof(head));} struct EDGE{int to,nxt,val;}ee[M*2]; void add(int x,int y,int z){ee[++cnt].nxt=head[x],ee[cnt].to=y,ee[cnt].val=z,head[x]=cnt;} */ struct matrix { int c[2][2]; }; matrix operator *(const matrix &a,const matrix &b) { matrix c={0}; for(int i=0;i<=1;i++) for(int j=0;j<=1;j++) for(int k=0;k>x) { if(x==-1)break; int f[2]={0,1};//0,1 matrix a,ans; a=matrix{0,1,1,1}; ans=matrix{1,0,0,1}; while(x) { if(x&1)ans=ans*a; a=a*a; x/=2; } cout<<ans.c[1][0]<<endl; } return 0; }
作者:夕林山寸



斐波那契 算法 矩阵

需要 登录 后方可回复, 如果你还没有账号请 注册新账号