Codeforces Round #247 (Div. 2)C. k-Tree(动态规划)

传送门

Description

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.

 

 

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 Input

3 3 2

3 3 3

4 3 2

4 5 2

Sample Output

3

1

6

7

思路

题意:

 给定一个k叉树,并且边的权重为1,2,...,k,问有多少条路径,使得路径上的边的权重之和为n,并且这条路径上至少存在一条边其权重不小于d。

题解:

首先考虑有多少条路径,其权重之和为n,用dp[n]代表权重和为n的方案,那么由于是k叉树,那么dp[n] = dp[n - 1] + dp[n - 2] + ... + dp[n - k];现在考虑路径上至少有一条边权重不小于d的情况,用dp[n][1]代表权重之和为1且存在至少一条边不小于d的方案数,那么dp[n][0]代表权重之和为n且路径上每一条边都小于d的方案数。所以当前权重为x时,继续下一步,那么选择的边权为y的分支时,转移方程:当y < d dp[x + y][0] += dp[x][0] 否则dp[x + y][1] += dp[x][0],不管y 是否大于 d,都有dp[x + y][1] += dp[x][1];

 

#include
using namespace std;
typedef __int64 LL;
const int maxn = 105;
const int mod = 1e9+7;
LL dp[maxn][2];

int main()
{
	LL n,k,d;
	memset(dp,0,sizeof(dp));
	scanf("%I64d%I64d%I64d",&n,&k,&d);
	dp[0][0] = 1;
	for (int i = 0;i <= n;i++)
	{
		for (int j = 1;j <= k;j++)
		{
			if (i + j > n)	continue;
			if (j < d)	dp[i + j][0] += dp[i][0];
			else dp[i + j][1] += dp[i][0];
			
			dp[i + j][1] += dp[i][1];
			
			dp[i + j][0] %= mod;
			dp[i + j][1] %= mod;
		}
	}
	printf("%I64d\n",dp[n][1]);
	return 0;
}

  

import java.util.*;
import static java.lang.System.out;

public class KTree {
    static final int MOD = (int) (1e9+7);
    public static void main(String[] args){
        long[][] dp = new long[105][2];
        int n,k,d;
        Scanner scan = new Scanner(System.in);
        n = scan.nextInt();
        k = scan.nextInt();
        d = scan.nextInt();
        dp[0][0] = 1;
        for (int i = 0;i <= n;i++){
            for (int j = 1;j <= k;j++){
                if (i + j > n)  continue;

                if (j < d)  dp[i + j][0] += dp[i][0];
                else dp[i + j][1] += dp[i][0];

                dp[i + j][1] += dp[i][1];

                dp[i + j][0] %= MOD;
                dp[i + j][1] %= MOD;
            }
        }
        out.println(dp[n][1]);
    }
}

  

 

转载于:https://www.cnblogs.com/ZhaoxiCheung/p/7287807.html

你可能感兴趣的:(数据结构与算法,java)