Codeforces Round #247 (Div. 2) C. k-Tree ###K //K
题目链接:https://codeforces.com/problemset/problem/431/C
题意:给定有根的无限节点的k叉树 问有多少条路径 使得和为n 并且至少有一条边的长度大于d
思路:dp[i][0/1] 代表 和为i 的 没有/有经过至少一条边长为d的方案数
每次可以往结尾加的数是 1~k 所以转移方程就是 dp[i][0/1] dp[i-j][0/1] 区别于背包问题,背包的方案数是无序的这个是有序的
0
View Code
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1e2+10; 4 const int mod=1e9+7; 5 #define ll long long 6 #define ull unsigned long long 7 #define pi pair<int,ll> 8 #define fi first 9 #define sc second 10 #define pb push_back 11 ll dp[maxn][2]; 12 13 14 15 int main() 16 { 17 ios::sync_with_stdio(0); 18 cin.tie(0); 19 int n,k,d; 20 cin>>n>>k>>d; 21 dp[0][0]=1; 22 for(int i=1;i<=n;i++) 23 { 24 for(int j=1;j<=k;j++) 25 { 26 if(i-j<0) break; 27 if(j>=d) 28 { 29 dp[i][1]+=dp[i-j][1]; 30 dp[i][1]%=mod; 31 dp[i][1]+=dp[i-j][0]; 32 dp[i][1]%=mod; 33 } 34 else 35 { 36 dp[i][0]+=dp[i-j][0]; 37 dp[i][0]%=mod; 38 dp[i][1]+=dp[i-j][1]; 39 dp[i][1]%=mod; 40 } 41 } 42 } 43 cout<<dp[n][1]<<'\n'; 44 45 46 }
状态机模型 当j>=d 时 有->有 无->有 当j<d 时 有->有 无->无

浙公网安备 33010602011771号