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;
    }
    
    
  • 相关阅读:
    做了点数论的题
    2016日报总结
    2016在H3C
    2017春节小目标
    2016项目小总结
    明明的随机数
    计算字符个数
    字符串最后一个单词的长度
    字符串分隔
    考研回忆
  • 原文地址:https://www.cnblogs.com/linruier/p/10596580.html
Copyright © 2011-2022 走看看