51nod1832(二叉树/高精度模板+dfs)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1832
题意: 中文题诶~
思路: 若二叉树中有 k 个节点只有一个子树, 则答案为 1 << k.
详情参见:http://blog.csdn.net/gyhguoge01234/article/details/77836484
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#define ll long long
using namespace std; const int MAX = 1e2;
const int M = 1e9;//1e9为1节
const int MAXN = ; struct BigInt{
const static int mod = ;
const static int DLEN = ;
int a[], len;
BigInt(){
memset(a, , sizeof(a));
len = ;
}
BigInt(int v){
memset(a, , sizeof(a));
len = ;
do{
a[len++] = v % mod;
v /= mod;
}while(v);
}
BigInt(const char s[]){
memset(a, , sizeof(a));
int L = strlen(s);
len = L / DLEN;
if(L % DLEN) len++;
int index = ;
for(int i = L - ; i >= ; i -= DLEN){
int t = ;
int k = i - DLEN + ;
if(k < ) k = ;
for(int j = k; j <= i; j++)
t = t * + s[j] - '';
a[index++] = t;
}
}
BigInt operator +(const BigInt &b)const{
BigInt res;
res.len = max(len, b.len);
for(int i = ; i <= res.len; i++) res.a[i] = ;
for(int i = ; i < res.len; i++){
res.a[i] += ((i < len) ? a[i] : ) + ((i < b.len) ? b.a[i] : );
res.a[i + ] += res.a[i] / mod;
res.a[i] %= mod;
}
if(res.a[res.len] > ) res.len++;
return res;
}
BigInt operator *(const BigInt &b)const{
BigInt res;
for(int i = ; i < len; i++){
int up = ;
for(int j = ; j < b.len; j++){
int temp = a[i] * b.a[j] + res.a[ i + j] + up;
res.a[i + j] = temp%mod;
up = temp / mod;
}
if(up != )
res.a[i + b.len] = up;
}
res.len = len + b.len;
while(res.a[res.len - ] == && res.len > ) res.len--;
return res;
}
void output(){
printf("%d", a[len - ]);
for(int i = len - ; i >= ; i--)
printf("%04d", a[i]);
printf("\n");
}
}; // 先序遍历 X L … R …
// 后序遍历 … L … R X const int N = 1e5 + ;
int a[N], b[N];
BigInt sol(); void dfs(int al, int ar, int bl, int br){
if(ar - al <= ) return;
al++;
br--;
int indx = bl, cnt = ;;
while(a[al] != b[indx]) indx++;
int newar = al + (indx - bl + );
int newbr = indx + ;
cnt++;
dfs(al, newar, bl, newbr);
if(ar - al != indx - bl + ){
cnt++;
dfs(newar, ar, newbr, br);
}
if(cnt == ) sol = sol * ;
} int main(void){
int n;
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%d", &a[i]);
}
for(int i = ; i < n; i++){
scanf("%d", &b[i]);
}
sol = ;
dfs(, n, , n);
sol.output();
return ;
}
51nod1832(二叉树/高精度模板+dfs)的更多相关文章
- [Template]高精度模板
重新写一下高精度模板(不要问我为什么) 自认为代码风格比较漂亮(雾 如果有更好的写法欢迎赐教 封装结构体big B是压位用的进制,W是每位长度 size表示长度,d[]就是保存的数字,倒着保存,从1开 ...
- C++高精度模板
原文地址:http://blog.csdn.net/wall_f/article/details/8373395 原文只附代码,没有解析,本文增加了一些对代码的解释. 请注意:本模板不涉及实数运算与负 ...
- [note]高精度模板
高精度模板 先定义一个struct struct gj{ int l,s[N]; bool fh; void Print(){ if(fh)putchar('-'); for(int i=l;i> ...
- 高精度模板 支持各种运算 c++
绪言 自从有了高精度模板,妈妈再也不用怕我不会打高精度了! 代码 代码长度与日俱增啊~~~ #include<iostream> #include<cstring> #incl ...
- 51nod 1832 先序遍历与后序遍历【二叉树+高精度】
题目链接:51nod 1832 先序遍历与后序遍历 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的 ...
- 高精度模板 Luogu P1932 A+B & A-B & A*B & A/B Problem
P1932 A+B & A-B & A*B & A/B Problem 题目背景 这个题目很新颖吧!!! 题目描述 求A.B的和差积商余! 输入输出格式 输入格式: 两个数两行 ...
- [模板] dfs序, 树链剖分, 换根
树链剖分 树链剖分是一种对树的分治, 可以把树上的任意一条链分解为 \(O(\log n)\) 条在dfs序上相邻的子链, 便于数据结构(如线段树)来维护. 另外, 子树在dfs序上也是一个连续的区间 ...
- Java 大数、高精度模板
介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...
- JAVA高精度模板
刚开始还坚持用C++写高精来着,后来发现JAVA写高精方便太多了,所以也来学习一下JAVA高精度的模板. 参考:https://www.cnblogs.com/imzscilovecode/p/883 ...
随机推荐
- Py修行路 python基础 (十五)面向对象编程 继承 组合 接口和抽象类
一.前提回忆: 1.类是用来描述某一类的事物,类的对象就是这一类事物中的一个个体.是事物就要有属性,属性分为 1:数据属性:就是变量 2:函数属性:就是函数,在面向对象里通常称为方法 注意:类和对象均 ...
- Angular-cli新建项目目录结构详解
Angular-cli新建项目目录结构详解 在上一篇博客中我们已经通过Angular CLI命令行工具创建出来一个全新的Angular项目,要想写项目,首先我们要先搞清楚项目的目录结构是怎样的,每个文 ...
- Jsp页面中的中文乱码问题解决
Jsp页面中的中文乱码问题解决 在编写Jsp页面的时候,发现写入其中的中文在浏览器浏览的时候会出现乱码的情况. 出现乱码的原因分析: 因为页面中对自己的编码格式的声明和页面的实际编码格式不相同,而浏览 ...
- Jquery异步
一.Jquery向aspx页面请求数据$("#Button1").bind("click", function () { $.ajax({ type: &quo ...
- Linux3一些文件操作命令more,less,pr,head,tail,wc
查看文件内容命令: more和less 用cat命令可以查看文件.有时候文件太大,可以用管道符号|配合more或者less一同使用. cat <文本文件名称>|more cat < ...
- Linux Java Meven环境变量设置
linux中的环境变量设置,可以在两个地方设置.他们分别是: /etc/profile 在这个文件下设置表示全局的,所有用户有效. 用户工作目录,用 ls -a查看,可以看到有一个.bash_pro ...
- 下载tomcat安装版
1.我们首先在浏览器的地址栏中输入下载地址,如下: Tomcat下载网站链接:http://tomcat.apache.org/ 大家注意:以上下载链接中以zip或gz结尾的都是免安装版的,我们要下载 ...
- matplotlib的颜色和控制条
为了方便记忆,收藏备用 一 linestyle '-' solid line style '--' dashed line style '-.' dash-dot line style ':' dot ...
- Luogu 4251 [SCOI2015]小凸玩矩阵
BZOJ 4443 二分答案 + 二分图匹配 外层二分一个最小值,然后检验是否能选出$n - k + 1$个不小于当前二分出的$mid$的数.对于每一个$a_{i, j} \geq mid$,从$i$ ...
- Python基础入门-while循环示例
闲来无事! 想写一些基础的东西! 比如今天的while循环,,,,,, 很多python初学者,最开始学习python的时候,会被while循环给干蒙蔽! 那么今天,小编为大家讲解一些基础的实例,来帮 ...