➤ Problem Link : 165B. Burning Midnight Oil
✅ C++ Solution :
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
bool func(ll v,ll n,ll k)
{
ll p = (floor)((double)log(v)/log(k));
double d = pow(k,p+1);
d=1/d;
d-=1;
d/=(1-k);
d*=(v*k);
if(d>=n)
return true;
return false;
}
bool check(ll x,ll n,ll k)
{
ll sum=x;
ll t=k;
while(k<=x)
{
sum+=x/k;
k*=t;
}
if(sum>=n)
return true;
return false;
}
int main()
{
ll n,k;
cin>>n>>k;
if(n<k)
{
cout<<n;
exit(0);
}
ll low,mid,high;
low=0;
high=1000000000;
while(low<high)
{
mid=low+(high-low)/2;
if(func(mid,n,k))
high=mid;
else
low=mid+1;
}
for(ll x=low;;x++)
{
if(check(x,n,k))
{
cout<<x;
break;
}
}
}
Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook.
😇Happy Learning!!
