天天看點

Codeforces Contest 1117 problem D Magic Gems —— 矩陣快速幂

Reziba has many magic gems. Each magic gem can be split into M normal gems. The amount of space each magic (and normal) gem takes is 1 unit. A normal gem cannot be split.

Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is N units. If a magic gem is chosen and split, it takes M units of space (since it is split into M gems); if a magic gem is not split, it takes 1 unit.

How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is N units? Print the answer modulo 1000000007 (109+7). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ.

Input

The input contains a single line consisting of 2 integers N and M (1≤N≤1018, 2≤M≤100).

Output

Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is N units. Print the answer modulo 1000000007 (109+7).

Examples

inputCopy

4 2

outputCopy

5

inputCopy

3 2

outputCopy

3

Note

In the first example each magic gem can split into 2 normal gems, and we know that the total amount of gems are 4.

Let 1 denote a magic gem, and 0 denote a normal gem.

The total configurations you can have is:

1111 (None of the gems split);

0011 (First magic gem splits into 2 normal gems);

1001 (Second magic gem splits into 2 normal gems);

1100 (Third magic gem splits into 2 normal gems);

0000 (First and second magic gems split into total 4 normal gems).

Hence, answer is 5.

題意:

你有很多的魔法寶石,你可以将一個魔法寶石分成m個普通的寶石,問你得到n個寶石有多少種方法,注意分解不同的魔法寶石是不同的。

題解:

狀态轉移方程很簡單就可以得到:dp[i]=dp[i-1]+dp[i-m],那麼用矩陣快速幂就可以了,因為m隻有100.

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
int maxn;
struct Matrix
{
    ll m[105][105];
    Matrix()
    {
        memset(m,0,sizeof(m));
    }
    void init()
    {
        for(int i=0; i<maxn; i++)
            for(int j=0; j<maxn; j++)
                m[i][j]=(i==j);
    }
    Matrix  operator +(const Matrix &b)const
    {
        Matrix c;
        for(int i=0; i<maxn; i++)
        {
            for(int j=0; j<maxn; j++)
            {
                c.m[i][j]=(m[i][j]+b.m[i][j])%mod;
            }
        }
        return c;
    }
    Matrix  operator *(const Matrix &b)const
    {
        Matrix c;
        for(int i=0; i<maxn; i++)
        {
            for(int j=0; j<maxn; j++)
            {
                for(int k=0; k<maxn; k++)
                {
                    c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%mod)%mod;
                }
            }
        }
        return c;
    }
    Matrix  operator^(const ll &t)const
    {
        Matrix ans,a=(*this);
        ans.init();
        ll n=t;
        while(n)
        {
            if(n&1) ans=ans*a;
            a=a*a;
            n>>=1;
        }
        return ans;
    }
};
int main()
{
    ll n;
    scanf("%lld%lld",&n,&maxn);
    if(n<maxn)
        return 0*printf("1\n");
    Matrix temp;
    temp.m[0][0]=1,temp.m[0][maxn-1]=1;
    for(int i=1;i<maxn;i++)
        temp.m[i][i-1]=1;
    Matrix aa,bb;
    for(int i=0;i<maxn;i++)
        bb.m[i][0]=1;
    aa=temp^(n-maxn+1);
    aa=aa*bb;
    cout<<aa.m[0][0]%mod<<endl;
    return 0;
}