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

202 篇文章 5 订阅
4 篇文章 0 订阅

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值