Topic 1113: Binary Tree

Source: Internet
Author: User

Time limit:1 second

Memory limit:32 MB

Special Judgment:No

Submit:3332

Solution:983

Description:

 

 

 


As shown above, it is composed of positive integers 1, 2, 3 ...... A special binary tree. We know that the last node of this binary tree is N. The problem is how many nodes are in the subtree where node m is located.

For example, if n = 12, M = 3, the nodes 13, 14, 15 and the following do not exist. the nodes in the subtree where node m is located include 3, 6, 7, 12, therefore, the subtree where node m is located has four nodes.

Input:

The input data includes multiple rows. Each row provides a set of test data, including two integers, M and N (1 <= m <= n <= 1000000000 ). The last group of test data contains two zeros, indicating the end of the input, which does not need to be processed.

Output:

For each group of test data, output a row. The row contains an integer that shows the number of nodes in the Child tree where node m is located.

Sample input:
3 120 0
Sample output:
4
Source:
Question about the vitality of computer research at Peking University in 2007
The problem is not difficult, but the recursion times out. You can calculate the number of layers and use the formula to calculate the number of points.
There is a recursion problem, that is, after each group of data is complete, because time is a global variable, so it will not be set to zero next time. If it is cleared in recursion, it will also kneel down. Therefore, after calling Main (), time = 0. This is acceptable because of the time global variable.
Recursive code:
 1 #include <stdio.h> 2 int time=0; 3 int timecount(int m,int n) 4 {     5     if(m<=n) 6         time++;     7     else 8         return 0; 9     timecount(2*m,n);    10     timecount(2*m+1,n);11     12     return time;13 }14 int main(void)15 {16     int m,n,t;17     18     while(scanf("%d %d",&m,&n)!=EOF&&m!=0)19     {    20         t=timecount(m,n);21         time=0;22         printf("%d\n",t);23     }24     return 0;25 }

 

Topic 1113: Binary Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.