Add and Divide

Add and Divide Codeforces-1485A

2021.05.02 训练题D
**题目大意:**有两个正整数 a , b ,每次可进行两个操作,1.a/=b,2.b++ ,求需要最少多少步能让a==0
**思路:**由于两次a/b和a/(b+1)的效果是一样的,故只需考虑a除以那个数能够保证除的次数最少即可,可以从2开始循环遍历,暴力求解

题目

You have two positive integers a and b.

You can perform two kinds of operations:

a=⌊ab⌋ (replace a with the integer part of the division between a and b)
b=b+1 (increase b by 1)
Find the minimum number of operations required to make a=0.

Input
The first line contains a single integer t (1≤t≤100) — the number of test cases.

The only line of the description of each test case contains two integers a, b (1≤a,b≤109).

Output
For each test case, print a single integer: the minimum number of operations required to make a=0.

Example
Input
6
9 2
1337 1
1 1
50000000 4
991026972 997
1234 5678
Output
4
9
2
12
3
1
Note
In the first test case, one of the optimal solutions is:

1.Divide a by b. After this operation a=4 and b=2.
2.Divide a by b. After this operation a=2 and b=2.
3.Increase b. After this operation a=2 and b=3.
4.Divide a by b. After this operation a=0 and b=3.

代码实现:

import java.util.Scanner;

public class Add_and_Divide {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t--!=0) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			if(a < b) {
				System.out.println(1);
			}else {
				int temp = a;
				int min = Integer.MAX_VALUE;
				int cnt = 0;
				int mark = 0;
				if(b==1) {
					b++;
					mark = 1;
				}
				for(int i=b;;i++) {
					cnt = i-b+mark;
					while(temp != 0) {
						temp /= i;
						cnt++;
					}
					if(cnt <= min) min = cnt;
					else break;
					cnt = 0;
					temp = a;
				}
				System.out.println(min);
			}
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值