Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏

时间:2023-02-12 13:06:21
Binary Tree
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6355   Accepted: 2922

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*109) 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
给一个节点,计算从根节点到所给节点的所经过的左节点,和右节点的个数。
#include <cstdio>
#include <string.h>
#include <cmath>
#include <iostream>
#include <algorithm>
#define WW freopen("output.txt","w",stdout)
using namespace std;
int main()
{
int T,w=1;
int L,R;
int sum;
int ans;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&L,&R);
sum=0;
ans=0;
while(L!=1||R!=1)
{
if(L>R)
{
int a=(L-1)/R;//进行优化不然会TLE
ans+=a;
L-=(a*R);
}
else if(R>L)
{
int a=(R-1)/L;
sum+=a;
R-=(L*a);
} }
printf("Scenario #%d:\n",w++);
printf("%d %d\n\n",ans,sum);//两个换行
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Binary Tree 分类: POJ 2015-06-12 20:34 17人阅读 评论(0) 收藏的更多相关文章

  1. 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20&colon;53 2人阅读 评论&lpar;0&rpar; 收藏

    Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...

  2. Binary Indexed Tree 分类: ACM TYPE 2014-08-29 13&colon;08 99人阅读 评论&lpar;0&rpar; 收藏

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; int n, ...

  3. Poj 2559 最大矩形面积 v单调栈 分类: Brush Mode 2014-11-13 20&colon;48 81人阅读 评论&lpar;0&rpar; 收藏

    #include<iostream> #include<stack> #include<stdio.h> using namespace std; struct n ...

  4. hadoop调优之一:概述 分类: A1&lowbar;HADOOP B3&lowbar;LINUX 2015-03-13 20&colon;51 395人阅读 评论&lpar;0&rpar; 收藏

    hadoop集群性能低下的常见原因 (一)硬件环境 1.CPU/内存不足,或未充分利用 2.网络原因 3.磁盘原因 (二)map任务原因 1.输入文件中小文件过多,导致多次启动和停止JVM进程.可以设 ...

  5. Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14&colon;38 98人阅读 评论&lpar;0&rpar; 收藏

    Mad scientist Mike has constructed a rooted tree, which consists of n vertices. Each vertex is a res ...

  6. NYOJ 119 士兵杀敌(三)【ST算法】 分类: Brush Mode 2014-11-13 20&colon;56 101人阅读 评论&lpar;0&rpar; 收藏

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=119 解题思路: RMQ算法. 不会的可以去看看我总结的RMQ算法. http://blo ...

  7. Help Me with the Game 分类: POJ 2015-06-29 16&colon;34 17人阅读 评论&lpar;0&rpar; 收藏

    Help Me with the Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3706   Accepted: ...

  8. bzoj 1041 圆上的整点 分类: Brush Mode 2014-11-11 20&colon;15 80人阅读 评论&lpar;0&rpar; 收藏

    这里先只考虑x,y都大于0的情况 如果x^2+y^2=r^2,则(r-x)(r+x)=y*y 令d=gcd(r-x,r+x),r-x=d*u^2,r+x=d*v^2,显然有gcd(u,v)=1且u&l ...

  9. Segment Tree 分类: ACM TYPE 2014-08-29 13&colon;04 97人阅读 评论&lpar;0&rpar; 收藏

    #include<iostream> #include<cstdio> using namespace std; struct node { int l, r, m; int ...

随机推荐

  1. Qt之布局管理--基本布局

    Qt提供的布局类以及他们之间的继承关系QLayout-----QGirdLayout | ---QBoxLayout----QHBoxLayout | --QVBoxLayout----------- ...

  2. php安全模式

    http://www.cnblogs.com/samson/archive/2011/08/08/2130550.html php安全模式:safe_mode=on|off启用safe_mode指令将 ...

  3. 关于wordpress后台首页加载ajax&period;googleapis特别慢的解决办法

    通过审查元素发现,拖慢后台加载速度的主要是两个路径 1.https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js 2.h ...

  4. &lbrack;LeetCode&rsqb; Perfect Number 完美数字

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  5. 从网卡发送数据再谈TCP&sol;IP协议—网络传输速度计算-网卡构造

    在<在深谈TCP/IP三步握手&四步挥手原理及衍生问题—长文解剖IP>里面提到 单个TCP包每次打包1448字节的数据进行发送(以太网Ethernet最大的数据帧是1518字节,以 ...

  6. 暑期培训7日游解题思路(day1~day3)

    暑期培训7日游解题思路(day1~day3) day1 第一天,王聿中老师出的题目比较简单,T1很水,T2是个简单的DP,T3还是有一点意思的.在网格图中删掉若干条边,使得所有格子都联通,求删掉的边的 ...

  7. CodeFirst&plus;MySql开发

    CodeFirst+MySql开发简单入门 记录一下使用Mysql进行EF Codefirst方式开发的简单过程. 0.准备工作 安装MySql,mysql-connector-net,mysql-f ...

  8. 【nodejs】初识 NodeJS(一)

    构建一个基础的 http 服务器 需要引用 http 模块,http 模块是 node.js 的内置模块. var http = require('http'); http.createServer( ...

  9. C&num;编程(十九)----------部分类

    部分类 C#中使用关键字partial把类,结构或结构放在多个文件中.一般情况下,一个类全部驻留在单个文件中.但有时候,多个开发人员需要访问同一个类,或者某种类型的代码生成器生成了一个类的某部分,所以 ...

  10. xsl如何实现递归复制?

    <xsl:template match="*" mode="addSeatSelectionToAirProduct"> <xsl:eleme ...