1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream> #include <cstdio> #define ul unsigned long long using namespace std; ul twosPower[60]; ul binSearch(int Left) { int lw=0,hi=55; int mid; while(hi-lw>0) { mid = (lw+hi)/2; if(Left<twosPower[mid]) { hi=mid; } else if(Left>=twosPower[mid]) { lw=mid+1; } } return twosPower[lw-1]; } int recursion(ul Left,ul Right) { //<<"inside recur "<<Left<<" "<<Right<<endl; ul LeftNumberFloor; if(Left==0) return 0; else if(Left==Right) return Left; else if(Left==1 && Right==2) return 0; else { LeftNumberFloor = binSearch(Left); if(Right>LeftNumberFloor && Right<(2*LeftNumberFloor))//they are in the same base { return LeftNumberFloor + recursion(Left-LeftNumberFloor,Right-LeftNumberFloor); } else if(Right==LeftNumberFloor) return LeftNumberFloor+0; else return 0; } return 0; } int main() { int t; ul lt,rt; twosPower[0]=1; twosPower[1]=2; for(int i=2;i<60;i++) twosPower[i]=twosPower[i-1]*2; cin>>t; while(t--) { cin>>lt>>rt; cout<<recursion(lt,rt)<<endl; } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream> #include <cstdio> #define ul unsigned long long using namespace std; ul twosPower[60]; ul binSearch(int Left) { int lw=0,hi=55; int mid; while(hi-lw>0) { mid = (lw+hi)/2; if(Left<twosPower[mid]) { hi=mid; } else if(Left>=twosPower[mid]) { lw=mid+1; } } return twosPower[lw-1]; } int recursion(ul Left,ul Right) { //<<"inside recur "<<Left<<" "<<Right<<endl; ul LeftNumberFloor; if(Left==0) return 0; else if(Left==Right) return Left; else if(Left==1 && Right==2) return 0; else { LeftNumberFloor = binSearch(Left); if(Right>LeftNumberFloor && Right<(2*LeftNumberFloor))//they are in the same base { return LeftNumberFloor + recursion(Left-LeftNumberFloor,Right-LeftNumberFloor); } else if(Right==LeftNumberFloor) return LeftNumberFloor+0; else return 0; } return 0; } int main() { int t; ul lt,rt; twosPower[0]=1; twosPower[1]=2; for(int i=2;i<60;i++) twosPower[i]=twosPower[i-1]*2; cin>>t; while(t--) { cin>>lt>>rt; cout<<recursion(lt,rt)<<endl; } return 0; } |