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 ...
随机推荐
- js实现可拖动的布局
思路:采用flex布局,js即时修改固定列的宽度 注意:父元素需设置position:relative:因offsetLeft和offsetTop是相对于具有定位的(position:absolute ...
- day16-作业 后台管理
二话不说开撸作业 作业要求: 后台管理平台 ,编辑表格:1. 非编辑模式:可对每行进行选择: 反选: 取消选择2. 编辑模式:进入编辑模式时如果行被选中,则被选中的行万变为可编辑状态,未选中的不改变退 ...
- 【294】◀▶ Python 字符串说明
目录: 一.Python访问字符串中的值 二. Python 转义字符 三.Python 字符串运算符 参考:Python 字符串 一.Python访问字符串中的值 Python不支持单字符类型, ...
- OSCache安装
OSCache是一个基于web应用的组件,他的安装工作主要是对web应用进行配置,大概的步骤如下: 1. 下载.解压缩OSCachehttps://java.net/downloads/oscache ...
- A Look at the Razor View Engine in ASP.NET MVC
The biggest architectural difference that exists between ASP.NET MVC and ASP.NET Web Forms is the ne ...
- HDR
[HDR] 什么是 HDR? 高动态范围拍摄(HDR)现在已经得到广泛使用,被用来补偿大多数数码成像传感器有限的动态范围.照片的动态范围是指最暗的色彩与最亮的色彩之间的亮度范围——也可以一并表示色调范 ...
- 使用Fuel安装openstack
一.前言 Fuel是OpenStack的开源部署和管理工具.作为OpenStack社区的开发贡献者,它为OpenStack.OpenStack相关社区项目以及OpenStack插件的部署和管理提供了直 ...
- codeforce469DIV2——C. Zebras
题意 0, 010, 01010 这一类的01交替且开头和结尾都为0的序列被称为zebra序列.给出一段01序列,尝试能否把他分为k个子序列使得每个子序列都是zebra序列. 分析 这个题应该算是水题 ...
- 【bzoj1017】[JSOI2008]魔兽地图DotR
1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec Memory Limit: 162 MBSubmit: 1658 Solved: 755[Submit][S ...
- 面试题:java实例变量,局部变量,类变量 背1
一.实例变量 也叫对象变量.类成员变量:从属于类由类生成对象时,才分配存储空间,各对象间的实例变量互不干扰,能通过对象的引用来访问实例变量.但在Java多线程中,实例变量是多个线程共享资源,要注意同步 ...