buptoj 1578
题目链接:http://acm.bupt.edu.cn/onlinejudge/newoj/showProblem/show_problem.php?problem_id=1578
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <iostream> 5 #include <cstring> 6 #include <queue> 7 #include <vector> 8 #include <bitset> 9 10 using namespace std; 11 int a,b; 12 int calculatenum(int num){ 13 int ans = 0; 14 while(num>0){ 15 num = num/2; 16 ans++; 17 } 18 return ans; 19 } 20 int calculateone(int num){ 21 int ans = 0; 22 while(num>0){ 23 if(num%2) ans++; 24 num = num/2; 25 } 26 return ans; 27 } 28 29 int main(){ 30 // if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);} 31 32 int T; 33 cin>>T; 34 while(T--){ 35 36 cin>>a>>b; 37 38 int numa = calculatenum(a); 39 int numb = calculatenum(b); 40 41 if(a == b){ 42 printf("%d\n",calculateone(b)); 43 } 44 else if(b == (1<<numb )- 1){ 45 printf("%d\n",numb); 46 } 47 else if(numa < numb) { 48 printf("%d\n",numb-1); 49 } 50 else{ 51 int maxnum = calculateone(b); 52 int temp = b; 53 numa--; 54 while(a>>numa == b>>numa){ 55 temp = temp%(1<<numa); 56 numa--; 57 } 58 numa++; 59 if(temp != (1<<numa)-1){ 60 maxnum = calculateone(b / (1<<numa)) + calculatenum(temp) - 1; 61 } 62 printf("%d\n",maxnum); 63 } 64 65 } 66 }