1485A - Add and Divide - CodeForces Solution


brute force greedy math number theory *1000

Please click on ads to support us..

Python Code:

from cmath import inf
 
def custom_log(a : int, b : int) -> int:
    turn = 0
    if b == 1:
        b = 2
        turn += 1
    while a > 0:
        a //= b
        turn += 1
    return turn
 
for _ in range(
    int(input())
    ):
    a, b = map(int, input().split())
    ans = inf
    for i in range(100):
        ans = min(ans, (custom_log(a, b+i) + i))
    print(ans)

C++ Code:

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define rf(i, a, b) for (ll i = a; i >= b; i--)
typedef std::vector<long long> vi;
#define F first
#define S second
#define fast                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define mod 1000000007
#define PB push_back
#define MP make_pair
#define PI 3.14159265358979323846
#define all(a) a.begin(), a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))
const ll INF = LLONG_MAX / 2;
const ll N = 2e5 + 1;
using namespace std;
int main()
{
    fast;
    ll t = 1;
    std::cin >> t;
    while (t--)
    {
        ll a, b, i, j = 0;
        std::cin >> a >> b;
        if (b > a)
        {
            cout << 1 << "\n";
        }
        else if (a == b)
        {
            cout << 2 << "\n";
        }
        else
        {
            ll ans = mod;
            if (b == 1)
            {
                fr(i, 1, 31)
                {
                    ll co = 0, d = a;
                    while (d != 0)
                    {
                        d = d / (i + b);
                        co++;
                    }
                    ans = min(ans, i + co);
                }
                cout << ans << "\n";
                continue;
            }
            fr(i, 0, 31)
            {
                ll co = 0, d = a;
                while (d != 0)
                {
                    d = d / (i + b);
                    co++;
                }
                ans = min(ans, i + co);
            }
            cout << ans << "\n";
        }
    }
}


Comments

Submit
0 Comments
More Questions

1111A - Superhero Transformation
954A - Diagonal Walking
39F - Pacifist frogs
1451C - String Equality
386A - Second-Price Auction
1690E - Price Maximization
282B - Painting Eggs
440A - Forgotten Episode
233B - Non-square Equation
628B - New Skateboard
262B - Roma and Changing Signs
755C - PolandBall and Forest
456B - Fedya and Maths
376B - IOU
1623B - Game on Ranges
1118A - Water Buying
1462C - Unique Number
301A - Yaroslav and Sequence
38A - Army
38C - Blinds
1197A - DIY Wooden Ladder
1717D - Madoka and The Corruption Scheme
1296D - Fight with Monsters
729D - Sea Battle
788A - Functions again
1245B - Restricted RPS
1490D - Permutation Transformation
1087B - Div Times Mod
1213B - Bad Prices
1726B - Mainak and Interesting Sequence