BZOJ1089 [SCOI2003]严格n元树 【dp + 高精】
Description
如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树。如果该树中最底层的节点深度为d
(根的深度为0),那么我们称它为一棵深度为d的严格n元树。例如,深度为2的严格2元树有三个,如下图:

给出n, d,编程数出深度为d的n元树数目。
Input
仅包含两个整数n, d( 0 < n < = 32, 0 < = d < = 16)
Output
仅包含一个数,即深度为d的n元树的数目。
Sample Input
2 2
【样例输入2】
2 3
【样例输入3】
3 5
Sample Output
3
【样例输出2】
21
【样例输出2】
58871587162270592645034001
题解
设f[d]为深度不大于d的n元树的个数,显然答案就是f[d] - f[d - 1]
对于考虑f[d]的根节点,它的每一棵子树方案数都是f[d - 1],用乘法原理:
f[d] = f[d - 1] ^ n + 1【+1考虑只有一个根节点】
边界:f[0] = 1
再者就是高精【好久没写高精乘高精了】,写的时候还需要先调一下
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define fo(i,x,y) for (int i = (x); i <= (y); i++)
#define Redge(u) for (int k = head[u]; k != -1; k = edge[k].next)
using namespace std;
const int maxn = 20,maxm = 205,INF = 1000000000; int N,D; struct NUM{
int n[maxm],len;
NUM() {memset(n,0,sizeof(n)); len = 0;}
}f[maxn]; inline istream& operator >>(istream& in,NUM& a){
string s;
in>>s;
a.len = s.length();
for (int i = 0; i < a.len; i++) a.n[i] = s[a.len - i - 1] - '0';
return in;
} inline ostream& operator << (ostream& out,const NUM& a){
if (!a.len) out<<0;
else {
for (int i = a.len - 1; i >= 0; i--) out<<a.n[i];
}
return out;
} inline NUM operator *(const NUM& a,const NUM& b){
NUM c;
c.len = a.len + b.len + 2;
int carry = 0,temp;
for (int i = 0; i < a.len; i++){
for (int j = 0; j < b.len; j++){
temp = c.n[j + i] + a.n[i] * b.n[j] + carry;
c.n[j + i] = temp % 10;
carry = temp / 10;
}
int len = i + b.len;
while (carry) {
temp = c.n[len] + carry;
c.n[len] = temp % 10;
carry = temp / 10;
len++;
}
}
while (!c.n[c.len - 1]) c.len--;
return c;
} inline NUM operator + (const NUM& a,const int& b){
NUM c = a;
int temp = c.n[0] + b,carry = temp / 10;
c.n[0] = temp % 10;
for (int i = 1; carry && i < c.len; i++){
temp = c.n[i] + carry;
c.n[i] = temp % 10;
carry = temp / 10;
}
if (carry) c.n[c.len++] = carry;
return c;
} inline NUM operator - (const NUM& a,const NUM& b){
NUM c;
c.len = a.len;
int carry = 0;
for (int i = 0; i < a.len; i++){
c.n[i] = a.n[i] - b.n[i] + carry;
if (c.n[i] < 0) c.n[i] += 10,carry = -1;
else carry = 0;
}
while (!c.n[c.len - 1]) c.len--;
return c;
} inline NUM qpow(NUM a,int b){
NUM ans; ans.n[0] = ans.len = 1;
for (; b; b >>= 1,a = a * a)
if (b & 1) ans = ans * a;
return ans;
} int main()
{
/*cin>>f[0]>>f[1];
cout<<f[0] * f[1]<<endl;*/ cin>>N>>D;
if (!D) {cout<<1<<endl;return 0;}
f[0].n[0] = f[0].len = 1;
for (int i = 1; i <= D; i++){
f[i] = qpow(f[i - 1],N) + 1;
}
cout<<f[D] - f[D - 1]<<endl;
return 0;
}
BZOJ1089 [SCOI2003]严格n元树 【dp + 高精】的更多相关文章
- bzoj1089 [SCOI2003]严格n元树(dp+高精)
1089: [SCOI2003]严格n元树 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1899 Solved: 954[Submit][Statu ...
- BZOJ1089:[SCOI2003]严格n元树(DP,高精度)
Description 如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树.如果该树中最底层的节点深度为d (根的深度为0),那么我们称它为一棵深度为d的严格n元树.例如,深度为2的严 ...
- BZOJ1089: [SCOI2003]严格n元树
1089: [SCOI2003]严格n元树 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 762 Solved: 387[Submit][Status ...
- P4295 [SCOI2003]严格N元树 DP
思路:DP 提交:\(5\)次 错因:2次高精写错(我太菜了),2次写错特判 题解: 设\(f[i]\)表示深度\(\leq i\)的严格\(n\)元树的数目,有 \[f[i]=pow(f[i-1], ...
- [BZOJ1089][SCOI2003]严格n元树(递推+高精度)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1089 分析: 第一感觉可以用一个通式求出来,但是考虑一下很麻烦,不好搞的.很容易发现最 ...
- bzoj 1089 [SCOI2003]严格n元树(DP+高精度)
1089: [SCOI2003]严格n元树 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1250 Solved: 621[Submit][Statu ...
- 【BZOJ1089】[SCOI2003]严格n元树(高精度,动态规划)
[BZOJ1089][SCOI2003]严格n元树(高精度,动态规划) 题面 BZOJ 洛谷 题解 设\(f[i]\)表示深度为\(i\)的\(n\)元树个数.然后我们每次加入一个根节点,然后枚举它的 ...
- SCOI2003 严格N元树
SCOI2003 严格N元树 Description 如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树.如果该树中最底层的节点深度为d (根的深度为0),那么我们称它为一棵深度为d的 ...
- BZOJ 1089: [SCOI2003]严格n元树
1089: [SCOI2003]严格n元树 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1591 Solved: 795[Submit][Statu ...
随机推荐
- javaweb(三十八)——mysql事务和锁InnoDB(扩展)
MySQL/InnoDB的加锁分析,一直是一个比较困难的话题.我在工作过程中,经常会有同事咨询这方面的问题.同时,微博上也经常会收到MySQL锁相关的私信,让我帮助解决一些死锁的问题.本文,准备就My ...
- [.NET] 使用HttpClient操作HFS (HTTP File Server)
前言 本篇文章介绍如何使用HttpClient操作HFS (HTTP File Server),为自己留个纪录也希望能帮助到有需要的开发人员.关于HTTP File Server的介绍.安装.设定,可 ...
- 强化学习读书笔记 - 11 - off-policy的近似方法
强化学习读书笔记 - 11 - off-policy的近似方法 学习笔记: Reinforcement Learning: An Introduction, Richard S. Sutton and ...
- 袋鼠云研发手记 | 数栈·开源:Github上400+Star的硬核分布式同步工具FlinkX
作为一家创新驱动的科技公司,袋鼠云每年研发投入达数千万,公司80%员工都是技术人员,袋鼠云产品家族包括企业级一站式数据中台PaaS数栈.交互式数据可视化大屏开发平台Easy[V]等产品也在迅速迭代.在 ...
- JAVA学习笔记--正则表达式
正则表达式是一种强大而灵活的文本处理工具.使用正则表达式,可以让我们以编程的方式构造复杂的文本,并对输入的字符串进行搜索. 一.基础正则表达式语法(表格来自J2SE6_API) 字符 x 字符 x \ ...
- 1035 Password (20 分)(字符串)
注意下单复数 #include<bits/stdc++.h> using namespace std; pair<string,string>pa; int main() { ...
- 【python】详解time模块功能asctime、localtime、mktime、sleep、strptime、strftime、time等函数以及时间的加减运算
在Python中,与时间处理相关的模块有:time.datetime以及calendar.学会计算时间,对程序的调优非常重要,可以在程序中狂打时间戳,来具体判断程序中哪一块耗时最多,从而找到程序调优的 ...
- phpquery 学习笔记
phpQuery是一个基于PHP的服务端开源项目,它可以让PHP开发人员轻松处理DOM文档内容,比如获取某新闻网站的头条信息.更有意思的是,它采用了jQuery的思想,你可以像使用jQuery一样处理 ...
- 总结在Visual Studio Code运行node.js项目遇到的问题
一.cannot find module “lodash” 项目运行时出现以下错误: Error: Cannot find module 'lodash' at Function.Module._re ...
- Saver 保存与读取
tensorflow 框架下的Saver 功能,用以保存和读取运算数据 Saver 保存数据 代码 import tensorflow as tf # Save to file #remember t ...