Binary Tree(生成二叉树)
Description
Binary trees are a common data structure in computer science. In this problem we will look at an infinite binary tree where the nodes contain a pair of integers. The tree is constructed like this:
- The root contains the pair (1, 1).
- If a node contains (a, b) then its left child contains (a + b, b) and its right child (a, a + b)
Problem
Given the contents (a, b) of some node of the binary tree described above, suppose you are walking from the root of the tree to the given node along the shortest possible path. Can you find out how often you have to go to a left child and how often to a right child?
Input
Every scenario consists of a single line containing two integers i and j (1 <= i, j <= 2*10 9) that represent
a node (i, j). You can assume that this is a valid node in the binary tree described above.
Output
Sample Input
3
42 1
3 4
17 73
Sample Output
Scenario #1:
41 0 Scenario #2:
2 1 Scenario #3:
4 6
题目意思:当时一看题目确实被吓到了,以为真的是数据结构中的二叉树,毕竟作为一个萌新acmer,真真被唬住了。
看了看样例其实和二叉树没有太大关系,我们知道二叉树的根节点是(1,1),题目给一个(l,r)求其回到(1,1)
向左向右转的次数。
解题思路:我们先来考虑一下(l,r)的来源,因为r!=l,若l>r,那么(l,r)来自于(l-r,r)向左转;若r>l,
那么(l,r)来自于(l,r-l)向右转。那么可以通过递推的不断相减得到以下的代码:
#include<stdio.h>
#include<string.h>
int main()
{
int t;
long long a,b,m,n,lcount,rcount,i;
scanf("%d",&t);
i=;
while(t--)
{
scanf("%lld%lld",&a,&b);
lcount=;
rcount=;
while()
{
if(a>b)
{
a=a-b;
lcount++;
}
if(b>a)
{
b=b-a;
rcount++;
}
if(b==&&a==)
break;
}
printf("Scenario #%lld:\n",i++);
printf("%lld %lld\n\n",lcount,rcount);
}
return ;
}
很不幸的是交上之后直接时间超限,再看看题 two integers i and j (1 <= i, j <= 2*10 9),都达到了数亿的数量级,显然会造成数亿次的常数
运算,那么就需要换换思路了,我参考了一下网上大佬们的算法,运用除法来代替减法实现加速运算,这种思路其实在之前的某些题目中
运用过。
上代码:
#include<stdio.h>
#include<string.h>
int main()
{
int t;
long long a,b,c,m,n,lcount,rcount,i;
scanf("%d",&t);
i=;
while(t--)
{
scanf("%lld%lld",&a,&b);
lcount=;
rcount=;
while()
{
if(a>b)
{
c=a/b;
a=a%b;///当b==1时,有可能造成a==0
if(a==)///此时需要调成结果
{
c--;
a=;
}
lcount=lcount+c;
}
if(b>a)
{
c=b/a;
b=b%a;
if(b==)
{
c--;
b=;
}
rcount=rcount+c; }
if(b==&&a==)
break;
}
printf("Scenario #%lld:\n",i++);
printf("%lld %lld\n\n",lcount,rcount);
}
return ;
}
不得不感慨,还是得学习啊。
Binary Tree(生成二叉树)的更多相关文章
- 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化
遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [LeetCode] Invert Binary Tree 翻转二叉树
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 数据结构《9》----Threaded Binary Tree 线索二叉树
对于任意一棵节点数为 n 的二叉树,NULL 指针的数目为 n+1 , 线索树就是利用这些 "浪费" 了的指针的数据结构. Definition: "A binary ...
随机推荐
- OO课程总结
OO课程总结 OO终于划上了句号,这学期熬夜熬得最多的一门课,掉了最多头发的一门课. 一.测试与正确性 测试是最最最常见的用来找到程序错误以及验证程序正确的手段,在之前的作业中,写完代码还是会存在很多 ...
- 【项目笔记】完成一个基于SSM框架的增删改查的模块后总结的问题
最近为了准备新工作重新摸出了SSM框架,同时从0学习了JQuery,终于用一周做完了一个包括增删改查的模块(主要是属性太多了,其中一个类50+,复制粘贴耗时). 从中特意记下了几个遇到的问题,总结一下 ...
- 使用tp3.2和mbUploadify.js上传图片的代码,记录一下
HTML: <div class="form-group"> <label class="col-sm-1 control-label no-paddi ...
- thinkphp5查询表达式IN使用小计
根据多个id批量更新指定字段值 $map[] = ['id','in', input('post.id/a')]; $result = db('picture')->where($map)-&g ...
- Homebrew(brew)安装MySQL成功后无法登录
Homebrew简称brew,OSX上的软件包管理工具,在Mac终端可以通过brew安装.更新.卸载各种软件,(简直就是神器级武器). 废话不多说,没安装brew自己去百度学习安装,这里就不多说了. ...
- ARM设备树
学习目标:学习设备树相关内容: 一.概念 在Linux 2.6中,ARM架构的板极硬件细节过多地被硬编码在arch/arm/plat-xxx和arch/arm/mach-xxx,在kernel中存在大 ...
- C语言链队列
链队列类似于单链表,为了限制只能从两端操作数据,其结构体内有2个指针分别指向头尾,但队列里的节点用另一种结构体来表示,头尾指针则为指向该结构体的类型.只能通过操作头尾指针来操作队列. typedef ...
- python range,xrange区别
range: 直接生成一个列表对象 xrange: 生成一个xrange对象 xrange使用: 操作一个非常大的数据时,而且没存比较吃紧的时,可以使用xrange来节省内存 xrange一般在循环里 ...
- dtree的自定义select动作
项目中用到了dtree,别问我为什么用这么古老的插件,因为简单啊orz,文件树的条目不多,detree加载卡顿的问题也不用解决,开森. 在使用过程中在选择节点后需要自定义一些onclick的动作,本来 ...
- 阻止Quartus优化掉信号
使用SignalTap II Logic Analyzer观察信号,有时要观察的信号会被Quartus优化掉,这种情况下可以给信号指定属性.以下例子均使用Verilog. 1. 如果是组合逻辑信号,可 ...