Loading

Codeforces Round #701 (Div. 2) A. Add and Divide

题目链接

题目大意

给你两个数字\(a\)\(b\),让你执行两种操作:

  • \(a=\lfloor \frac{a}{b}\rfloor\)
  • \(b=(b+1)\)

问你最少需要几次操作,让\(a\)变成\(0\)

题目分析

\(a=10^9,b=1\)的极限情况下,最少操作次数不超过20次,之后再去暴力枚举在20次操作内,经过上述两种操作让\(a=0\)最少需要几次操作。同时,需要注意,当\(b\le1\)的时候是没有什么判断价值的,因为这个时候再去执行\(\lfloor \frac{a}{b}\rfloor\)的次数肯定是最多的。

AC代码

#include <iostream>
#include <algorithm>
using namespace std;

int t, a, b;

inline int f(int A, int B)
{
    int res = 0;
    while (a) { a /= b; res++; }
    return res;
}

int main()
{
    scanf("%d", &t);
    while (t --)
    {
        scanf("%d%d", &a, &b);
        int res = 0x3f3f3f3f;
        for (int i = 0; i < 20; i++)
        {
            if (b + i <= 1) continue;
            res = min(res, f(a, b + i) + i);
        }
        
        cout << res << '\n';
    }
    
    return 0;
}
posted @ 2021-08-17 22:24  Frank_Ou  阅读(68)  评论(0编辑  收藏  举报