【luogu P1962 斐波那契数列】 题解

题目链接:https://www.luogu.org/problemnew/show/P1962

给你篇dalao的blog自己看吧,把矩阵快速幂的板子一改就OK

 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
struct Matrix{
long long m[][];
}A,E,ans;
long long n,k, mod = ;
Matrix mul(Matrix A,Matrix B)
{
Matrix C;
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
{
C.m[i][j] = ;
for(int k = ; k <= ; k++)
C.m[i][j] = (C.m[i][j]+(A.m[i][k]*B.m[k][j]))%mod;
}
return C;
}
Matrix fast(Matrix A, long long k)
{
Matrix S = E;
while(k)
{
if(k&) S = mul(S,A);
A = mul(A,A);
k = k>>;
}
return S;
}
int main(){ scanf("%lld",&k); E.m[][] = ;
E.m[][] = ;
A.m[][] = ;
A.m[][] = ;
A.m[][] = ;
ans = fast(A,k); printf("%lld ",(ans.m[][])%mod);
return ;
}
上一篇:(四)Lua脚本语言入门


下一篇:07.进程管理+作业控制+文件查找与压缩+文件压缩与打包+tar打包解包+NFS