Codeforces Burning Midnight Oil

/*

 * 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 = -1;

    while (left <= right) {

        mid = left + (right - left) / 2;

        if (n <= calc(k, mid)) {

            right = mid - 1;

        } else {

            left = mid + 1;

        }

    }

    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 0;

}

 

你可能感兴趣的:(codeforces)