[codefoeces] Educational Codeforces Round 60 D. Magic Gems

time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 11 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 11 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 10000000071000000007 (109+7109+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 22 integers 𝑁N and 𝑀M (1𝑁10181≤N≤1018, 2𝑀1002≤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 10000000071000000007 (109+7109+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 22 normal gems, and we know that the total amount of gems are 44.

Let 11 denote a magic gem, and 00 denote a normal gem.

The total configurations you can have is:

  • 11111111 (None of the gems split);
  • 00110011 (First magic gem splits into 22 normal gems);
  • 10011001 (Second magic gem splits into 22 normal gems);
  • 11001100 (Third magic gem splits into 22 normal gems);
  • 00000000 (First and second magic gems split into total 44 normal gems).

Hence, answer is 55.

 

矩阵快速幂

 1 #include <bits/stdc++.h>
 2 #define M 101
 3 using namespace std;
 4 typedef long long ll;
 5 ll n,m;
 6 const int mod = 1e9+7;
 7 struct Mat{
 8     ll a[M][M];
 9     Mat(){
10         memset(a,0,sizeof(a));
11     }
12 };
13 Mat operator * (Mat A,Mat B){
14     Mat ret;
15     for (int i = 0;i < m;++i){
16         for (int j = 0;j < m;++j){
17             ll tmp = 0;
18             for (int k = 0;k < m;++k){
19                 tmp = (tmp + A.a[i][k]*B.a[k][j]) % mod;
20             }
21             ret.a[i][j] = tmp;
22         }
23     }
24     return ret;
25 }
26 
27 Mat power(Mat mat,ll n){
28     Mat ret;
29     for (int i = 0;i < m;++i){
30         ret.a[i][i] = 1;
31     }
32     while(n){
33         if (n&1) ret = ret*mat;
34         n >>= 1;
35         mat = mat*mat;
36     }
37     return ret;
38 }
39 
40 int main(){
41     scanf("%lld%lld",&n,&m);
42     if (n < m) return 0 * puts("1");
43     Mat mat;
44     mat.a[0][0] = mat.a[0][m-1] = 1;
45     for (int i = 1;i < m;++i) mat.a[i][i-1] = 1;
46     mat = power(mat,n-m+1);
47     ll ans = 0;
48     for (int i = 0;i < m;++i){
49         ans = (ans + mat.a[0][i]) % mod;
50     }
51     printf("%lld\n",ans);
52     return 0;
53 }

 

posted @ 2019-02-19 16:42  mizersy  阅读(368)  评论(0编辑  收藏  举报