Codeforces Round #701 (Div. 2) A.Add and Divide【思维】

题意:

给定数字 a、b,可以进行如下两种操作:(1) a = ⌊ a b ⌋ a = \lfloor \frac{a}{b} \rfloor a=ba;(2) b = b + 1 b = b + 1 b=b+1,问最少进行多少次操作才能将 a 变为 0,有 t 组数据。 1 ≤ a , b ≤ 1 0 9 1 \le a,b \le 10^9 1a,b109 1 ≤ t ≤ 100 1\le t \le 100 1t100


思路:

  1. 假定进行 x 次 1 操作,y 次 2 操作,一定是先把 2 操作进行完了之后才进行 1 操作。
  2. 考虑最差的情况,a = 1 0 9 10^9 109, b = 2,这种情况下需要 30 次 1 操作可以把 a 变为 0,也就是说,无论 a、b 的值是什么( b ≠ 1 b ≠ 1 b=1),最多只需要 30 次 1 操作就可以把 a 变为 0。因此,我们可以枚举进行 0 ~ 30 次的 2 操作,对应的算出 1 操作 的次数,通过这样的枚举,就可以找到最小的总的操作次数。

AC Codes:

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
//#include <unordered_set>
//#include <unordered_map>
#include <deque>
#include <list>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <assert.h>
using namespace std;
typedef long long ll;
//cout << fixed << setprecision(k); 输出k位精度
//cout << setw(k); 设置k位宽度

const int N = 2e5 + 6, M = 1e9 + 7, INF = 0x3f3f3f3f;

int main() {
    //freopen("/Users/xumingfei/Desktop/ACM/test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int a, b;
        cin >> a >> b;
        int ans = 2e9;
        for (int i = 0; i <= 30; i++) {
            if (b + i == 1) continue;
            int x = a, now = 0;
            while (x > 0) {
                now++;
                x /= b + i;
            }
            ans = min(ans, now + i);
        }
        cout << ans << '\n';
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值