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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux目录结构详解_linux系统配置文件目录

    linux目录结构详解_linux系统配置文件目录前言平常linux系统用的也不少,那么linux下的每个目录都是用来干什么的,小伙伴们有仔细研究过吗?让我们来了解下吧Linux系统目录结构登录系统后,在当前命令窗口下输入命令:[root@

    2022年8月6日
    6
  • phcharm激活码破解方法「建议收藏」

    phcharm激活码破解方法,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月14日
    79
  • 深度剖析LinkedHashSet「建议收藏」

    深度剖析LinkedHashSet「建议收藏」HashMap是一个利用数组存储key-value键值对的一个数据结构,为了有序的要求,然后我们引入了LinkedHashMap来满足我们对顺序的要求,再到后面我们学习了HashSet这种数据结构,利用的是HashMap的Key的唯一性来实现HashSet的去重的目的LinkedHashSet也HashSet一样也在内部使用了HashMap,因为LinkedHashSet要维持元素之间的顺序,所以它使用的实HashMap的有序版本,也就是LinkedHashMap

    2022年10月12日
    0
  • ProgressDialog不显示

    ProgressDialog不显示一般用ProgressDialog来提示用户等待耗时操作,如图中代码块IsVideoQualityWithFace();如果不在单独线程中执行的话,ProgressDialog将不会显示 AlertDialog.Builderbuilder=newAlertDialog.Builder(context); builder.setTitle(“提示”) .setMessage

    2022年7月14日
    39
  • 什么都能播放的媒体播放器——Potplayer

    什么都能播放的媒体播放器——Potplayer“它体积小巧、界面简洁、解码功能强大、它是视屏播放器的不二之选!”——来自一个朋友“PotPlayerisbyfarthebestvideoplayerIhaveusedyet.Thefactthatit’sfreeisicingonthecake.”——来自另一个朋友没错,它就是本期要介绍的主角Potplayer。

    2022年7月12日
    31
  • 【论文笔记】A Multi-Task Learning Formulation for Predicting Disease Progression「建议收藏」

    AMulti-TaskLearningFormulationforPredictingDiseaseProgression论文地址摘要1.介绍2.多任务回归方程2.1时间平滑先验2.2解决数据不完整问题2.3LASSO时间群正则2.3.1纵向(时间项)稳定特征选择2.4本文算法3.实验4.结论论文地址AMulti-TaskLearningFormulati…

    2022年4月16日
    34

发表回复

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

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