zoukankan      html  css  js  c++  java
  • Full Binary Tree(sdut 2882)

    Problem Description:

    In computer science, a binary tree is a tree data structure in which each node has at most two children. Consider an infinite full binary tree (each node has two children except the leaf nodes) defined as follows. For a node labelled v its left child will be labelled 2 * v and its right child will be labelled 2 * v + 1. The root is labelled as 1.
     
    You are given n queries of the form i, j. For each query, you have to print the length of the shortest path between node labelled i and node labelled j.
     Input
    First line contains n(1 ≤ n ≤ 10^5), the number of queries. Each query consists of two space separated integers i and j(1 ≤ i, j ≤ 10^9) in one line.

    Output

    For each query, print the required answer in one line.
     

    Sample Input

    5
    1 2
    2 3
    4 3
    1024 2048
    3214567 9998877
    题解 只要找到公共节点即可,水题一个;
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        long long int i,j,a,b,m,n,p;
        scanf("%lld",&p);
        for(int k=0;k<p;k++)
        {
            scanf("%lld%lld",&a,&b);
            m=a>b?a:b;
            n=a>b?b:a;
            long long int count1=0,count2=0;
            while(m!=n)
            {
                if(n>m)
                    n/=2;
                if(m==n)
                    break;
                if(m>n)
                m/=2;
            }//找到公共节点;
            while(a!=m)
            {
                a/=2;
                count1++;
            }
            while(b!=m)
            {
                b/=2;
                count1++;
            }//统计下每一节点到公共节点的距离
            printf("%lld
    ",count1);
    
        }
        return 0;
    }
  • 相关阅读:
    我最讨厌画图,这辈子我都不想再画图
    bzoj1218[HNOI2003]激光炸弹
    bzoj1196[HNOI2006]公路修建问题
    bzoj1588[HNOI2002]营业额统计
    bzoj2039[2009国家集训队]employ人员雇佣
    bzoj3874[Ahoi2014]宅男计划
    bzoj2282[Sdoi2011]消防
    bzoj1798[Ahoi2009]Seq 维护序列seq
    bzoj4003[JLOI2015]城池攻占
    bzoj2809[Apio2012]dispatching
  • 原文地址:https://www.cnblogs.com/moomcake/p/8627551.html
Copyright © 2011-2022 走看看