POJ 2499 Binary Tree 数学题解

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*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.

Sample Input

3
42 1
3 4
17 73

Sample Output

Scenario #1:
41 0

Scenario #2:
2 1

Scenario #3:
4 6

Source

TUD Programming Contest 2005 (Training Session), Darmstadt, Germany

本题名为二叉树,其实主要是考数学加速计算的方法。

本题思路最简单就是从目标节点往根节点查找,那么效率就等于树高了;

不过由于树高可能会极大,故此这样查找会超时。

那么就在查找根节点的时候,把题目的+-法变成除法查找,就可以极大加速查找了,由原来的超时变成0ms过了。

#include <stdio.h>
int main()
{
	int T, a, b, lc, rc, t = 1;
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d %d", &a, &b);
		rc = lc = 0;
		while (a != 1 || b != 1)
		{
			if (a > b)
			{
				int c = a / b;
				a %= b;//因为合法,故此只有b==1,有可能a==0
				if (!a) --c, a = 1;//最后a==0的时候要修正结果
				lc += c;
			}
			else
			{
				int c = b / a;
				b %= a;
				if (!b) --c, b = 1;
				rc += c;
			}
		}
		printf("Scenario #%d:\n%d %d\n\n", t++, lc, rc);
	}
	return 0;
}
时间: 2024-12-25 17:44:21

POJ 2499 Binary Tree 数学题解的相关文章

POJ 2499 Binary Tree 题解

本题使用所谓的辗转相除法. 还需要逆过来遍历二叉树.可以想象给出的数据点是根节点,然后遍历到根节点(1,1). 考的是根据给出的规则,总结规律的能力. #include <stdio.h> namespace BinaryTree2499_1 { int main() { int T, a, b, le, ri; scanf("%d", &T); for (int t = 1; t <= T; t++) { scanf("%d %d", &

Poj 2499 Binary Tree

题目链接:http://poj.org/problem?id=2499 思路: 结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求最短路径<a1, a2, a3,...,an>,考虑从已经知道的结点(a, b)开始找出最短路径回到根节点(1, 1): 即向左移动次数和向右移动次数最少回到根节点,由贪心算法,若 a>b 时,a 减少最大即减去 b: 若 a < b,b 减少最大即减去a值,循环直到到达根节点(1, 1

POJ 3252 Round Numbers 数学题解

Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets

POJ 2367 Genealogical tree 拓扑题解

一条标准的拓扑题解. 我这里的做法就是: 保存单亲节点作为邻接表的邻接点,这样就很方便可以查找到那些点是没有单亲的节点,那么就可以输出该节点了. 具体实现的方法有很多种的,比如记录每个节点的入度,输出一个节点之后,把这个节点对于其他节点的入度去掉,然后继续查找入度为零的点输出.这个是一般的做法了,效果和我的程序一样的. 有兴趣的也可以参考下我这种做法. #include <stdio.h> #include <string.h> #include <vector> us

LeetCode110 Blanced Binary Tree Java 题解

题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 题解: 判断一颗二叉树是不是平衡二叉树  ,平衡二叉树是每个节

leetcode Minimum Depth of Binary Tree C++题解

题目描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 给出一个二叉树,求其最短路径 最短路径定义:从根节点到最近的叶子节点的路径上的节点个数 思路描述 这道题对于我来说引发的思考比较多 首先明确一个比较大但很重要的概念,二叉

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

LeetCode: Maximum Depth of Binary Tree 题解

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解: 题意比较清楚, 找到从root出发最长的一条路径的长度. 采用DFS即可. 相似的一道题: Minimux Depth of Binary Tree 解法: http://