当前位置:网站首页>POJ-2499 Binary Tree
POJ-2499 Binary Tree
2022-07-05 12:14:00 【Full stack programmer webmaster】
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 The question : root (a,b), The left child is (a+b,b), The right child is (a,a+b)
3 Given (m,n), The initial root is (1,1), from (1,1) To (m,n) You need to walk to the left sub tree several times ,
4 Walk to the right sub tree several times .
5 Ideas : Converse thinking , from (m,n) To (1,1).
6 Given (m,n), Beg his father , If m>n, Then his father is (m-n,n),
7 otherwise (m,n-m).
8 Code 1 : ----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 Code 2 :
41 Use division instead of subtraction , The quotient obtained is the number of times to go left , final m=m%n.
42 n>m And so on .
43 Here's the thing to watch out for :
44 If m>n,m%n == 0 What do I do ? Because root (1,1) There can be no 0 There is , So special treatment :
45 frequency :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 }
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/110203.html Link to the original text :https://javaforall.cn
边栏推荐
- Get all stock data of big a
- abap查表程序
- Codeforces Round #804 (Div. 2)
- What is digital existence? Digital transformation starts with digital existence
- Riddle 1
- Vscode shortcut key
- Uniapp + unicloud + Unipay realize wechat applet payment function
- Codeforces Round #804 (Div. 2)
- Simply solve the problem that the node in the redis cluster cannot read data (error) moved
- MySQL multi table operation
猜你喜欢
[untitled]
Four operations and derivative operations of MATLAB polynomials
互联网公司实习岗位选择与简易版职业发展规划
Yolov5 target detection neural network -- calculation principle of loss function
16 channel water lamp experiment based on Proteus (assembly language)
Select drop-down box realizes three-level linkage of provinces and cities in China
【pytorch 修改预训练模型:实测加载预训练模型与模型随机初始化差别不大】
mysql拆分字符串做条件查询
Tabbar configuration at the bottom of wechat applet
Embedded software architecture design - message interaction
随机推荐
[upsampling method opencv interpolation]
嵌入式软件架构设计-消息交互
Want to ask, how to choose a securities firm? Is it safe to open an account online?
MySQL regular expression
Semantic segmentation experiment: UNET network /msrc2 dataset
Master the new features of fluent 2.10
1 plug-in to handle advertisements in web pages
Network five whip
Check the debug port information in rancher and do idea remote JVM debug
abap查表程序
GPS数据格式转换[通俗易懂]
Sentinel sentinel mechanism of master automatic election in redis master-slave
多表操作-自关联查询
Mongodb replica set
MySQL data table operation DDL & data type
Wireless WiFi learning 8-channel transmitting remote control module
yolov5目标检测神经网络——损失函数计算原理
【ijkplayer】when i compile file “compile-ffmpeg.sh“ ,it show error “No such file or directory“.
[pytorch pre training model modification, addition and deletion of specific layers]
yolov5目標檢測神經網絡——損失函數計算原理