思路:矩阵快速幂,根据递推的特性列出矩阵,直接求解,主要注意n<m的情况
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
ll n, m;
const int N = 105;
const ll mod = 1000000007;
struct mat {
ll x[N][N];
mat() {
memset(x, 0, sizeof x);
}
};
mat operator *(const mat &a, const mat &b) {
mat ans;
for(int i = 0; i < m; i++)
for(int j = 0; j < m; j++)
for(int k = 0; k < m; k++)
ans.x[i][j] = (ans.x[i][j] + a.x[i][k] * b.x[k][j] % mod) % mod;
return ans;
}
mat operator ^ (mat &a, ll n) {
mat ret;
for(int i = 0; i < m; ++i)
ret.x[i][i] = 1;
while(n) {
if(n & 1)
ret = ret * a;
a = a * a;
n >>= 1;
}
return ret;
}
int main() {
cin >> n >> m;
if(n < m) {
cout << 1 << endl;
return 0;
}
mat a;
for(int i = 0; i < m - 1; i++)
a.x[i + 1][i] = 1;
a.x[0][0] = a.x[0][m - 1] = 1;
mat ans = a ^ (n - m);
ll ak = ans.x[0][0] * 2 % mod;
for(int i = 1; i < m; i++)
ak = (ak + ans.x[0][i]) % mod;
cout << ak;
return 0;
}