Topic 1113: Two fork Trees

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 120 0
Sample output:
4

BFS: Memory Limit exceed
Code:
#include <cstdio>#include<queue>using namespacestd;intMain () {intn,m; Queue<int>Q;  while(SCANF ("%d%d", &n,&m)! =EOF) {        if(n==0&&m==0)             Break;  while(Q.empty () = =false) {q.pop ();        } q.push (n); intans=0;  while(Q.empty () = =false){            intnow=Q.front ();            Q.pop (); ++ans; if(now*2<=m) Q.push ( now*2); if(now*2+1<=m) Q.push ( now*2+1); } printf ("%d\n", ans); }    return 0;} /************************************************************** problem:1113 User:lcyvino language:c++ R Esult:memory Limit exceed****************************************************************/

Use recursion instead: Time Limit exceed

Code:
#include <cstdio>using namespacestd;Long LongCountnode (Long LongXLong Longm) {    if(x>m)return 0; Else{x=x<<1; return 1+countnode (x,m) +countnode (x+1, M); }} intMain () {Long Longn,m;  while(SCANF ("%lld%lld", &n,&m)! =EOF) {        if(n==0&&m==0)             Break; printf ("%lld\n", Countnode (n,m)); }    return 0;} /************************************************************** problem:1113 User:lcyvino language:c++ Re Sult:time Limit exceed****************************************************************/

Had to use the formula:

Code:
#include <cstdio>#include<math.h>using namespacestd;intMain () {intn,m;  while(SCANF ("%d%d", &n,&m)! =EOF) {        if(n==0&&m==0)             Break; intDepth_n= (int) (log2 (n) +1); intDepth_m= (int) (Log2 (m) +1); intsum=0; intnum=1;  for(intindex=0; index<depth_m-depth_n;++index) {Sum+=num; Num*=2; }        Long LongLeftnode=n,rightnode=N;  for(intindex=0; index<depth_m-depth_n;++index) {Leftnode=2*Leftnode; Rightnode=2*rightnode+1; }        if(m>=Rightnode) {Sum+=rightnode-leftnode+1; }Else{            if(m>=leftnode) Sum+=m-leftnode+1; } printf ("%d\n", sum); }    return 0;} /************************************************************** problem:1113 User:lcyvino language:c++ Re sult:accepted time:0 Ms memory:1032 kb****************************************************************/

Topic 1113: Two fork Trees

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.