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 ...
随机推荐
- Java并发编程(十一)常用工具
Java为开发提供了很多有用的工具类,这些工具类可以帮助我们更加高效的编写并发程序,本篇我们将介绍这些实用工具的用法. ThreadLocal ThreadLocal类用于解决多线程共享一个变量的问题 ...
- angular路由传参和获取路由参数的方法
1.首先是需要导入的模块 import { Router } from "@angular/router";//路由传参用到 import{ActivatedRoute,Param ...
- 使用xampp发现php的date()函数与本地相差7个小时
具体方法: 1. 打开php.ini 2. 搜索timezone 3. 修改为PRC 4. 回车键 5. 修改为PRC 6. 完成 没想到这么一个小问题也是一个大坑,在网上找了半天基本都是说要修改这个 ...
- 获取当前目录下所有php文件内的函数名
$dir = dirname(__FILE__); $files = scandir($dir); foreach($files as $name){ if($name == '.' || $name ...
- Ubuntu18.04 设置wifi热点
(1)在终端输入 命令:nm-connection-editor (2)然后点击wifi网络进行配置即可:
- python学习第二天 -----2019年4月17日
第二周-第02章节-Python3.5-模块初识 #!/usr/bin/env python #-*- coding:utf-8 _*- """ @author:chen ...
- (数据科学学习手札51)用pymysql来操控MySQL数据库
一.简介 pymysql是Python中专门用来操控MySQL数据库的模块,通过pymysql,可以编写简短的脚本来方便快捷地操控MySQL数据库,本文就将针对pymysql的基本功能进行介绍: 二. ...
- 数据结构与算法之排序(3)插入排序 ——in dart
插入排序的思想比冒泡.选择要复杂,效率也比前两者更高.插入排序算法中运用了分治.逆向冒泡等思想,假设i之前的都是排好序的,i之后的都是待比较并排序的,然后逐次逆向向前比较,若小于前值,则将前值循环依次 ...
- fixed layout android
http://benfrain.com/easy-css-fix-fixed-positioning-android-2-2-2-3/ http://caniuse.com/#feat=css-fix ...
- 20155204 2016-2017-2 《Java程序设计》第10周学习总结
20155204 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 1.计算机网络概述 路由器和交换机组成了核心的计算机网络,计算机只是这个网络上的节点以及控 ...