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 ...
随机推荐
- 初识ExtJS 6----自学笔记(一)
一.使用环境 这一点写在前面,是为了方便大家在找资料的时候可以直接定位环境版本. ExtJS版本 6.2 中文官方网站提供版本,网站地址http://extjs.org.cn/node/793 开发 ...
- ABAP术语-V1 Module
V1 Module 原文;http://www.cnblogs.com/qiangsheng/archive/2008/03/21/1115707.html Function module creat ...
- python新建一个表格xls并写入数据
# -*- coding:utf-8 -*- import xlwt workbook = xlwt.Workbook() # 新建一个工作簿 sheet = workbook.add_sheet(& ...
- shell习题第9题:sed的常用用法
[题目要求] 把一个文本文档的前5行中包含字母的行删除掉,同时把6到10行中的全部字母删除掉. [核心要点] sed命令 [脚本] .txt |sed '/[a-zA-Z]/d' .txt |sed ...
- cookie的简单认识
1.cookie Cokie又叫会话跟踪技术,实际上就是客户端硬盘上的一个文本文件,该文件用来存储一些数据. 首先,HTTP请求时无状态的,也就是说,你打开一个网页和下一个网页之间没有任何关系,数据不 ...
- $.ajax 完整参数
jquery中的ajax方法参数 url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意 ...
- Java实例 Part4:数组及其常用操作
目录 Part4:数组及其常用操作 Example01:将二维数组的行列交换 Example02:使用选择排序法对数组进行排序 Example03:使用冒泡排序法对数组进行排序 Example04:使 ...
- 关于LP64,ILP64,LLP64,ILP32,LP32字长(数据)模型
太长不看: 1.32位Windows和类Unix使用ILP32字长模型,64位Windows使用ILP64模型,64位类Unix使用LP64字长模型. 2.根据1,long在32位和64位Window ...
- 在vim编辑器中实现python的tab补全
在vim编辑器中实现python的tab补全 在vim编辑器中实现python tab补全插件有Pydiction,Pydiction可以实现下面python代码的自动补全: 1.简单python ...
- 20155216 实验二 Java面向对象程序设计
实验二:Java面向对象程序设计 实验内容 1.初步掌握单元测试和TDD. 2.理解并掌握面向对象的三要素:封装.继承.多态. 3.初步掌握UML建模. 4.熟悉S.O.L.I.D原则. 5.了解设计 ...