51nod1832(二叉树/高精度模板+dfs)

题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1832

 

题意: 中文题诶~

 

思路: 若二叉树中有 k 个节点只有一个子树, 则答案为 1 << k.

详情参见:http://blog.csdn.net/gyhguoge01234/article/details/77836484

 

代码:

  1 #include <iostream>
  2 #include <stdio.h>
  3 #include <string.h>
  4 #define ll long long
  5 using namespace std;
  6 
  7 const int MAX = 1e2;
  8 const int M = 1e9;//1e9为1节
  9 const int MAXN = 35;
 10 
 11 struct BigInt{
 12     const static int mod = 10000;
 13     const static int DLEN = 4;
 14     int a[600], len;
 15     BigInt(){
 16         memset(a, 0, sizeof(a));
 17         len = 1;
 18     }
 19     BigInt(int v){
 20         memset(a, 0, sizeof(a));
 21         len = 0;
 22         do{
 23             a[len++] = v % mod;
 24             v /= mod;
 25         }while(v);
 26     }
 27     BigInt(const char s[]){
 28         memset(a, 0, sizeof(a));
 29         int L = strlen(s);
 30         len = L / DLEN;
 31         if(L % DLEN) len++;
 32         int index = 0;
 33         for(int i = L - 1; i >= 0; i -= DLEN){
 34             int t = 0;
 35             int k = i - DLEN + 1;
 36             if(k < 0) k = 0;
 37             for(int j = k; j <= i; j++)
 38                 t = t * 10 + s[j] - '0';
 39             a[index++] = t;
 40         }
 41     }
 42     BigInt operator +(const BigInt &b)const{
 43         BigInt res;
 44         res.len = max(len, b.len);
 45         for(int i = 0; i <= res.len; i++) res.a[i] = 0;
 46         for(int i = 0; i < res.len; i++){
 47             res.a[i] += ((i < len) ? a[i] : 0) + ((i < b.len) ? b.a[i] : 0);
 48             res.a[i + 1] += res.a[i] / mod;
 49             res.a[i] %= mod;
 50         }
 51         if(res.a[res.len] > 0) res.len++;
 52         return res;
 53     }
 54     BigInt operator *(const BigInt &b)const{
 55         BigInt res;
 56         for(int i = 0; i < len; i++){
 57             int up = 0;
 58             for(int j = 0; j < b.len; j++){
 59                 int temp = a[i] * b.a[j] + res.a[ i + j] + up;
 60                 res.a[i + j] = temp%mod;
 61                 up = temp / mod;
 62             }
 63             if(up != 0)
 64             res.a[i + b.len] = up;
 65         }
 66         res.len = len + b.len;
 67         while(res.a[res.len - 1] == 0 && res.len > 1) res.len--;
 68         return res;
 69     }
 70     void output(){
 71         printf("%d", a[len - 1]);
 72         for(int i = len - 2; i >= 0; i--)
 73             printf("%04d", a[i]);
 74         printf("\n");
 75     }
 76 };
 77 
 78 // 先序遍历 X L … R …
 79 // 后序遍历 … L … R X
 80 
 81 const int N = 1e5 + 10;
 82 int a[N], b[N];
 83 BigInt sol(1);
 84 
 85 void dfs(int al, int ar, int bl, int br){
 86     if(ar - al <= 1) return;
 87     al++;
 88     br--;
 89     int indx = bl, cnt = 0;;
 90     while(a[al] != b[indx]) indx++;
 91     int newar = al + (indx - bl + 1);
 92     int newbr = indx + 1;
 93     cnt++;
 94     dfs(al, newar, bl, newbr);
 95     if(ar - al != indx - bl + 1){
 96         cnt++;
 97         dfs(newar, ar, newbr, br);
 98     }
 99     if(cnt == 1) sol = sol * 2;
100 }
101 
102 int main(void){
103     int n;
104     scanf("%d", &n);
105     for(int i = 0; i < n; i++){
106         scanf("%d", &a[i]);
107     }
108     for(int i = 0; i < n; i++){
109         scanf("%d", &b[i]);
110     }
111     sol = 1;
112     dfs(0, n, 0, n);
113     sol.output();
114     return 0;
115 }
View Code

 

转载于:https://www.cnblogs.com/geloutingyu/p/7693105.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391076.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

重学TCP协议(11)TFO(Tcp Fast Open)

1. TFO 为了改善web应用相应时延&#xff0c;google发布了通过修改TCP协议利用三次握手时进行数据交换的TFO(TCP fast open&#xff0c;RFC 7413)。 TFO允许在TCP握手期间发送和接收初始SYN分组中的数据。如果客户端和服务器都支持TFO功能&#xff0c;则可以减少建立到同一服…

[网络安全] 远程登录

远程登录方式: 1.图像化远程登录 做法: 运行"窗口"输入 "mstsc " 输入ip地址 注意: 被远程计算机&#xff0c;必须打开远程登录服务: 信息面板–系统–允许远程访问。被远程计算机&#xff0c;必须存在拥有远程桌面权限的用户。 2.命令行远程登录 teln…

外星人图像和外星人太空船_卫星图像:来自太空的见解

外星人图像和外星人太空船By Christophe Restif & Avi Hoffman, Senior Software Engineers, Crisis Response危机应对高级软件工程师Christophe Restif和Avi Hoffman Editor’s note: In 2019, we piloted a new feature in Search SOS Alerts for major California wild…

chrome恐龙游戏_如何玩没有互联网的Google Chrome恐龙游戏-在线和离线

chrome恐龙游戏Several years ago, Google added a fun little Easter egg to Chrome: if your internet went down and you tried to visit a web page, youd see the message "Unable to connect to the Internet" or "No internet" with a little pixi…

Hotpatch潜在的安全风险

屎蛋 2016/06/22 10:11author:[email protected]0x00 “Hotpatch”简介IOS App的开发者们经常会出现这类问题&#xff1a;当一个新版本上线后发现存在一个严重的bug&#xff0c;有可能因为一个逻辑问题导致支付接口存在被薅羊毛的风险&#xff0c;这个时候能做的只能是赶快修复…

spring中@Inject和@Autowired的区别?分别在什么条件下使用呢?

问题&#xff1a;spring中Inject和Autowired的区别&#xff1f;分别在什么条件下使用呢&#xff1f; 我在浏览SpringSource上的一些博客&#xff0c;在其他一个博客中&#xff0c;那个作者用了Inject&#xff0c;但是我觉得他用Autowired也行 下面是一部分代码&#xff1a; …

Objective-C语言的动态性

Objective-C具有相当多的动态特性&#xff0c;基本的&#xff0c;也是经常被提到和用到的有动态类型&#xff08;Dynamic typing&#xff09;&#xff0c;动态绑定&#xff08;Dynamic binding&#xff09;和动态加载&#xff08;Dynamic loading&#xff09; 一、编译时和运行…

内存泄漏和内存溢出的区别

原文地址https://www.zhihu.com/question/40560123 简单来说&#xff0c;操作系统就像资源分配人员&#xff0c;你要使用内存的时候分给你&#xff0c;你用完了还给它。如果你使用了没有分配给你的内存就是内存溢出&#xff0c;如果你用完了没有还就是内存泄漏。会引起的问题&a…

怎么注销笔记本icloud_如何在笔记本电脑或台式机的Web浏览器中在线查看Apple iCloud照片

怎么注销笔记本icloudPicture this: you just returned from a beautiful vacation and want to show all those gorgeous photos to your family. But your phone just died. And since youre at a family dinner your laptop is nowhere to be found.想象一下&#xff1a;您刚…

棒棒糖 宏_棒棒糖图表

棒棒糖 宏AKA: lollipop plot又名&#xff1a;棒棒糖情节 WHY: a lollipop chart (LC) is a handy variation of a bar chart where the bar is replaced with a line and a dot at the end. Just like bar graphs, lollipop plots are used to make comparisons between diff…

ubuntu上如何安装tomcat

1. 在官网下载linux里面的tomcat 2. 放到DownLoads下面--把tomcat的压缩包放到DownLoads3. sudo mkdir /usr/local/tomcat/ -在usr/local/路径下新建一个tomcat的文件夹4 sudo tar zxvf tomcat。。。。tar.gz -C /usr/local/tomcat/---把解压后的tomcat放到usr/local/下的tomca…

leetcode 1734. 解码异或后的排列(位运算)

给你一个整数数组 perm &#xff0c;它是前 n 个正整数的排列&#xff0c;且 n 是个 奇数 。 它被加密成另一个长度为 n - 1 的整数数组 encoded &#xff0c;满足 encoded[i] perm[i] XOR perm[i 1] 。比方说&#xff0c;如果 perm [1,3,2] &#xff0c;那么 encoded [2,…

ZooKeeper3.4.5-最基本API开发

2019独角兽企业重金招聘Python工程师标准>>> package cn.itcast.bigdata.zk;import java.io.IOException; import java.util.List;import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEven…

字符串转换整数python_将Python字符串转换为Int:如何在Python中将字符串转换为整数

字符串转换整数pythonUnlike many other programming languages out there, Python does not implicitly typecast integers (or floats) to strings when you concatenate them to strings.与现有的许多其他编程语言不同&#xff0c;Python在将整数连接到字符串时不会隐式地将…

理解Java里面的必检异常和非必检异常

问题&#xff1a;理解Java里面的必检异常和非必检异常 Joshua Bloch在"Effective Java"里面说过 在可恢复的条件下和编程错误导致的运行时错误时&#xff0c;使用必检异常&#xff08;第二版的第52页&#xff09; 让我们来看一下我对这个的正确理解吧 下面是我对…

使用vim打开文件的16进制形式,编辑和全文替换

1、先用vim打开文件的二进制形式&#xff0c;如果不以二进制可能会产生转换错误。 vim -b file-to-open.dat 2、用xxd把文件转换成十六进制格式 :%!xxd 现在就可以对待普通文本一样查看和编辑二进制文件了。 3、vim 单文件替换方法 :%s/old/new/gc 全文执行替换,询问是…

nlp自然语言处理_不要被NLP Research淹没

nlp自然语言处理自然语言处理 (Natural Language Processing) 到底是怎么回事&#xff1f; (What is going on?) NLP is the new Computer VisionNLP是新的计算机视觉 With enormous amount go textual datasets available; giants like Google, Microsoft, Facebook etc have…

opencv 随笔

装环境好累&#xff0c;python3.6&#xff0c;opencv3.4 好不容易装好了&#xff0c;结果 addweight的时候总是报错 The operation is neither array op array (where arrays have the same size and the same number of channels), nor array op scalar, nor scalar op array …

js打开飞行模式_什么是飞行模式? 它有什么作用?什么时候应该打开它?

js打开飞行模式If youve flown on an airplane in the last decade and you have a smart phone, youve likely had to put that phone in airplane mode before the plane takes off.如果您在过去的十年中乘坐过飞机&#xff0c;并且拥有一部智能手机&#xff0c;那么您可能必…

在Java 里面怎么比较字符串

问题&#xff1a;在Java 里面怎么比较字符串 到目前为止&#xff0c;我使用 操作符去比较字符串在我的程序里面。然而&#xff0c;却产生了一个bug&#xff0c;将这个改为了.equals()以后&#xff0c;就把bug修复了 是不是太辣鸡了&#xff1f;它什么时候应该被使用或者说是不…