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;
}

猜你喜欢

转载自blog.csdn.net/linruier2017/article/details/87709171
今日推荐