当前位置:网站首页>POJ-2499 Binary Tree
POJ-2499 Binary Tree
2022-07-05 12:12:00 【全栈程序员站长】
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.cn/110203.html原文链接:https://javaforall.cn
边栏推荐
- HiEngine:可媲美本地的云原生内存数据库引擎
- mysql拆分字符串做条件查询
- 15 methods in "understand series after reading" teach you to play with strings
- Hash tag usage in redis cluster
- Multi table operation - Auto Association query
- Acid transaction theory
- Redirection of redis cluster
- ACID事务理论
- Uniapp + unicloud + Unipay realize wechat applet payment function
- Embedded software architecture design - message interaction
猜你喜欢
【主流Nivida显卡深度学习/强化学习/AI算力汇总】
一次生产环境redis内存占用居高不下问题排查
Why do you always fail in automated tests?
【上采样方式-OpenCV插值】
Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle
[yolov3 loss function]
Matlab struct function (structure array)
The survey shows that traditional data security tools cannot resist blackmail software attacks in 60% of cases
mysql拆分字符串做条件查询
Automated test lifecycle
随机推荐
Codeforces Round #804 (Div. 2)
你做自动化测试为什么总是失败?
Course design of compilation principle --- formula calculator (a simple calculator with interface developed based on QT)
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
Pytorch weight decay and dropout
Principle of persistence mechanism of redis
【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
Acid transaction theory
redis的持久化机制原理
Ncp1342 chip substitute pn8213 65W gallium nitride charger scheme
Embedded software architecture design - message interaction
Halcon 模板匹配实战代码(一)
Simple production of wechat applet cloud development authorization login
codeforces每日5题(均1700)-第五天
vscode快捷键
【yolov3损失函数】
Xi IO flow
跨平台(32bit和64bit)的 printf 格式符 %lld 输出64位的解决方式
Swift - enables textview to be highly adaptive
多表操作-子查询