-
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)