Codeforces1485 A. Add and Divide(枚举)

题意:

在这里插入图片描述

解法:

显 然 先 进 行 操 作 2 再 进 行 操 作 1 , 当 b = 2 时 , 答 案 再 l o g 2 ( a ) 左 右 , 因 此 直 接 操 作 2 的 次 数 ( 只 需 要 枚 举 到 l o g 2 ( a ) ) , 然 后 计 算 总 操 作 次 数 , 更 新 答 案 即 可 显然先进行操作2再进行操作1,\\ 当b=2时,答案再log2(a)左右,\\ 因此直接操作2的次数(只需要枚举到log2(a)),\\ 然后计算总操作次数,更新答案即可 21,b=2,log2(a),2(log2(a)),,

code:

#include <bits/stdc++.h>
#define int long long
using namespace std;


void solve(){
    
    
    int a,b;cin>>a>>b;
    int ans=1e9;
    for(int i=0;i<=1e3;i++){
    
    
        int bb=b+i;
        if(bb==1)continue;
        int aa=a;
        int temp=0;
        while(aa){
    
    
            aa/=bb;
            temp++;
        }
        ans=min(ans,temp+i);
    }
    cout<<ans<<endl;
}
signed main(){
    
    
    int T;cin>>T;
    while(T--){
    
    
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44178736/article/details/114055576