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:
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?
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*109) that represent
a node (i, j). You can assume that this is a valid node in the binary tree described above.
Every scenario consists of a single line containing two integers i and j (1 <= i, j <= 2*109) 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.
题意:这是一棵二叉树,其节点为(a,b),该节点的左孩子为(a+b,b),右孩子为(a,a+b),这颗树的根节点为(1,1),给出你一个节点,求从根节点到该节点从左边走了多少边,从右边走了多少边.
思路:该题有个规律,如果该节点的左边数>右边数,那就说明其是父节点的左孩子,反之,是其父节点的右孩子,则从该节点一步一步的往上走,直至返回到根节点.
题意:这是一棵二叉树,其节点为(a,b),该节点的左孩子为(a+b,b),右孩子为(a,a+b),这颗树的根节点为(1,1),给出你一个节点,求从根节点到该节点从左边走了多少边,从右边走了多少边.
思路:该题有个规律,如果该节点的左边数>右边数,那就说明其是父节点的左孩子,反之,是其父节点的右孩子,则从该节点一步一步的往上走,直至返回到根节点.
Sample Input
3
42 1
3 4
17 73
Sample Output
Scenario #1:
41 0
Scenario #2:
2 1
Scenario #3:
4 6
<span style="font-size:18px;">#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
int left,right,n,a,b;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
left=0;
right=0;
scanf("%d %d",&a,&b);
if(a==1&&b==1) //如果该节点直接是根节点,则左右走的不是皆为0
{
left=0;
right=0;
}
else if(a==1) //如果该节点的左边数等于1,则说明,从根节点到该节点只需走右边的边
{
left=0;
right=b-1;
}
else if(b==1) </span><span style="font-size:18px;">//如果该节点的右边数等于1,则说明,从根节点到该节点只需走左边的边</span><span style="font-size:18px;">
{
left=a-1;
b=0;
}
else
{
while(1) //当该节点两边的数不一样大小时,先从大的那边返回,直至返回到小于另一边
{
if(a>b)
{
left+=a/b;
int k;
k=a/b;
a=a-k*b;
if(a==1) //如果该节点的其中一边已经返回到根节点了,那么可直接计算另一边的步数并跳出
{
right+=b-1;
break;
}
}
else if(a<b)
{
right+=b/a;
int k;
k=b/a;
b=b-k*a;
if(b==1)
{
left+=a-1;
break;
}
}
}
}
printf("Scenario #%d:\n",i);
printf("%d %d\n\n",left,right);
}
return 0;
}</span>