【矩阵乘法】【快速幂】【递推】斐波那契数列&&矩乘优化递推模板
题目大意:
F[0]=0
F[1]=1
F[n+2]=F[n+1]+F[n]
求F[n] mod 104。
| F[n+2] |
| F[n+1] |
=
| 1 | 1 |
| 1 | 0 |
*
| F[n+1] |
| F[n] |
记这个矩阵为A,则有:
| F[n+1] |
| F[n] |
=
An
*
| F[1] |
| F[0] |
=
An
*
| 1 |
| 0 |
然后可以快速幂

#include<cstdio>
#include<vector>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> mat;
mat operator * (const mat &a,const mat &b)
{
mat c(a.size(),vec(b[0].size()));
for(int i=0;i<a.size();++i)
for(int j=0;j<b[0].size();++j)
for(int k=0;k<b.size();++k)
c[i][j]=(c[i][j]+a[i][k]*b[k][j])%10000;
return c;
}
mat Quick_Pow(mat x,int p)
{
if(!p)
{
mat t(2,vec(2));
t[0][0]=1; t[1][1]=1;
return t;
}
mat res=Quick_Pow(x,p>>1);
res=res*res;
if(p&1) res=res*x;
return res;
}
int n;
int main()
{
scanf("%d",&n);
mat A(2,vec(2));
A[0][0]=1; A[0][1]=1; A[1][0]=1;
printf("%d\n",Quick_Pow(A,n)[1][0]);
return 0;
}
——The Solution By AutSky_JadeK From UESTC
转载请注明出处:http://www.cnblogs.com/autsky-jadek/

浙公网安备 33010602011771号
