Codeforces 431C k-Tree【dp】

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.

A 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: n, k and d (1 ≤ n, k ≤ 100; 1 ≤ d ≤ k).

Output

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

Examples
Input
3 3 2
Output
3
Input
3 3 3
Output
1
Input
4 3 2
Output
6
Input
4 5 2
Output
7

题目大意:

给你一个K树,K树的定义:

每个节点都有K个孩子,每条树边都有权值,并且树边权值是从1-k排布的,如题干中图片所示。

然后让你找一共有多少条路,使得从根节点出发,走完这条路之后,权值总和为n,并且保证至少有一条树边的权值大于等于d、答案对1e9+7取膜。


思路;


1、经典的dp统计方案数的类型题。我们设定dp【i】【j】【2】,其中dp【i】【j】【0】表示当前走到第i层,前i层的树边总和为j,并且还没有一条树边的权值大于等于d的方案数。那么dp【i】【j】【1】表示当前走到第i层,前i层树边综合为j,并且右至少一条树边的权值大于等于d的方案数。


2、那么不难想到其状态转移方程:

①dp【i】【j】【1】+=dp【i-1】【k】【1】(k<j)

表示走到第i层,选择走的路的权值为j-k,并且这条路上至少有一条树边的权值大于等于d的方案数的状态转移。

然后我们讨论当前选择的这条路的权值j-k所带来的变化:

②dp【i】【j】【0】+=dp【i-1】【k】【0】(j-k<d)

如果当前选择的这条路是小于d的,那么其当前走到了第i层权值为j的不存在一条树边大于等于d的方案数,只能从走到第i-1层权值和为k的不存在一条树边大于等于d的方案数转移过来。

③dp【i】【j】【1】+=dp【i-1】【k】【0】(j-k>=d)

如果当前选择的这条路是大于等于d的,那么其当前走到了第i层权值为j的情况,就是一个包含了树边大于等于d的情况,那么走到第i-1层权值和为k的不存在一条树边大于等于d的状态就可以转移到走到第i层权值和为j的存在一条树边大于等于d的状态上来。


3、注意取膜。


Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
int mod=1e9+7;
int dp[150][150][2];
int main()
{
    int n,k,d;
    while(~scanf("%d%d%d",&n,&k,&d))
    {
        memset(dp,0,sizeof(dp));
        dp[0][0][0]=1;
        for(int i=1;i<=100;i++)
        {
            for(int j=0;j<=n;j++)
            {
                for(int kk=0;kk<j;kk++)
                {
                    int tmp=j-kk;
                    if(tmp>k)continue;
                    if(tmp>=d)
                    {
                        dp[i][j][1]+=dp[i-1][kk][0];
                        dp[i][j][1]%=mod;
                        dp[i][j][1]+=dp[i-1][kk][1];
                        dp[i][j][1]%=mod;
                    }
                    else
                    {
                        dp[i][j][0]+=dp[i-1][kk][0];
                        dp[i][j][0]%=mod;
                        dp[i][j][1]+=dp[i-1][kk][1];
                        dp[i][j][1]%=mod;
                    }
                }
            }
        }
        int ans=0;
        for(int i=1;i<=100;i++)
        {
            ans+=dp[i][n][1];
            ans%=mod;
        }
        printf("%d\n",ans);
    }
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值