• 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*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
    题目意思:当时一看题目确实被吓到了,以为真的是数据结构中的二叉树,毕竟作为一个萌新acmer,真真被唬住了。
    看了看样例其实和二叉树没有太大关系,我们知道二叉树的根节点是(1,1),题目给一个(l,r)求其回到(1,1)
    向左向右转的次数。
    解题思路:我们先来考虑一下(l,r)的来源,因为r!=l,若l>r,那么(l,r)来自于(l-r,r)向左转;若r>l,
    那么(l,r)来自于(l,r-l)向右转。那么可以通过递推的不断相减得到以下的代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 int main()
     4 {
     5     int t;
     6     long long a,b,m,n,lcount,rcount,i;
     7     scanf("%d",&t);
     8     i=1;
     9     while(t--)
    10     {
    11         scanf("%lld%lld",&a,&b);
    12         lcount=0;
    13         rcount=0;
    14         while(1)
    15         {
    16             if(a>b)
    17             {
    18                 a=a-b;
    19                 lcount++;
    20             }
    21             if(b>a)
    22             {
    23                 b=b-a;
    24                 rcount++;
    25             }
    26             if(b==1&&a==1)
    27                 break;
    28         }
    29         printf("Scenario #%lld:
    ",i++);
    30         printf("%lld %lld
    
    ",lcount,rcount);
    31     }
    32     return 0;
    33 }
    
    

    很不幸的是交上之后直接时间超限,再看看题 two integers i and j (1 <= i, j <= 2*10 9),都达到了数亿的数量级,显然会造成数亿次的常数

    运算,那么就需要换换思路了,我参考了一下网上大佬们的算法,运用除法来代替减法实现加速运算,这种思路其实在之前的某些题目中

    运用过。

    上代码:

     1 #include<stdio.h>
     2 #include<string.h>
     3 int main()
     4 {
     5     int t;
     6     long long a,b,c,m,n,lcount,rcount,i;
     7     scanf("%d",&t);
     8     i=1;
     9     while(t--)
    10     {
    11         scanf("%lld%lld",&a,&b);
    12         lcount=0;
    13         rcount=0;
    14         while(1)
    15         {
    16             if(a>b)
    17             {
    18                 c=a/b;
    19                 a=a%b;///当b==1时,有可能造成a==0
    20                 if(a==0)///此时需要调成结果
    21                 {
    22                     c--;
    23                     a=1;
    24                 }
    25                 lcount=lcount+c;
    26             }
    27             if(b>a)
    28             {
    29                c=b/a;
    30                b=b%a;
    31                if(b==0)
    32                {
    33                    c--;
    34                    b=1;
    35                }
    36                rcount=rcount+c;
    37 
    38             }
    39             if(b==1&&a==1)
    40                 break;
    41         }
    42         printf("Scenario #%lld:
    ",i++);
    43         printf("%lld %lld
    
    ",lcount,rcount);
    44     }
    45     return 0;
    46 }

    不得不感慨,还是得学习啊。

     
  • 相关阅读:
    事件的解除与绑定
    JavaScript 继承
    left 和 margin-left
    表格 DOM 操作
    基于继承的拖拽
    碰撞运动
    弹性运动
    完美运动框架
    JS 操作 Cookie
    DIV拖拽
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/8671081.html
Copyright © 2020-2023  润新知