k-Tree
C. k-Tree
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputQuite 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.
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 test(s)
input
3 3 2
output
3
input
3 3 3
output
1
input
4 3 2
output
6
input
4 5 2
output
7
DP题:这题的n不大,才100.假设我们走到某个节点的权值和为n,那么上一个节点的权值就为n-i(1 <= i <= k)。那么其实权值和为n的路径数 d[n] = d[n-1] + d[n-2] +...+d[n-k]。
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <map> #include <iostream> #include <vector> using namespace std; #define LL long long #define MOD 1000000007 int n,k,d; LL dp[2][105]; int main(){ while(~scanf("%d %d %d", &n, &k, &d)){ memset(dp,0,sizeof(dp)); dp[0][0] = dp[1][0] = 1; for(int i = 1; i <= n; i++){ for(int j = 1; j <= k; j++){ if(j > i) break; dp[0][i] = (dp[0][i]+dp[0][i-j])%MOD; } for(int j = 1; j < d; j++){ if(j > i) break; dp[1][i] = (dp[1][i]+dp[1][i-j])%MOD; } } printf("%I64d\n", ((dp[0][n]-dp[1][n])%MOD+MOD)%MOD); } return 0; }