acdream1412:2-3 trees 组合数dp
题意:
给出一个标准2-3树的叶子节点(最底层节点)个数 L,求2-3数的形成方案数并取余
分析:
如果有L个叶子
枚举 每个 可以使x*2+y*3=L 的 x y
那么在最底层就有 c(x+y,x)种 2,3的放法
上一层就是有 x+y个叶子的子问题 这样就可以递推算了
枚举时首先注意到 x=L y=-L 是一组特解,因此可以由它求出所有的通解
提前处理出组合数。
然后对于每组合法的解通过dp转移即可
代码:
#include <iostream>
#include <stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<ctype.h>
using namespace std;
#define MAXN 10000
const int mm =;
int c[][];
long long dp[];
long long L,r;
void ini()
{
memset(c,,sizeof(c));
c[][]=;
c[][]=c[][]=;
for(int i=;i<=L/;i++)
{
c[i][]=;
for(int j=;j<i;j++)
{
c[i][j]=(c[i-][j-]%r+c[i-][j]%r)%r;
}
c[i][i]=;
}
}
void solve()
{
dp[]=;
dp[]=;
dp[]=;
for(int i=;i<=L;i++)
{
dp[i]=;
int x=-i,y=i;
x=(x%+)%;
y=(i-x*)/;
while(x*<=i)
{
int k=x>y?y:x;
dp[i]=(dp[i]+((long long)c[x+y][k]*dp[x+y])%r)%r;
x+=;
y-=;
}
}
printf("%lld\n",dp[L]);
}
int main()
{
//freopen("shu.txt","r",stdin);
while(scanf("%lld%lld",&L,&r)!=EOF)
{
ini();
solve();
}
return ;
}
acdream1412:2-3 trees 组合数dp的更多相关文章
- 【bzoj4517】[Sdoi2016]排列计数 组合数+dp
题目描述 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是稳定的 满足条 ...
- HDU 1693 Eat the Trees(插头DP)
题目链接 USACO 第6章,第一题是一个插头DP,无奈啊.从头看起,看了好久的陈丹琦的论文,表示木看懂... 大体知道思路之后,还是无法实现代码.. 此题是插头DP最最简单的一个,在一个n*m的棋盘 ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)
Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...
- hdu1693:eat trees(插头dp)
题目大意: 题目背景竟然是dota!屠夫打到大后期就没用了,,只能去吃树! 给一个n*m的地图,有些格子是不可到达的,要把所有可到达的格子的树都吃完,并且要走回路,求方案数 题解: 这题大概是最简单的 ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees (DP)
C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- HDU1693 Eat the Trees 插头dp
原文链接http://www.cnblogs.com/zhouzhendong/p/8433484.html 题目传送门 - HDU1693 题意概括 多回路经过所有格子的方案数. 做法 最基础的插头 ...
- hdu 1693 Eat the Trees——插头DP
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1693 第一道插头 DP ! 直接用二进制数表示状态即可. #include<cstdio> # ...
- 【CF917D】Stranger Trees 树形DP+Prufer序列
[CF917D]Stranger Trees 题意:给你一棵n个点的树,对于k=1...n,问你有多少有标号的n个点的树,与给出的树有恰好k条边相同? $n\le 100$ 题解:我们先考虑容斥,求出 ...
- 96. Unique Binary Search Trees (Tree; DP)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- facebook分享遇到的错误解决方法
*** Terminating app due to uncaught exception 'InvalidOperationException', reason: ''App ID not foun ...
- php form表单post提交获取不到数据,而使用get提交能获取到数据 的解决办法
开发环境:xampp,mac,phpstorm 其实出现这个问题的原因就是在于phpstorm,它默认使用的是自带的内部服务器,这个服务器使用63342端口,而且服务器内部有问题,导致POST方法异常 ...
- Hadoop 类Grep源代码注释
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agree ...
- IOS-连接
http://blog.sina.com.cn/s/articlelist_2659739627_0_2.html
- 自定义控件 带描边的TextView
使用 public class MainActivity extends Activity { @Override protected void onCreate(Bundle sav ...
- C#。2. 2 语句
二.语句: 顺序,分支,循环. (一)顺序:略 分支:判断--表达式. if(){} 四大类: 1.if if (age > 18) { Console.WriteLin ...
- hdu 2032
水题 AC代码: #include <stdio.h> int main() { int a[31][31]; int i,j,n; a[1][1]=a[2][1]=a[2][2]=1; ...
- T-SQL 一次插入多行数据
使用 INSERT SELECT 向表中插入数据 --将t1中查询到的数据插入添加到t2中(t2表必须存在,且顺序.数据类型必须与t1一致) INSERT INTO t2(USERNAME,PASSW ...
- 解决PL/SQL Dev连接Oracle弹出空白提示框
第一次安装Oracle,装在虚拟机中,用PL/SQL Dev连接远程数据库的时候老是弹出空白提示框,网上找了很久,解决方法也很多,可是就是没法解决我这种情况的. 没办法,只能自己研究,经过大概一天时间 ...
- for循环删除list元素陷阱
首先我们先看一段代码,如下: List<String> list=new ArrayList<String>(); list.add("123"); lis ...