Description

Background 
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

The first line contains the number of scenarios. 
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

The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing two numbers l and r separated by a single space, where l is how often you have to go left and r is how often you have to go right when traversing the tree from the root to the node given in the input. Print an empty line after every scenario.

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(生成二叉树)的更多相关文章

  1. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  2. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  3. 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

    [104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...

  4. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  5. [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, ...

  6. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  7. [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 ...

  8. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

  9. hdu1710(Binary Tree Traversals)(二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  10. 数据结构《9》----Threaded Binary Tree 线索二叉树

    对于任意一棵节点数为 n 的二叉树,NULL 指针的数目为  n+1 , 线索树就是利用这些 "浪费" 了的指针的数据结构. Definition: "A binary ...

随机推荐

  1. c#使用emit方法DB,实体相互转换

    网上有很多ORM框架和数据库转换方法. c#由EF控制,但是大家知道的是影响效率和底层控制. 因此最近几个月一直在研究.最后产出了Hikari数据库连接池. 但是该库只是定位在连接池,无法有效进行后续 ...

  2. ABAP术语-V1 Module

    V1 Module 原文;http://www.cnblogs.com/qiangsheng/archive/2008/03/21/1115707.html Function module creat ...

  3. php计算上个月是几月份

    PHP计算上个月的时间, $date = date("Y-m-d"); $arr = explode('-',$date); foreach ($arr as $key=>$ ...

  4. Docker部署大型互联网电商平台

    1.Docker简介 1.1虚拟化 1.1.1什么是虚拟化 在计算机中,虚拟化(英语:Virtualization)是一种资源管理技术,是将计算机的各种实体资源,如服务器.网络.内存及存储等,予以抽象 ...

  5. 关于STM32 DMA相关总结[概述知识点]

    关于DMA相关知识的总结,写给未来的自己,希望有帮助.立个Flag[坚持写博客总结自己工作或学习记录自己的生活] ------------------------------------------- ...

  6. 如何通过审计安全事件日志检测密码喷洒(Password Spraying)攻击

    许多渗透测试人员和攻击者通常都会使用一种被称为“密码喷洒(Password Spraying)”的技术来进行测试和攻击.对密码进行喷洒式的攻击,这个叫法很形象,因为它属于自动化密码猜测的一种.这种针对 ...

  7. 《Java核心技术36讲》阅读笔记:Exception和Error有什么区别?

    1.Exception 和 Error有什么区别?运行时异常与一般异常有什么区别? Exception和Error都继承自java.lang.Throwable.在Java中只有Throwable的实 ...

  8. [Err] ERROR: wrong record type supplied in RETURN NEXT

    在写GP 输出不定长列数据表 函数时,报了一个错,百思不得其解.在公司大佬帮助下,知道是什么鬼了.. 先看看例子吧: ---- 函数定义 CREATE OR REPLACE FUNCTION &quo ...

  9. Ubuntu配置android环境

    jdk:http://www.oracle.com/technetwork/cn/java/javase/downloads/index.html 安装JDK的步骤:http://jingyan.ba ...

  10. oracle-11g-64位安装和plaql

    1.oracle卸载 如果是新装,请跳过此步骤 卸载步骤: 1.停止所有服务 2.用自带删除软件,删除所有目录 3.打开注册表: -->运行regedit,删除HKEY_LOCAL_MACHIN ...