zoukankan      html  css  js  c++  java
  • 【矩阵乘法】【快速幂】【递推】斐波那契数列&&矩乘优化递推模板

    题目大意:

    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
    ",Quick_Pow(A,n)[1][0]);
    	return 0;
    }
  • 相关阅读:
    4.18下午
    4.18上午
    2017.5.10-afternoon
    2017.5.9-afternoon
    2017.5.10-morning
    2017.5.9-morning
    2017.5.8-afternoon
    2017.5.8-morning
    2017.5.5-afternoon
    2017.5.5-morning
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/4499725.html
Copyright © 2011-2022 走看看