k-Tree

Quite recently a creative student Lesha had a lecture on trees. After the lecture Lesha was inspired and came up with the tree of his own which he called a k-tree.

k-tree is an infinite rooted tree where:

  • each vertex has exactly k children;
  • each edge has some weight;
  • if we look at the edges that goes from some vertex to its children (exactly k edges), then their weights will equal 1, 2, 3, ..., k.

As soon as Dima, a good friend of Lesha, found out about the tree, he immediately wondered: "How many paths of total weight n (the sum of all weights of the edges in the path) are there, starting from the root of a k-tree and also containing at least one edge of weight at least d?".

Help Dima find an answer to his question. As the number of ways can be rather large, print it modulo 1000000007 (10^9 + 7).

Input

A single line contains three space-separated integers: nk and d (1 ≤ n, k ≤ 100; 1 ≤ d ≤ k).

Output

Print a single integer — the answer to the problem modulo 1000000007 (10^9 + 7).

dp题,记录前c层总和为且总和为p的路径,分别储存含有大于等于d的边的与不含的路径数

dp(t,c,p)代表前c+1层总和为p的路径,t=0时为不含大于等于d的边的,t=1时为包含的路径数

dp(1,c,p)=dp(0,c-1,p-d)+dp(0,c-1,p-d-1)+…+dp(0,c-1,max{p-k,0})+dp(1,c-1,p-1)+dp(1,c-1,p-2)+…+dp(1,c-1,max{p-k,0})

dp(0,c,p)=dp(0,c-1,p-1)+dp(0,c-1,p-2)+…+dp(0,c-1,max{p-d+1,0})

#include<bits/stdc++.h>
using namespace std;
const int MOD=1e9+7;
int dp[2][105][105],ans;
int main(){
	int n,k,d;
	cin>>n>>k>>d;
	for(int i=1;i<d;i++)
		dp[0][0][i]=1;
	for(int i=d;i<=k;i++)
		dp[1][0][i]=1;
	for(int i=1;i<=n-d+1;i++)
		for(int j=1;j<=n;j++){
			for(int t=max(j-k,0);t<=j-d;t++)
				dp[1][i][j]+=dp[0][i-1][t],dp[1][i][j]%=MOD;
			for(int t=max(j-k,0);t<=j-1;t++)
				dp[1][i][j]+=dp[1][i-1][t],dp[1][i][j]%=MOD;
			for(int t=max(0,j-d+1);t<=j-1;t++)
				dp[0][i][j]+=dp[0][i-1][t],dp[0][i][j]%=MOD;
		}
	for(int i=0;i<=n-d+1;i++) 
		ans+=dp[1][i][n],ans%=MOD;
	cout<<ans;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/u013455437/article/details/109264778