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使用pip安装/卸载包「建议收藏」

    Python使用pip安装/卸载包「建议收藏」不一定需要专业编辑器,原生Python也能安装包,使用pip就可以了。1、首先确认电脑上已安装的Python有无pip程序。打开Python文件所在的位置,“Scripts”文件夹,查看。2、若无pip,则到官方下载最新版Python安装包,运行安装包,Python会自动升级,升级完毕后再次查看,pip程序已经存在了。Python官方下载地址进入某一个版本的下载页面,根据自己的需要下载…

    2022年10月16日
    3
  • chrome下document.cookie为空

    chrome下document.cookie为空今天遇到一个待解决的问题:关于Chrome浏览器下,可设置cookie,但无法读取的问题!baidu.cookie.set(‘hideMask’,’1′);从这里可以看到chrome中相关的cookie存储情况,能找到已设置成功的cookie值:chrome://chrome/settings/cookies但是,通过document…

    2022年7月20日
    40
  • 雷达系统设计及matlab仿真(一) 第一章 雷达基础知识概论(测距 距离分辨率 多普勒频率 雷达方程 噪声和信噪比 脉冲积累)

    雷达系统设计及matlab仿真(一) 第一章 雷达基础知识概论(测距 距离分辨率 多普勒频率 雷达方程 噪声和信噪比 脉冲积累)1.雷达基础知识了解2.雷达测距最大不模糊距离3.距离分辨率4.多普勒频率‘5.雷达方程噪声和信噪比6.搜索(警戒)7.脉冲积累相干积累与非相干积累8.雷达损失

    2022年5月4日
    75
  • 重建二叉树(Java)

    重建二叉树(Java)题目:输入一个链表的头结点,从尾到头反过来打印出每个结点的值。链表的结点定义如下:structListNode{ intm_nKey; ListNode*m_pNext;}第一思路:我的第一思路是从头到尾输出类比数组那样,于是乎想把链表中的链表结点的指针反转过来,改变链表的方向,然后实现从头到尾输出(结果为从尾到头输出),可是发现修改链表的指针,反转链表的结构比较麻烦。于是乎放弃。最优…

    2022年6月14日
    29
  • PHP网站常见安全漏洞,及相应防范措施总结

    PHP网站常见安全漏洞,及相应防范措施总结

    2021年9月24日
    43
  • mac怎么上传文件到服务器_linux传输文件到linux

    mac怎么上传文件到服务器_linux传输文件到linux前言我们使用mac时,想让本地文件上传至服务器,该怎么办呢windows系统,我们可以使用xftp或者rz命令,那么mac呢?mac系统,我们可以使用sftp、scp或者rz命令,本文介绍sft

    2022年7月28日
    9

发表回复

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

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