练习cf1485A. Add and Divide
题目如下
A. Add and Divide
time limit per test1 second
memory limit per test256 megabytes
You have two positive integers 𝑎 and 𝑏.
You can perform two kinds of operations:
𝑎=⌊𝑎/𝑏⌋ (replace 𝑎 with the integer part of the division between 𝑎 and 𝑏)
𝑏=𝑏+1 (increase 𝑏 by 1)
Find the minimum number of operations required to make 𝑎=0.
Input
The first line contains a single integer 𝑡 (1≤𝑡≤100) — the number of test cases.
The only line of the description of each test case contains two integers 𝑎, 𝑏 (1≤𝑎,𝑏≤109).
Output
For each test case, print a single integer: the minimum number of operations required to make 𝑎=0.
题目大意
通过一下两种操作使得a=0,最少需要几次操作。
操作如下1,a = a / b;2. b = b + 1;
题目分析
要使得a = 0,那么最后一步一定是a = a / b,要满足a = 0,那么只要让最后一步的a < b即可。
若初始b=1时必须先对b进行加一的操作。
b越大时a除的越快,可以通过枚举得到效率最高的除数。
点击查看代码
#include <iostream>
using namespace std;
int main(){
int t; cin >> t;
while(t--){
long long a,b;
cin >> a >> b;
int ans = 1e9;
for(int i = 0; i <= 40; i++){
long long aa = a;
long long bb = b + i;
if(bb == 1) {
continue;
}
int cnt = i;
while(aa >= bb){
aa /= bb;
cnt++;
}
ans = min(ans,cnt);
}
ans++;
cout << ans << "\n";
}
return 0;
}

浙公网安备 33010602011771号