音量减的时候,分两种,一种是减到大于目标M,另一种是减到小于M,停顿的时候可以减少最后往上加的次数,小于0的时候变成0
然后比一下这两种的最小值。
1 /*--------------------------------------------------------------------------------------*/ 2 #include <algorithm> 3 #include <iostream> 4 #include <cstring> 5 #include <ctype.h> 6 #include <cstdlib> 7 #include <cstdio> 8 #include <vector> 9 #include <string> 10 #include <queue> 11 #include <stack> 12 #include <cmath> 13 #include <set> 14 #include <map> 15 16 //debug function for a N*M array 17 #define debug_map(N,M,G) printf(" ");for(int i=0;i<(N);i++) 18 {for(int j=0;j<(M);j++){ 19 printf("%d",G[i][j]);}printf(" ");} 20 //debug function for int,float,double,etc. 21 #define debug_var(X) cout<<#X"="<<X<<endl; 22 #define LL long long 23 const int INF = 0x3f3f3f3f; 24 const int LLINF = 0x3f3f3f3f3f3f3f3f; 25 /*--------------------------------------------------------------------------------------*/ 26 using namespace std; 27 28 LL N,M,ans; 29 int T; 30 31 LL sum(int x) {return (1LL<<x) - 1LL;} 32 33 LL dfs(LL cur,LL cnt,int stop) 34 { 35 //printf("cur:%d cnt:%d ",cur,cnt); 36 if(cur == M) return cnt; 37 int i = 0; 38 while(cur-sum(i) > M) i++; 39 40 if(cur -sum(i) == M ) return cnt + i; 41 LL up = M - max(0LL,cur-sum(i) ); 42 LL res = i + max(0LL,up-stop); 43 return min(cnt+res , dfs(cur-sum(i-1) , cnt + i,stop+1)); 44 } 45 46 int main() 47 { 48 scanf("%d",&T); 49 while(T--) 50 { 51 ans = LLINF; 52 scanf("%I64d%I64d",&N,&M); 53 if(N <= M) 54 { 55 printf("%I64d ",M-N); 56 continue; 57 } 58 59 printf("%I64d ",dfs(N,0,0)); 60 } 61 }