0w1

CFR 689 C. Mike and Chocolate Thieves ( Binary Search )

Problem - C - Codeforces
If the first person took a, then the one who took most would have taken a * k^3.
The restriction is essentially
a * k^3 ≤ n, k > 1, a ≥ 1
If n is fixed, the count of valid ( a, k ) will be
{ \displaystyle
\sum_{k=2}^{\lfloor n^{1/3} \rfloor} \lfloor \frac{n}{k^3} \rfloor
}
And since we want to search whether such n exists for the given count m, where the function with n is non-decreasing, we will apply binary search.

ll get_ways( ll n ){
    ll res = 0;
    for( int i = 2; 1LL * i * i * i <= n; ++i )
        res += n / ( 1LL * i * i * i );
    return res;
}

void solve(){
    ll M; cin >> M;
    ll lb = 1, ub = 1e16, ans = -1;
    while( lb <= ub ){
        ll mid = ( lb + ub ) / 2;
        if( get_ways( mid ) >= M ) ans = mid, ub = mid - 1;
        else lb = mid + 1;
    }
    if( get_ways( ans ) == M )
        cout << ans << "\n";
    else
        cout << -1 << "\n";
}