CodeForces431 C.k-Tree (dp)

C. 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.

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

思路:
容易想到:
先用边权为1-k的边求出距离为n的方案数,
然后只用边权小于d的边求出距离为n的方案数,
两者相减就是至少用一条大于等于d的边的方案数,

先想二维,比较无脑
每一层的边权为1-k,所以最多n层(n层都走边权为1的边).

d[i][j]表示第i层下边权和为j的方案数
则d[i][j]=sigma d[i-1][j-x]; x为每层可选的边权
循环n次求出n层每一层的方案数就行了
因为每次更新只需要用到上一层的,所以空间还可以优化(就不写了)

一维:
d[i]表示边权和为i的方案数
d[0]=1;
for(1->n)  n层
d[i]+=d[i-j]; j为可选的边权,且j<=i

也是求完两种然后相减就行了
code1:

//二维

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define int long long
const int maxm=105;
const int mod=1e9+7;
int d[maxm][maxm];//用所有边的方案数
int dd[maxm][maxm];//用小于dis的边的方案数
int n,k,dis;
signed main(){
    cin>>n>>k>>dis;
    for(int i=1;i<=k;i++){//第1层
        d[1][i]=1;
    }
    for(int c=2;c<=n;c++){//第2-n层
        for(int i=1;i<=k;i++){
            for(int j=i;j<=n;j++){
                d[c][j]+=d[c-1][j-i];
                d[c][j]%=mod;
            }
        }
    }
    k=dis-1;//直接把k改成dis-1就行了
    for(int i=1;i<=k;i++){//第1层
        dd[1][i]=1;//只用小于dis的
    }
    for(int c=2;c<=n;c++){//第2-n层
        for(int i=1;i<=k;i++){//只用小于dis的
            for(int j=i;j<=n;j++){
                dd[c][j]+=dd[c-1][j-i];
                dd[c][j]%=mod;
            }
        }
    }
    int ans=0;
    for(int c=1;c<=n;c++){//计算所有层的答案和
        ans+=d[c][n];
        ans%=mod;
        ans-=dd[c][n];//减去只用小于dis的边的方案数
        if(ans<0)ans+=mod;//小于0记得加回去
    }
    cout<<ans<<endl;
    return 0;
}
code2:

//一维

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define int long long
const int maxm=105;
const int mod=1e9+7;
int d[maxm];
int dd[maxm];
int n,k,dis;
signed main(){
    cin>>n>>k>>dis;
    d[0]=dd[0]=1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=k&&j<=i;j++){
            d[i]+=d[i-j];
            d[i]%=mod;
        }
    }
    k=dis-1;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=k&&j<=i;j++){
            dd[i]+=dd[i-j];
            dd[i]%=mod;
        }
    }
    int ans=d[n]-dd[n];
    if(ans<0)ans+=mod;
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值