首页 > 其他 > 详细

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

时间:2021-08-18 10:12:59      阅读:22      评论:0      收藏:0      [点我收藏+]

题目链接

题目大意

给你两个数字\(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;
}

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

原文:https://www.cnblogs.com/FrankOu/p/15154576.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!