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 ...
随机推荐
- PHP字符串的处理(四)-HTML标签的字符串格式化
和html标签相关的字符串格式化 nl2br() //在字符串中每个新行"\n"之前插入html换行符"<br />" <?php echo ...
- cs231n神经网络 常用激活函数
CS231n课程笔记翻译:神经网络笔记1(上) 一.常用激活函数 每个激活函数(或非线性函数)的输入都是一个数字,然后对其进行某种固定的数学操作.下面是在实践中可能遇到的几种激活函数: ——————— ...
- 使用ReentrantReadWriteLock类
读读共享 类ReentrantReadWriteLock的使用:写写互斥 读写互斥
- sql数据库各个版本清除日志
SQL2005清空删除日志: 复制代码 代码如下: Backup Log DNName with no_log --'这里的DNName是你要收缩的数据库名,自己注意修改下面的数据 ...
- 【bzoj1455】罗马游戏
1455: 罗马游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1061 Solved: 439[Submit][Status][Discuss] ...
- Ros学习——移动机器人Ros导航详解及源码解析
1 执行过程 1.运行仿真机器人fake_turtlebot.launch:加载机器人模型——启动机器人仿真器——发布机器人状态 2.运行amcl节点fake_amcl.launch:加载地图节点ma ...
- 383. Ransom Note 在字典数组中查找笔记数组
[抄题]: Given an arbitrary ransom note string and another string containing letters from all the magaz ...
- python之连接oracle数据库
环境: windows,python2.7 1.下载cx_Oracle 在windows下不要使用easy_install或者pip,因为这样安装不会同步环境,并报错: distutils.error ...
- 1.介绍templates
我们现在要计算int和double类型数据的平方,我们就需要2个函数: #include <iostream> using namespace std; int square(int x) ...
- Oracle数据库管理
一.Oracle 的(资源限制)概要文件 为了控制系统资源的使用, 可以利用资源限制概要文件. 资源限制概要文件是 Oracle 安全策略的重要组成部分, 利用资源限制概要文件可以对数据库用户进行基本 ...