题目概述
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.
题目链接
浅析
严格说来这题应该不算是与树有关的问题
对于这样一个求路径数的问题,我们可以把边权当做背包的重量, n当作背包的容量,总方案数当作价值。于是就转化成了一个完全背包问题。因为要求至少有一条边的权值大于等于d,所以我们可以开个二维数组dp[N][2], dp[i][1]表示总权值为i切满足条件的总数, dp[i][0]表示不满足权值大于等于d的总数,即所有的方案都是用权值小于d的背包凑出的,由此我们可以得到一下状态转移方程。
dp[i][1] = (dp[i][1] + dp[i - j][1]) % mod;
//根据j的取值去进一步讨论更新
if (j < d){
dp[i][0] = (dp[i][0] + dp[i - j][0]) % mod;
}else{
dp[i][1] = (dp[i][1] + dp[i - j][0]) % mod;
}
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 105;
const int mod = 1e9 + 7;
int n, k, d;
int dp[N][2];
int main() {
ios::sync_with_stdio(false);
cin.tie(), cout.tie();
cin >> n >> k >> d;
dp[0][0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= min(i, k); ++j) {
dp[i][1] = (dp[i][1] + dp[i - j][1]) % mod;
if (j < d){
dp[i][0] = (dp[i][0] + dp[i - j][0]) % mod;
}else{
dp[i][1] = (dp[i][1] + dp[i - j][0]) % mod;
}
}
}
printf("%d\n",dp[n][1]);
return 0;
}