题目链接: 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. BugkuCTF WEB

    web2 打开链接,一大堆表情 查看源代码 得到 flag 文件上传测试 打开链接 选择 1 个 jpg 文件进行上传,用 burp 抓包改包 将 php 改为 jpg,发包 得到 flag 计算器 ...

  2. Java对网络图片/本地图片转换成Base64编码和解码

    一.将本地图片转换成Base64编码字符串 /** * 将本地图片转换成Base64编码字符串 * * @param imgFile 图片目录路径 * @return */ public static ...

  3. Asp.Net framework 类库 自带的缓存 HttpRuntime.Cache HttpContext.Cache

    两个Cache 在.NET运用中经常用到缓存(Cache)对象.有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是应用程序 ...

  4. 嵌入式&nbsp;Linux&nbsp;与linux启…

    一.在ARM linux 下,一般而言,产品在启动的过程中应该加载模块,最简单的方法是修改启动过程的rc脚本(/etc/init.d/rcS),增加ismod /../xxx.ko这个命令.例如:加载 ...

  5. dataTable写入数据库(大数据写入)

    例1: connectionStr,链接字符串dataTableName, 数据库中对应表名sourceDataTable DataTable 要写入数据库的DataTable字段要和表一致 publ ...

  6. 在Build Path中包含其他工程

    ------------siwuxie095                                 在 TestBuildPath 的 Build Path 中包含 SupportProje ...

  7. ubuntu16.04 安装openpose

    安装 Anaconda3 Tensorflow-cpu python3tensorflow 1.4.1+opencv3, protobuf, python3-tk ================== ...

  8. Hyperledger Fabric Ordering Service过程

    排序服务在超级账本 Fabric 网络中起到十分核心的作用.所有交易在发送给 Committer 进行验证接受之前,需要先经过排序服务进行全局排序. 在目前架构中,排序服务的功能被抽取出来,作为单独的 ...

  9. Win10 linux子系统Ubuntu下显示图形界面

    转载 https://jingyan.baidu.com/article/ed2a5d1f98577809f6be17a3.html 打开终端界面,在这个窗口测试一下ls命令,无误. # 更新 sud ...

  10. Django rest_framework----认证,权限,频率组件

    认证 from rest_framework.authentication import BaseAuthentication from rest_framework.exceptions impor ...