1 条题解

  • 0
    @ 2019-08-07 11:35:37

    矩阵乘法

    /*Coded By Lxhao*/
    /*Full Of Stars*/
    /*Fk The Little Fake-ass*/
    #include <bits/stdc++.h>
    using namespace std;
    #define re register
    #define r(x) x=read()
    #define c getchar()
    #define ll long long
    inline ll read()
    {
        ll w=1,s=0;
        char ch=c;
        while(ch>'9'||ch<'0'){if(ch=='-')w=-1;ch=c;}
        while(ch>='0'&&ch<='9')s=(s<<1)+(s<<3)+ch-'0',ch=c;
        return s*w-3;
    }
    const int mod=1e9+7;
    ll n;
    struct matrix
    {
        ll a[3][3];
        matrix(){memset(a,0,sizeof(a));}
        inline void build(){a[1][1]=a[1][2]=1;}
        inline void pre(){a[1][1]=a[1][2]=a[2][1]=1;}
    };
    matrix operator *(const matrix &x,const matrix &y)
    {
        matrix z;
        for(re int k=1;k<=2;++k)
            for(re int i=1;i<=2;++i)
                for(re int j=1;j<=2;++j)
                    z.a[i][j]=(z.a[i][j]+x.a[i][k]*y.a[k][j]%mod)%mod;
        return z;
    }
    int main()
    {
        r(n);
        if(n<=0){printf("1");return 0;}
        matrix ans;ans.build();
        matrix base;base.pre();
        while(n)
        {
            if(n&1)ans=ans*base;
            base=base*base;
            n>>=1;
        }
        printf("%lld",ans.a[1][1]);
    }
    
  • 1

信息

难度
2
分类
(无)
标签
递交数
61
已通过
37
通过率
61%
被复制
1
上传者