codeforces431C

本文探讨了CodeForces-431C题目的解决方案,该题目涉及在一种特殊的无限树(K-Tree)中寻找特定权重路径的数量。文章详细介绍了问题背景、核心算法思路以及具体的实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

k-Tree

 CodeForces - 431C 

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 kedges), 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).

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-Tree定义,每个结点都有恰好K个孩子,这棵树无限增长。每个节点到它K个孩子的K条边的权重刚好是1,2,3...,K(看图应该也看得明白)
现在问有多少条路径,使得从根节点出发到达某个结点,经过的边权重之和恰好为n,并且经过的边至少有一条权重不小于d。

sol:dp应该看得出来,状态也很好构建dp[i][j][0,1]表示到第i层和为j是否有不小于d的边,因为n,k太小,毫无思考的n3dp直接上
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0'); return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int Mod=1000000007,N=105;
int n,K,D;
int dp[N][N][2];
int main()
{
    int i,j,k;
    R(n); R(K); R(D);
    dp[0][0][0]=1;
    for(i=1;i<=n;i++)
    {
        for(j=i-1;j<n;j++)
        {
            for(k=1;j+k<=n&&k<=K;k++)
            {
                dp[i][j+k][1]+=1ll*dp[i-1][j][1]%Mod;
                dp[i][j+k][1]-=(dp[i][j+k][1]>=Mod)?Mod:0;
                if(k>=D)
                {
                    dp[i][j+k][1]+=1ll*dp[i-1][j][0]%Mod;
                    dp[i][j+k][1]-=(dp[i][j+k][1]>=Mod)?Mod:0;
                }
                else
                {
                    dp[i][j+k][0]+=1ll*dp[i-1][j][0]%Mod;
                    dp[i][j+k][0]-=(dp[i][j+k][0]>=Mod)?Mod:0;
                }
            }
        }
    }
    int ans=0;
    for(i=1;i<=n;i++)
    {
        ans+=dp[i][n][1];
        ans-=(ans>=Mod)?Mod:0;
    }
    Wl(ans);
    return 0;
}
/*
input
3 3 2
output
3

input
3 3 3
output
1

input
4 3 2
output
6

input
4 5 2
output
7
*/
View Code
 
   

 

 

转载于:https://www.cnblogs.com/gaojunonly1/p/10651702.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值