EducationalCodeforcesRound60D. Magic Gems

28 篇文章 0 订阅
13 篇文章 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
Input

4 2

Output

5

Input

3 2

Output

3

现在有一款魔力宝石,一块能顶 m 块,现在选一些魔力宝石,你可以选择分裂一些,或者全分裂,再或者不分裂,但是要求这些宝石的总体积为 n 。求一共有多少种组合方法。
一开始的时候,没有特别明显的方法,于是就先以分裂为2块为例,列出了前5项,没想到写出来竟然是斐波那契数列,所以这个题是不是有递推关系呢?
对于总体积为n,分裂数为m的情况,显然,当n<m时,有且只有一种;只有当n>=m时,才会有分裂的选择。所以,可以选择在体积为 n-1 时,添加一块不分裂,也可以在体积为 n-m 时,添加一块分裂。综上所述,kind[n]=kind[n-1]+kind[n-m]
但是由于数据量的关系,不能用普通dp或者迭代,因为内存不支持,所以在这里要使用矩阵的形式改写上述关系式,并用矩阵快速幂来求出结果。
首先是设计矩阵,不妨首先考虑体积为 m 时
在这里插入图片描述
这样就构造出系数矩阵,并且要注意,对于体积为 n 时,只需要计算系数矩阵的 n-m+1 次方,并不是 n-m 次方。
在得到系数矩阵以及其幂次之后,就是要进行幂的运算,矩阵乘法不进行详细描述,请自行百度。
快速幂则是将幂次分解。以二进制为例,1e19的二进制中,最高位也不过70次方,也就是说,70次幂以内,就i可以得到其1e19次幂,这样极大的优化了时间复杂度。所以现在只需要根据二进制数,在合适的位置上加即可。注意应用到矩阵快速幂中,不定义矩阵的零次幂,或者说以原矩阵初始化,这时计算的幂次要-1 。


#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>

#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return

#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
const int mod=1e9+7;

int m;
ll n;
void Qpow(ll p,int c[][105]);
void mul(int lf[][105],int rg[][105]);
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;

    if(n<m){
        cout<<1<<endl;
    }else{
        int c[105][105];
        memset(c,0,sizeof(c));
        rep(i,0,m-1)
            c[i][i+1]=1;
        c[m-1][0]=c[m-1][m-1]=1;

        int ans=0;
        Qpow(n-m+1,c);

        rep(i,0,m){
            ans=(ans+c[m-1][i])%mod;
        }
        cout<<ans<<endl;
    }
    re 0;
}
void Qpow(ll p,int c[][105]){
    int rec[105][105];
    rep(i,0,m)
        rep(j,0,m)
            rec[i][j]=c[i][j];
            
    p--;
    while(p!=0){
        if(p%2){
            mul(c,rec);
            p--;
        }
        mul(rec,rec);

        p/=2;
    }

}
void mul(int lf[][105],int rg[][105]){
    ll tem[m][m];
    memset(tem,0,sizeof(tem));
    rep(i,0,m){
        rep(j,0,m){
            rep(k,0,m)
                tem[i][j]=(tem[i][j]+(1LL*lf[i][k]*rg[k][j])%mod)%mod;
        }
    }

    rep(i,0,m)
        rep(j,0,m)
            lf[i][j]=tem[i][j];
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值