zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    微服务架构有哪些优势?
    Java 线程数过多会造成什么异常?
    Java 死锁以及如何避免?
    抽象的(abstract)方法是否可同时是静态的(static), 是否可同时是本地方法(native),是否可同时被 synchronized 修饰?
    内部类可以引用它的包含类(外部类)的成员吗?有没有 什么限制?
    CSS选取第几个标签元素:nth-child、first-child、last-child
    数据库约束
    DQL查询语句
    网络编程(客户端,服务端文件上传下载)
    缓冲流,转换流
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/6920212.html
Copyright © 2011-2022 走看看