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:53 2人阅读 评论(0) 收藏

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

  2. Binary Indexed Tree 分类: ACM TYPE 2014-08-29 13:08 99人阅读 评论(0) 收藏

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

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

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

  4. hadoop调优之一:概述 分类: A1_HADOOP B3_LINUX 2015-03-13 20:51 395人阅读 评论(0) 收藏

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

  5. Codeforces 343D Water Tree 分类: Brush Mode 2014-10-05 14:38 98人阅读 评论(0) 收藏

    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:56 101人阅读 评论(0) 收藏

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

  7. Help Me with the Game 分类: POJ 2015-06-29 16:34 17人阅读 评论(0) 收藏

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

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

    这里先只考虑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:04 97人阅读 评论(0) 收藏

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

随机推荐

  1. jQuery 扩展功能

    源码如下: /*! * 说明:Jquery库扩展 * 创建时间: leo 2016/10/13 */ (function (window, jQuery, undefined) { jQuery.ex ...

  2. c++ unique_lock lock_guard

    unique_lock template <class Mutex> class unique_lock; Unique lock A unique lock is an object t ...

  3. php扩展的基本安装

    phpize ./config --with-php-config=.. make&make install php.ini

  4. paper 64:尺度空间(Scale space)理论

    尺度空间方法的基本思想是:在视觉信息处理模型中引入一个被视为尺度的参数,通过连续变化尺度参数获得不同尺度下的视觉处理信息,然后综合这些信息以深入地挖掘图像的本质特征.尺度空间方法将传统的单尺度视觉信息 ...

  5. 夺命雷公狗—angularjs—20—$watch监听的用法

  6. 夺命雷公狗---Thinkphp----16之首页的完成及全站的完成

    刚才我们首页只是完成了一部分的数据,那么这里我们就来将他所以的完成: IndexController.class.php控制器代码如下所示: <?php namespace Home\Contr ...

  7. zw版【转发·台湾nvp系列例程】HALCON MirrorRegion (Delphi)

    zw版[转发·台湾nvp系列例程]HALCON MirrorRegion (Delphi) procedure TForm1.Button1Click(Sender: TObject);var img ...

  8. [py]安装ipython

    系统:crunch bang11+python2.7.3 准备工具: sudo apt-get install python-pip python-dev build-essential 安装setu ...

  9. mysql创建视图

    CREATE ALGORI`sync_user`CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER V ...

  10. 利用foreach对页面控件的遍历 及三目运算符的使用

    1.利用foreach对页面控件的遍历 及三目运算符的使用 利用div将一组CheckBox放在一起用于遍历 <body> <form id="form1" ru ...