Codeforces Round #247 (Div. 2) C. k-Tree

C. k-Tree
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

The picture below shows a part of a 3-tree.

Codeforces Round #247 (Div. 2) C. k-Tree_第1张图片

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 (109 + 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 (109 + 7).

Sample test(s)
input
3 3 2
output
3
input
3 3 3
output
1
input
4 3 2
output
6
input
4 5 2
output
7


简单dp,dp[i][j][k]表示前i层,和为j,路径上最大值为k的路径数。
#include 
#include 
#include 
using namespace std;

#define clr(a) memset(a,0,sizeof(a));
typedef long long ll;
const ll mod=1000000007;
ll dp[2][101][101];
int d,k,n,now;

int main(){
	scanf("%d%d%d",&n,&k,&d);
	now=0;clr(dp[now]);
	ll ans=0;
	for(int i=1;i<=k;i++) dp[now][i][i]=1;
	for(int j=d;j<=k;j++){
		ans+=dp[now][n][j];
		if(ans>=mod) ans-=mod;
	}		
	for(int i=2;i<=n;i++){
		now^=1;clr(dp[now])
		for(int j=1;j<=n;j++){
			for(int r=1;r<=k;r++){
				if(dp[1-now][j][r]){
					for(int q=1;q<=r;q++){
						if(j+q<=n){
							dp[now][j+q][r]+=dp[1-now][j][r];
							if(dp[now][j+q][r]>=mod) dp[now][j+q][r]-=mod;
						}
					}
					for(int q=r+1;q<=k;q++){
						if(j+q<=n){
							dp[now][j+q][q]+=dp[1-now][j][r];
							if(dp[now][j+q][q]>=mod) dp[now][j+q][q]-=mod;
						}
					}
				}
			}
		}
		for(int j=d;j<=k;j++){
			ans+=dp[now][n][j];
			if(ans>=mod) ans-=mod;
		}		
	}
	printf("%I64d\n",ans);
	return 0;
}


你可能感兴趣的:(ACM)