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)
上一篇 2021年8月22日 上午6:00
下一篇 2021年8月22日 上午7:00


相关推荐

  • Hunyuan-MT-7B从零部署教程:支持藏蒙维哈朝的开源翻译大模型

    Hunyuan-MT-7B从零部署教程:支持藏蒙维哈朝的开源翻译大模型

    2026年3月12日
    2
  • 如何配置pytorch_pytorch如何下载

    如何配置pytorch_pytorch如何下载1.好像不支持python3.8。直接从setting里面安装时不行的,按其它教程(https://blog.csdn.net/lyz21/article/details/104295042)从官网https://pytorch.org/get-started/locally/,拷贝链接用pip下载,一直报找不到版本。后来发现,python3.8的原因,改成python3.7可以了,但会一直连接超时。2.发现要下载的其实是这两个文件:点开下面的两个链接,用下载软件下载了,我下到了e盘,直接pip

    2025年6月18日
    5
  • Mysql最左匹配原则

    Mysql最左匹配原则看了好多博客 讲讲自己的理解 索引的底层是一颗 B 树 那么联合索引当然还是一颗 B 树 只不过联合索引的健值数量不是一个 而是多个 构建一颗 B 树只能根据一个值来构建 因此数据库依据联合索引最左的字段来构建 B 树 例子 假如创建一个 a b 的联合索引 那么它的索引树是这样的可以看到 a 的值是有顺序的 1 1 2 2 3 3 而 b 的值是没有顺序的 1 2 1 4 1 2 所以 b 2 这种查询

    2026年3月20日
    2
  • golang面试题(带答案)[通俗易懂]

    golang面试题(带答案)[通俗易懂]1.下面代码输出什么,为什么 //make([]T,length,capacity) s1:=[]int{1,2,3} fmt.Println(s1,”哈哈”)//[123] s2:=s1 fmt.Println(s1,”哈哈”)//[123] fori:=0;i<3;i++{ s2[i]=s2[i]+1 } fmt.Println(s1)//[234] fmt.Println(s2)//[234][12

    2022年6月29日
    90
  • html每隔5秒自动执行一次,如何用crontab每5分钟执行一次?

    html每隔5秒自动执行一次,如何用crontab每5分钟执行一次?请问利用 crontab 设置每 6 小时执行一次脚本 我写的 我的需求是每天 0 点 6 点 12 点 18 点但结果是只在每天的 12 点和 18 点运行你写的脚步应该没有问题 要不 12 点和 18 点也不会运行 可能是其他原因 主机是不是中间停过呢 linux 新手提问 我有一段程序 test py 想让 linu 我也来补充下 除了楼上提出的 crontab 外 还可以使用 python 自带的定时任务库 sched 使用 c

    2026年3月26日
    1
  • pythonrandom函数用法_python之random模块函数的使用

    pythonrandom函数用法_python之random模块函数的使用1)random.random()#用于生成一个0到1的随机浮点数,(0,1】2)random.randint(a,b)#用于生成一个指定范围内的整数,【a,b】3)random.randrange([start],stop[,step])#从指定范围内,按指定基数递增的集合中获取一个随机数。注意参数是整数,且不包括stop。random.randrange(10,30,2),结果相当…

    2022年5月13日
    68

发表回复

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

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