zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 60 D. Magic Gems

    易得递推式为f[i]=f[i-1]+f[i-M] 最终答案即为f[N]. 由于N很大,用矩阵快速幂求解。

    code:

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int MOD=1e9+7;
    ll n,m;
    struct mat
    {
        ll a[105][105];
    };
    mat mat_mul(mat x,mat y)
    {
        mat res;
        memset(res.a,0,sizeof(res.a));
        for(int i=0;i<m;i++)
            for(int j=0;j<m;j++)
            for(int k=0;k<m;k++)
            res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j])%MOD;
        return res;
    }
    void mat_pow(ll n)
    {
        mat c,res;
        c.a[0][0]=c.a[0][m-1]=1;
        for(int i=1;i<m;i++)
        {
            c.a[i][i-1]=1;
        }
        
        memset(res.a,0,sizeof(res.a));
        for(int i=0;i<m;i++) res.a[i][i]=1;
        n--;
        while(n)
        {
            if(n&1) res=mat_mul(res,c);
            c=mat_mul(c,c);
            n=n>>1;
        }
        c=res;
        ll ans=c.a[m-1][0]*2%MOD;
        for(int j=1;j<m;j++)ans=(ans+c.a[m-1][j])%MOD;
        cout<<ans<<endl;
    }
    int main()
    {
        cin>>n>>m;
            mat_pow(n);
        return 0;
    }
    
    
  • 相关阅读:
    Android中设置APP应用字体不缩放,文字不随系统字体大小变化
    day02 作业
    day01
    2018.11.2
    2018.11.1
    2018.10.25
    2018.10.24
    2018.10.23
    2018.10.20
    2018.10.19学习总结
  • 原文地址:https://www.cnblogs.com/linruier/p/10596580.html
Copyright © 2011-2022 走看看