Codeforces 431C. k-Tree

Posted TOTOTOTOTZZZZZ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 431C. 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.

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 (10^9?+?7). 

Input

A single line contains three space-separated integers: nk and d (1?≤?n,?k?≤?100; 1?≤?d?≤?k).

Output

Print a single integer — the answer to the problem modulo 1000000007 (10^9?+?7).

 

动归题,列出式子

   设dp[i][0]是长度为i且其中没有大于等于d的边的方案数,dp[i][1]是长度为i且其中有至少一条大于等于d的边的方案数,则

    i < d时

     dp[i][0] = Σdp[i-a][0] (a∈[1,i]); dp[i][1] = 0

   i >= d

     dp[i][0] = Σdp[a][0] (a∈[i-d+1,i-1])

     dp[i][1] = Σdp[a][0] (a∈[max(0,i-k),i-d]) + Σdp[i-a][1] (a∈[max(0,i-k),i-1])

  本题就是要求dp[n][1]

  然后注意下边界的初始化即可

 

技术分享
#include<iostream>
#include<cstring>
#include<cstdio>
#include <string>
#include <sstream>
#include <map>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
#define MOD 1000000007
LL dp[105][2];
LL n,k,d;
int main(){
  // freopen("test.in","r",stdin);
  cin >> n >> k >> d;
  memset(dp,0,sizeof(dp));
  dp[1][0] = 1; dp[0][0] = 1;
  if (d == 1){
    dp[1][1] = 1; dp[1][0] = 0;
  }
  for (int i=2;i<=n;i++){
    if (i < d){ // dp[i][1] length = n weight >= d
      dp[i][1] = 0;
      // dp[i][0] = Σdp[i-a][0] (a∈[1,i]);
      for (int j=1;j<=i;j++){
        dp[i][0] += dp[i-j][0];
        dp[i][0] %= MOD;
      }
    }
    else {
      // dp[i][0] = Σdp[a][0] (a∈[i-d+1,i-1])
      for (int j=i-d+1;j<=i-1;j++){
        dp[i][0] += dp[j][0];
        dp[i][0] %= MOD;
      }
      // dp[i][1] = Σdp[a][0] (a∈[max(0,i-k),i-d]) + Σdp[i-a][1] (a∈[max(0,i-k),i-1])
      LL now;
      if (i-k>0){
        now = i -k;
      }
      else {
        now = 0;
      }
      for (int j=now;j<=i-1;j++){
        dp[i][1] += dp[j][1];
        dp[i][1] %= MOD;
      }
      for (int j=now;j<=i-d;j++){
        dp[i][1] += dp[j][0];
        dp[i][1] %= MOD;
      }
    }
  }
  // for (int i=1;i<=n;i++){
  //   cout << dp[i][1] << " " << dp[i][0] << endl;
  // }
  cout << dp[n][1] % MOD;
  return 0;
}
View Code

 

 

以上是关于Codeforces 431C. k-Tree的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 431C k-Tree

Codeforces 431C k-Tree(记忆化搜索)

「专题训练」k-Tree(CodeForces Round #247 Div.2 C)

CF431C k-Tree dp

Codeforces Round #431 (Div. 2) C

Codeforces431C_K-Tree_KEY