我进行了分类讨论大于100000000000000一类,小于100000000000000一类;
#include<bits/stdc++.h> #define ll long long using namespace std; int main(){ int t; scanf("%d",&t); while(t--){ ll k=0,a,b,sum,n,ans=0; scanf("%lld %lld",&a,&b); sum=1,n=0; if(a<100000000000000){ for(int i=0;;i++){ if(k<b){ k=pow(2,i); if(k>b){ k=b; } } else k=b; sum+=k; if(a==1&&b>0){ printf("0\n"); break; } if(a>sum){ n++; } else if(a==sum){ printf("%lld\n",n+1); break; } else if(a<sum){ printf("%lld\n",n+1); break; } } } else { if(a==1) printf("0\n"); else if(b==1) printf("%lld\n",a-1); for(int i=0;a>1&&b>1;i++){ if(k<b){ k=pow(2,i); if(k>b){ k=b; } sum+=k; if(a>sum){ n++; } if(a<=sum){ printf("%lld\n",n+1); break; } } else break; } if(a>sum&&a!=1&&b!=1){ sum=a-sum; ans=sum%b; sum/=b; if(ans==0){ printf("%lld\n",sum+n); } else printf("%lld\n",sum+n+1); } } } return 0; }