Educational Codeforces Round 60 (Rated for Div. 2) D. Magic Gems

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Reziba has many magic gems. Each magic gem can be split into
M
M
normal gems. The amount of space each magic (and normal) gem takes is
1
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
N
units. If a magic gem is chosen and split, it takes
M
M
units of space (since it is split into
M
M
gems); if a magic gem is not split, it takes
1
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
N
units? Print the answer modulo
1000000007
1000000007
(
10
9
+7
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
2
integers
N
N
and
M
M
(
1≤N≤
10
18
1≤N≤1018
,
2≤M≤100
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
N
units. Print the answer modulo
1000000007
1000000007
(
10
9
+7
109+7
).
Examples
Input
Copy
4 2
Output
Copy
5
Input
Copy
3 2
Output
Copy
3
Note
In the first example each magic gem can split into
2
2
normal gems, and we know that the total amount of gems are
4
4
.
Let
1
1
denote a magic gem, and
0
0
denote a normal gem.
The total configurations you can have is:
1111
1111
(None of the gems split);
0011
0011
(First magic gem splits into
2
2
normal gems);
1001
1001
(Second magic gem splits into
2
2
normal gems);
1100
1100
(Third magic gem splits into
2
2
normal gems);
0000
0000
(First and second magic gems split into total
4
4
normal gems).
Hence, answer is
5
5
.

题解:有一些魔法宝石,魔法宝石可以分成m个普通宝石,每个宝石(包括魔法宝石)占用1个空间,让你求占用n个空间的方法有几种,有不同数量的魔法宝石和不同分法的方法算不同的方法,
根据样例可得递推式:f[n]=f[n-1]+f[n-m],最后一个不分加上最后一个分.
因为n比较大,可以用矩阵快速幂来求,这里当n<m,只有一种方法,n==m时有2种方法.
#include <bits/stdc++.h>
const int MOD=1e9+7;
const int M=105;
using namespace std;
typedef long long ll;
struct matrix{
    ll m[M][M];
}ans,res;
matrix mul(matrix a,matrix b,int n){
    matrix tmp;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            tmp.m[i][j]=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            for(int k=1;k<=n;k++){
                tmp.m[i][j]+=a.m[i][k]*b.m[k][j];
                tmp.m[i][j]%=MOD;
            }
    return tmp;
}
void quickpower(ll n,int m){
    for(int i=1;i<=m;i++){
        res.m[i+1][i]=1;
        for(int j=1;j<=m;j++){
            if(i==j) ans.m[i][j]=1; else ans.m[i][j]=0;
        }
    }
    res.m[1][m]=1;res.m[m][m]=1;
    while(n){
        if(n&1){
            ans=mul(ans,res,m);
        }
        res=mul(res,res,m);
        n>>=1;
    }
}
int main(){
    ll n;
    int m;
    scanf("%I64d%d",&n,&m);
    if(n<m){
        printf("1\n");
        return 0;
    }
    matrix a;
    for(int i=1;i<m;i++) a.m[1][i]=1;
    a.m[1][m]=2;
    quickpower(n-m,m);
    a=mul(a,ans,m);
    printf("%I64d\n",a.m[1][m]);
    //cout << "Hello world!" << endl;
    return 0;
}

转载于:https://www.cnblogs.com/-yjun/p/10427824.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值