【原】 POJ 2499 Binary Tree 优化经典 解题报告

 

http://poj.org/problem?id=2499

 

root为(1,1),节点(a,b)的左孩子为(a+b,b),右孩子为(a,a+b)。
求从root到给定节点(a,b)所走过的左右路径各为多少


方法:不断优化算法的经典例子,类似于求最大公约数
TLE了几次才逐渐改进的算法:TLE-->0ms
详见注释

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

 
   1: #include <stdio.h>
   2:  
   3: void Traversal( int& l, int& r, int a, int b )
   4: {
   5:     /* 递归形式的最原始的算法,没有优化
   6:     if( a==1 && b==1 )
   7:         return ;
   8:     if( a>b )  // (a,b) is left child
   9:         Traversal( ++l, r, a-b, b ) ;
  10:     else
  11:         Traversal( l, ++r, a, b-a ) ;
  12:     */
  13:  
  14:     //优化1:改递归形式为循环形式
  15:     int multi ;
  16:     while( a!=1 || b!=1 )
  17:     {
  18:         //优化2:a==1和b==1这两种特殊情况要处理,提前结束循环
  19:         if( a==1 )  //只能走右路径
  20:         {
  21:             r += b-1 ;
  22:             break ;
  23:         }
  24:         if( b==1 )  //只能走左路径
  25:         {
  26:             l += a-1 ;
  27:             break ;
  28:         }
  29:  
  30:         //优化3:不再像递归形式那样,每次只走一个边(++l或++r)。
  31:         //         利用a、b之间的倍数关系优化算法
  32:         if( a>b )
  33:         {
  34:             multi = a/b ;
  35:             a -= b*multi ;
  36:             l += multi ;
  37:         }
  38:         else
  39:         {
  40:             multi = b/a ;
  41:             b -= a*multi ;
  42:             r += multi ;
  43:         }
  44:     }
  45: }
  46:  
  47: void run2499()
  48: {
  49:     int n ;
  50:     int i ;
  51:     int a,b ;
  52:     int l,r ;
  53:  
  54:     scanf( "%d", &n );
  55:     for( i=1 ; i<=n ; ++i )
  56:     {
  57:         scanf( "%d%d", &a,&b ) ;
  58:  
  59:         l = r = 0 ;
  60:         Traversal( l, r, a, b ) ;
  61:         
  62:         printf( "Scenario #%d:\n", i ) ;
  63:         printf( "%d %d\n\n", l,r  ) ;
  64:     }
  65: }
posted @ 2010-11-08 19:17  Allen Sun  阅读(1072)  评论(0编辑  收藏  举报