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

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

Sample Input
Input
3 3 2
Output
3
Input
3 3 3
Output
1
Input
4 3 2
Output
6
Input
4 5 2
Output
7
分析:
窝觉得另一个同学分析的很好这其实就是一个超级阶梯问题。
唯一变化的就是多了一个限制条件:必须要有权值大于等于b的边。
f[i]表示能组成重量为i的所有方案数。
多了的这个限制条件我们可以多开一维[0/1]来辅助。
f[i][0] 表示f[i][0] 的所有方案中都不含边权大于等于b的边。
f[i][1] 表示f[i][1] 的所有方案中都含有。
那么转移的时候分两种情况:
1:如果从j这条边转移过来,j边权大于等于b 则
f[i][1]=∑(f[i-j][0]+f[i-j][1])(b≤j≤k)
因为肯定有边符合条件所以可以从两个地方转移过来。但这时候f[i][0] 就没有地方可以转移过来了,所以不管他了=-=
2:如果j

// Created by ZYD in 2015.
// Copyright (c) 2015 ZYD. All rights reserved.
//

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
// #define mo 1e9+7
const int mo=1e9+7;
#define ll long long
#define mk make_pair
#define pb push_back
#define mem(array) memset(array,0,sizeof(array))
typedef pair<int,int> P;
int n,k,d;
long long  dp[105][2];
int main()
{
    freopen("in.txt","r",stdin);
    scanf("%d %d %d",&n,&k,&d);
    dp[0][0]=1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=k;j++)
            if(i>=j){
                if(j>=d){
                    dp[i][1]+=dp[i-j][0]+dp[i-j][1];
                }           
                else {
                    dp[i][1]+=dp[i-j][1];
                    dp[i][0]+=dp[i-j][0];

                }
                dp[i][1]=dp[i][1] % mo;
                dp[i][0]=dp[i][0] % mo;
            }
    printf("%lld\n",dp[n][1]);      
    return 0;
}


你可能感兴趣的:(codeforces)