• 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
  • 相关阅读:
    R tips
    向量化与并行计算
    cf relevent R package
    一篇关于相似性解释的文章,写得非常的仔细
    lsa cosine R
    install lsa package for R on ubuntu 10.04 lts lucid
    updatealternatives error no alternatives for xulrunner1.9javaplugin.so 问题解决
    《如何在windows程序中读取bios内容》
    php数组根据某一个键值,把相同键值的合并生成一个新的二维数组
    阿里云域名备案之如何填写真实性核验单
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/6920212.html
Copyright © 2020-2023  润新知