题目链接: 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)的更多相关文章

  1. [Template]高精度模板

    重新写一下高精度模板(不要问我为什么) 自认为代码风格比较漂亮(雾 如果有更好的写法欢迎赐教 封装结构体big B是压位用的进制,W是每位长度 size表示长度,d[]就是保存的数字,倒着保存,从1开 ...

  2. C++高精度模板

    原文地址:http://blog.csdn.net/wall_f/article/details/8373395 原文只附代码,没有解析,本文增加了一些对代码的解释. 请注意:本模板不涉及实数运算与负 ...

  3. [note]高精度模板

    高精度模板 先定义一个struct struct gj{ int l,s[N]; bool fh; void Print(){ if(fh)putchar('-'); for(int i=l;i> ...

  4. 高精度模板 支持各种运算 c++

    绪言 自从有了高精度模板,妈妈再也不用怕我不会打高精度了! 代码 代码长度与日俱增啊~~~ #include<iostream> #include<cstring> #incl ...

  5. 51nod 1832 先序遍历与后序遍历【二叉树+高精度】

    题目链接:51nod 1832 先序遍历与后序遍历 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 对于给定的一个二叉树的先序遍历和后序遍历,输出有多少种满足条件的 ...

  6. 高精度模板 Luogu P1932 A+B & A-B & A*B & A/B Problem

    P1932 A+B & A-B & A*B & A/B Problem 题目背景 这个题目很新颖吧!!! 题目描述 求A.B的和差积商余! 输入输出格式 输入格式: 两个数两行 ...

  7. [模板] dfs序, 树链剖分, 换根

    树链剖分 树链剖分是一种对树的分治, 可以把树上的任意一条链分解为 \(O(\log n)\) 条在dfs序上相邻的子链, 便于数据结构(如线段树)来维护. 另外, 子树在dfs序上也是一个连续的区间 ...

  8. Java 大数、高精度模板

    介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...

  9. JAVA高精度模板

    刚开始还坚持用C++写高精来着,后来发现JAVA写高精方便太多了,所以也来学习一下JAVA高精度的模板. 参考:https://www.cnblogs.com/imzscilovecode/p/883 ...

随机推荐

  1. IntelliJ IDEA 安装 Julia 插件

    打开 IntelliJ IDEA 点击 Configure 选择 Plugins 然后点击 Browse repositories 搜索 Julia,然后点击 Install 进行安装 安装完重启一下 ...

  2. php各版本编译好的扩展模块下载地址

    php各版本[,x86/64  v9/v11/v14  nts/ts]编译好的扩展模块下载地址: https://windows.php.net/downloads/pecl/releases/ ht ...

  3. git rm简介

    本文翻译整理自:http://web.mit.edu/~mkgray/project/silk/root/afs/sipb/project/git/git-doc/git-rm.html 在git中我 ...

  4. beijing

    #include<stdio.h> #include<string.h> #include<stdlib.h> #include<graphics.h> ...

  5. Docker学习笔记_创建和使用Centos容器

    实验:创建和使用Centos容器 步骤: 1.搜索                                                     sudo docker search cen ...

  6. 10个强大的Javascript表单验证插件推荐

    创建一个JavaScript表单验证插件,可以说是一个繁琐的过程,涉及到初期设计.开发与测试等等环节.实际上一个优秀的程序员不仅是技术高手,也应该是善假于外物的.本文介绍了10个不错的JavaScri ...

  7. Linux nethack

    一.简介 游戏目标:在地下城的最底层取得炎多的护符项链(Amulet of Yendor),并返回最上层,在圣祭坛上供奉给神灵.完成整个游戏的奖赏是,玩家会成为不朽的半神.   二.安装 1)下载源码 ...

  8. Python pip命令

    一.简介 pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品.distribute是setuptools的取代(Setuptools包后期不再维护了),p ...

  9. list 的扩展

    数据挖掘中会遇到添加多个新的特征s,对一个feature = list()来说, 除了可以用 feature.append('xx') # 在尾部添加一个特征 feature.extend(['xx' ...

  10. jq遍历table表demo

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...