codeforces-431C-k-Tree【dp】

原创 2015年11月19日 20:41:55

codeforces-431C-k-Tree【dp】


                time limit per test1 second     memory limit per test256 megabytes

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.
这里写图片描述

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 (109 + 7).

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

Output
Print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

input
3 3 2
output
3

input
3 3 3
output
1

input
4 3 2
output
6

input
4 5 2
output
7

题目链接:cf-431C

题目大意:满k叉树,每个顶点有k条边,边的权重为1~k,现求出从根节点出发,有多少条路径,使得总权值恰好为N,并且每条路径上至少有一条权值不少于d的边。

题目思路:dp[i][h],第一个参数从1-n,遍历权值为i的情况,h有两种情况,0和1,0表示权值为i的所有情况,1表示权值为i的时候路径中存在一个值大于d的情况。

状态转移方程:

//权值为i的处理所有情况

    dp[i][0] = dp[i][0] + dp[i - j][0]

//处理权值为i且存在边大于等于d的情况

        /* if (j >= d) dp[i][1] = dp[i][1] + dp[i - j ][0]   

           else dp[i][1] = dp[i][1] + dp[i - j ][1]        */

        即:dp[i][1] = dp[i][1] + dp[i - j][j<d]

所以结果为dp[n][1]

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
int dp[105][105];
#define mod 1000000007
int main(){
    int n,k,d;
    cin >> n >> k >> d;
    dp[0][0] = 1;               
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= min(k,i); j++)
        {
            dp[i][0] = (dp[i][0] + dp[i - j][0]) % mod;
            dp[i][1] = (dp[i][1] + dp[i - j][j<d]) % mod;
        }
    }
    cout << (dp[n][1] % mod) << endl;
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Codeforces 431C k-Tree (基础dp)

Codeforces 431C k-Tree (基础dp)

CodeForces 431C k-Tree(dp)

Quite recently a creative student Lesha had a lecture on trees. After the lecture Lesha was inspired...

Codeforces 431C k-Tree【dp】

C. k-Tree time limit per test 1 second memory limit per test 256 megabytes ...

Round 3 F - k-Tree CodeForces - 431C - K叉树 树形DP

题目链接: http://codeforces.com/problemset/problem/431/C大意: 无限的完全 K 叉树,每层边权值分别为 1.2.3…K 要求至少经过一个 大于 ...
  • Joovo
  • Joovo
  • 2017-07-17 15:15
  • 108

CF 431 C k-Tree(有序完全背包)

题目链接: CF 431 C k-Tree 题意: 有一个K叉树,每个节点都有k个子节点,每条边的权值依次是1,2,3…k。问由根节点出发经历节点的权值和组成n的路径有多少条?并且每条路径的最大...

CF 431C k-Tree

C. k-Tree time limit per test 1 second memory limit per test 256 megabytes input s...

Codeforces Round #247 (Div. 2) C.K-Tree

解题思路: 一开始觉得是树状DP,后来觉得是

Codeforces #247 (Div. 2) C. k-Tree

题意就不说了 我上来就用暴力回溯写了,这几天练暴力练得脑残了 暴力的

【周赛】第一周周赛——欢迎16级的新同学题解(题目出自codeforces 318A,546A,431C,665E,HDU 4104)

A题: A题题目链接 A题题目描述: Home W的数学 TimeLimit:1000MS  MemoryLimit:256MB 64-bit integer IO format:%I64...

CodeForces 238C World Eater Brothers(tree dp)

Description CodeForces 238C World Eater Brothers(tree dp) You must have heard of the two brothers dr...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)