codeforces431C
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.
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 kedges), 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).
Examples
3 3 2
3
3 3 3
1
4 3 2
6
4 5 2
7
题意:给出K-Tree定义,每个结点都有恰好K个孩子,这棵树无限增长。每个节点到它K个孩子的K条边的权重刚好是1,2,3...,K(看图应该也看得明白)
现在问有多少条路径,使得从根节点出发到达某个结点,经过的边权重之和恰好为n,并且经过的边至少有一条权重不小于d。
sol:dp应该看得出来,状态也很好构建dp[i][j][0,1]表示到第i层和为j是否有不小于d的边,因为n,k太小,毫无思考的n3dp直接上
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int Mod=,N=;
int n,K,D;
int dp[N][N][];
int main()
{
int i,j,k;
R(n); R(K); R(D);
dp[][][]=;
for(i=;i<=n;i++)
{
for(j=i-;j<n;j++)
{
for(k=;j+k<=n&&k<=K;k++)
{
dp[i][j+k][]+=1ll*dp[i-][j][]%Mod;
dp[i][j+k][]-=(dp[i][j+k][]>=Mod)?Mod:;
if(k>=D)
{
dp[i][j+k][]+=1ll*dp[i-][j][]%Mod;
dp[i][j+k][]-=(dp[i][j+k][]>=Mod)?Mod:;
}
else
{
dp[i][j+k][]+=1ll*dp[i-][j][]%Mod;
dp[i][j+k][]-=(dp[i][j+k][]>=Mod)?Mod:;
}
}
}
}
int ans=;
for(i=;i<=n;i++)
{
ans+=dp[i][n][];
ans-=(ans>=Mod)?Mod:;
}
Wl(ans);
return ;
}
/*
input
3 3 2
output
3 input
3 3 3
output
1 input
4 3 2
output
6 input
4 5 2
output
7
*/
codeforces431C的更多相关文章
- 「专题训练」k-Tree(CodeForces Round #247 Div.2 C)
题意与分析(Codeforces-431C) 题意是这样的:给出K-Tree--一个无限增长的树,它的每个结点都恰有\(K\)个孩子,每个节点到它\(K\)个孩子的\(K\)条边的权重各为\(1,2, ...
随机推荐
- mac nginx 启动 自启动
MacBook-Pro:local shihw$ brew services start nginx ==> Tapping homebrew/services Cloning into '/u ...
- Maven入门指南⑦:Maven的生命周期和插件
一个完整的项目构建过程通常包括清理.编译.测试.打包.集成测试.验证.部署等步骤,Maven从中抽取了一套完善的.易扩展的生命周期.Maven的生命周期是抽象的,其中的具体任务都交由插件来完成.Mav ...
- B. Alyona and a tree
二分+dfs序+前缀和+瞎几把yy #include "a.h" int n; aLL a; struct edge { int to, nx, w; } e[N << ...
- Linux进程管理 (1)进程的诞生
专题:Linux进程管理专题 目录: Linux进程管理 (1)进程的诞生 Linux进程管理 (2)CFS调度器 Linux进程管理 (3)SMP负载均衡 Linux进程管理 (4)HMP调度器 L ...
- OpenResty入门之使用Lua扩展Nginx
记住一点:nginx配置文件很多坑来源自你的空格少了或多了. 1.Centos下载安装 如果你的系统是 Centos 或 RedHat 可以使用以下命令: yum install readline-d ...
- SpringBoot整合Druid数据源
关于SpringBoot数据源请参考我上一篇文章:https://www.cnblogs.com/yueshutong/p/9409295.html 一:Druid介绍 1. Druid是什么? Dr ...
- disruptor 高性能之道
disruptor是一个高性能的线程间异步通信的框架,即在同一个JVM进程中的多线程间消息传递.应用disruptor知名项目有如下的一些:Storm, Camel, Log4j2,还有目前的美团点评 ...
- Ansible 简介
Ansible 是一个开源的基于 OpenSSH 的自动化配置管理工具.可以用它来配置系统.部署软件和编排更高级的 IT 任务,比如持续部署或零停机更新.Ansible 的主要目标是简单和易用,并且它 ...
- Rollup处理并打包JS文件项目实例
关于Rollup rollup是一款用来es6模块打包代码的构建工具(支持css和js打包).当我们使用ES6模块编写应用或者库时,它可以打包成一个单独文件提供浏览器和Node.js来使用. 它的优点 ...
- Vue Router 路由实现原理
一.概念 通过改变 URL,在不重新请求页面的情况下,更新页面视图. 二.实现方式 更新视图但不重新请求页面,是前端路由原理的核心之一,目前在浏览器环境中这一功能的实现主要有2种方式: 1.Hash ...