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****************************************************************/