POJ-2499 Binary Tree

POJ-2499 Binary Tree

Binary Tree
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5211   Accepted: 2420

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

 1 /*
 2 题意:根(a,b),则左孩子为(a+b,b),右孩子为(a,a+b)
 3     给定(m,n),初试根为(1,1),从(1,1)到(m,n)需要往左子树走几次,
 4     往右子树走几次。
 5 思路:逆向思维,从(m,n)到(1,1)。
 6     给定(m,n),求其父亲,如果m>n,则他父亲是(m-n,n),
 7     否则(m,n-m)。
 8 代码一:  ----TLE
 9 #include <iostream>
10 
11 using namespace std;
12 
13 int main()
14 {
15     int T, a, b, lcnt, rcnt;
16     cin >> T;
17     for(int i = 1; i <= T; ++i)
18     {
19         cin >> a >> b;
20         lcnt = rcnt = 0;
21         while(a != 1 || b != 1)
22         {
23             if(a >= b)
24             {
25                 ++lcnt;
26                 a -= b;
27             }
28             else
29             {
30                 ++rcnt;
31                 b -= a;
32             }
33         }
34         cout << "Scenario #" << i << ':' << endl;
35         cout << lcnt << ' ' << rcnt << endl <<endl;
36     }
37     return 0;
38 }
39 
40 代码二:
41 用除法代替减法,得到的商即为往左走的次数,最后的m=m%n。
42 n>m时情况类推。
43 需要特别注意的是:
44     如果m>n,m%n == 0 怎么办?因为根(1,1)不可能有0存在,所以特殊处理一下:
45 次数:m/n-1;m=1
46 */
47 #include <iostream>
48 
49 using namespace std;
50 
51 int main()
52 {
53     int T, a, b, lcnt, rcnt;
54     cin >> T;
55     for(int i = 1; i <= T; ++i)
56     {
57         cin >> a >> b;
58         lcnt = rcnt = 0;
59         while(a != 1 || b != 1)
60         {
61             if(a >= b)
62             {
63                 if(a % b)
64                 {
65                     lcnt += a/b;
66                     a %= b;
67                 }
68                 else
69                 {
70                     lcnt += a/b-1;
71                     a = 1;
72                 }
73             }
74             else
75             {
76                 if(b % a)
77                 {
78                     rcnt += b/a;
79                     b %= a;
80                 }
81                 else
82                 {
83                     rcnt += b/a-1;
84                     b = 1;
85                 }
86             }
87         }
88         cout << "Scenario #" << i << ':' << endl;
89         cout << lcnt << ' ' << rcnt << endl <<endl;
90     }
91     return 0;
92 }

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/110203.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python后端常见架构_常见的后端框架

    python后端常见架构_常见的后端框架后端vs前端如果您是Web开发世界的新手,后端和前端开发之间的区别可能不那么明显,但是,了解两者之间的区别很重要。以下是前端开发人员与后端开发人员的一些区别。前端开发:前端开发人员在很大程度上负责用户所看到的内容(即网站页面),前端开发人员主要使用HTML,CSS和JavaScript。他们的主要关注点是创建出色的用户体验,并确保网站设计和布局或Web应用程序始终具有凝聚力。后端开发:另一方面,后…

    2022年6月10日
  • 内测体验:JetBrains面向未来的Fleet编辑器是什么+究竟怎样 使用初体验+与vsc对比

    内测体验:JetBrains面向未来的Fleet编辑器是什么+究竟怎样 使用初体验+与vsc对比引言上个月,我在看到某公众号推广后,作为热衷于先进技术、常年游历于各个软件公司内测组的用户自然是早早申请了内测。因为在申请时官网的公告是“我们也不知道新一代编辑器(Fleet)什么时候可以与大家见面”,因此我也没有过多在意。令人意外的是,昨天晚上22:09,我收到了来自JetBrains的邮件。此处点名一下GitHubCopilot项目,申请完了这么久还不给个信[手动旺柴]简介如果你实在不知道什么是JetBrains,那你也应该知道PyCharm,再次也要知道AndroidStudio。

    2022年6月7日
  • adb安装apk_adb常用命令最全解析及应用

    adb安装apk_adb常用命令最全解析及应用启动adbserver命令:adbstart-server停止adbserver命令:adbkill-server查看adb版本命令:adbversion查询已连接设备/模拟器命令:adbdevices安装APK命令格式:adbinstall[-lrtsdg]参数:adbinstall后面可以跟一些可选参数来控制安装APK的行为,可用参数及含义如下:参数含义…

    2022年5月2日
  • idea2019.3激活码(注册激活)

    (idea2019.3激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/ide…

    2022年3月29日
  • VS2019、VS2017、VS2013产品密钥

    VS2019、VS2017、VS2013产品密钥VisualStudio2013VisualStudioUltimate2013KEY:BWG7X-J98B3-W34RT-33B3R-JVYW9VisualStudioPremium2013KEY:FBJVC-3CMTX-D8DVP-RTQCT-92494VisualStudioProfessional2013KEY:XDM3T-W3T3V-MGJWK-8B…

    2022年5月19日
  • oracle 触发器通知,Oracle触发器详细介绍

    oracle 触发器通知,Oracle触发器详细介绍欢迎进入Oracle社区论坛,与200万技术人员互动交流>>进入触发器是特定事件出现的时候,自动执行的代码块。类似于存储过程,但是用户不能直接调用他们。功能:1、允许/限制对表的修改2、自动生成派生列,比如自增字段3、强制数据一致性4、提供欢迎进入Oracle社区论坛,与200万技术人员互动交流>>进入触发器是特定事件出现的时候,自动执行的代…

    2022年7月27日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号