Codeforces Burning Midnight Oil

时间:2023-01-17 13:46:45
/*
* BurningMidnightOil.cpp
*
* Created on: 2013-10-12
* Author: wangzhu
*/ /**
* 每次至少写多少行代码ret:
* 1)、当n<=k时,肯定是ret = n;
* 2)、当n > k时,则 ret>=k&ret <= n,故只需要按二分的思路将其查找一下,就可以,
* 对于每一个可能的值进行计算可以书写的代码行,之后继续,最后得到的结果就是答案
*/
#include<cstdio>
#include<iostream>
using namespace std;
#define LL long long
LL calc(int k, int v) {
//数据容易溢出
LL sum = v, kk = k;
while (v / kk) {
sum += v / kk;
kk *= k;
}
return sum;
}
int binarySearch(int n, int k) {
LL left = k, right = n, mid = -;
while (left <= right) {
mid = left + (right - left) / ;
if (n <= calc(k, mid)) {
right = mid - ;
} else {
left = mid + ;
}
}
return (int) left;
}
int main() {
freopen("data.in", "r", stdin);
int n, k;
while (~scanf("%d%d", &n, &k)) {
if(n <= k) {
printf("%d\n",n);
continue;
} printf("%d\n", binarySearch(n, k));
}
return ;
}