1113. Two fork tree (simple math problem)

Source: Internet
Author: User

Title Description:

As shown above, by positive integer ... Formed a special binary tree. We know that the last node of this binary tree is n. The question now is how many nodes are included in the subtree where node m is located.

For example, n = 12,m = 3 Then the node in the 13,14,15 and the subsequent nodes are not present, the node m is the subtree of the nodes included in the 3,6,7,12, so the node m in the subtree has 4 nodes.

Input:

The input data includes multiple lines, each of which gives a set of test data, including two integers m,n (1 <= m <= n <= 1000000000). The last set of test data includes two 0, which indicates the end of the input, and this set of data is not processed.

Output:

For each set of test data, the output line contains an integer that gives the number of nodes included in the subtree of the node m.

Sample input:
3 12
0 0
Sample output:
4
#include <iostream>using namespacestd;intMain () {intn,m;  while(Cin>>m>>n && m!=0){        intnum=1; if(m==n) {num=1; }        Else {            intleft=2*m; intright=2*m+1;  while(right<N) {num+ = (right-left+1); Left=2*Left ; Right=2*right+1; }            if(n>=Left ) {num+=n-left+1; }} cout<<num<<Endl; }    return 0;}

1113. Two fork tree (simple math problem)

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.