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 ...
随机推荐
- Unity商店下载的文件保存路径?
Win7系统: C:\Users\系统用户名\AppData\Roaming\Unity\Asset Store MAC:"~/Library/Unity/Asset\ Store" ...
- Struts 2(八):文件上传
第一节 基于Struts 2完成文件上传 Struts 2框架中没有提供文件上传,而是通过Common-FileUpload框架或COS框架来实现的,Struts 2在原有上传框架的基础上进行了进一步 ...
- Spring学习(六)-----Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
- cocos2dx2.0 帧动画的创建和播放过程 深入分析
一.帧动画的创建过程帧动画的实现有四个不可或缺的类,如下:1.CCSpriteFrame:精灵帧信息.存储帧动画的每一帧的纹理基本信息. class CC_DLL CCSpriteFrame : pu ...
- DeepLearning - Overview of Sequence model
I have had a hard time trying to understand recurrent model. Compared to Ng's deep learning course, ...
- oozie的shell-action中加入hive脚本命令启动执行shell同时操作hive,抛异常Container killed on request. Exit code is 143 Container exited with a non-zero exit code 143
使用oozie来调度操作,用shell的action执行命令,其中shell里包含着hive -e 操作执行时,oozie窗口报 WARN ShellActionExecutor: - SERVER[ ...
- Scrum立会报告+燃尽图(Beta阶段第二周第六次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2414 项目地址:https://coding.net/u/wuyy694 ...
- HDU 4489 The King’s Ups and Downs dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4489 The King's Ups and Downs Time Limit: 2000/1000 ...
- 1029 C语言文法翻译(2)
program à external_declaration | program external_declaration 翻译:<源程序>→ <外部声明> | <源程序 ...
- 201621123037 《Java程序设计》第7周学习总结
作业06-接口.内部类 1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 答: 思维导图: 其他-笔记: 2. 书面作业 1. ArrayList代码分析 1.1 解释Arr ...