One day a highly important task was commissioned to Vasya — writing a program in a night. The program consists of n lines of code. Vasya is already exhausted, so he works like that: first he writes v lines of code, drinks a cup of tea, then he writes as much as  lines, drinks another cup of tea, then he writes  lines and so on: , ...

The expression  is regarded as the integral part from dividing number a by number b.

The moment the current value  equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.

Vasya is wondering, what minimum allowable value v can take to let him write not less than n lines of code before he falls asleep.

Input

The input consists of two integers n and k, separated by spaces — the size of the program in lines and the productivity reduction coefficient, 1 ≤ n ≤ 109, 2 ≤ k ≤ 10.

Output

Print the only integer — the minimum value of v that lets Vasya write the program in one night.

Examples

Input
7 2
Output
4
Input
59 9
Output
54

Note

In the first sample the answer is v = 4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4 + 2 + 1 = 7 lines in a night and complete the task.

In the second sample the answer is v = 54. Vasya writes the code in the following portions: 54, 6. The total sum is 54 + 6 = 60, that's even more than n = 59.

题解:找到一个合适的值V,

【一天,一个非常重要的任务被委托给Vasya--在一个晚上写一个程序。该程序由n行代码组成。Vasya已经耗尽,所以,他的作品就像是:第一,他写道v行代码,喝一杯茶,然后他尽可能多写为线,饮料一杯茶,然后他写道线等:, ...

该表达式被视为从数字a除以数字b组成的组成部分。

当前值等于0 的那一刻,Vasya立即睡着了,他只在早上才醒来,当时程序应该已经完成​​。

Vasya很纳闷,有什么最小允许值v可以让他写不低于比ň行代码,他睡着了。】

【题析】:需要用二分查找

 #include<bits/stdc++.h>
using namespace std; int main() {
long long n;
int m;
scanf("%lld %d",&n,&m);
long long v;
int flag = ;
long long vv;
long long mid;
long long left = ;
long long right = n;
long long ans = n;
while(left <= right) {
mid = (left + right) / ;
//printf("%lld %lld %lld\n",left,right,mid);
long long sum = mid;
int count = ;
long long v = m;
int vv = mid/v;
while(vv){
sum += vv;
count++;
v = v*m;
vv = mid/v;
}
if(flag == ) break;
if(sum >= n) { ans = min(ans,mid);
right = mid - ;
ans = min(ans,mid);
} else {
left = mid + ;
}
}
printf("%lld\n",ans);
return ;
}

B - Burning Midnight Oil CodeForces - 165B的更多相关文章

  1. Codeforces Burning Midnight Oil

    /* * BurningMidnightOil.cpp * * Created on: 2013-10-12 * Author: wangzhu */ /** * 每次至少写多少行代码ret: * 1 ...

  2. 2016.09.14,英语,《Using English at Work》全书笔记

    半个月时间,听完了ESLPod出品的<Using English at Work>,笔记和自己听的时候的备注列在下面.准备把每个语音里的快速阅读部分截取出来,放在手机里反复听. 下一阶段把 ...

  3. English idioms

    a hot potato : speak of an issue(mostly current) which many people are talking about and which is us ...

  4. 英语口语练习系列-C14-常用片语

    句子 1. Some ads are extremely persuasive and we find we buy products we don't really need. 有一些广告非常有说服 ...

  5. CSU训练分类

    √√第一部分 基础算法(#10023 除外) 第 1 章 贪心算法 √√#10000 「一本通 1.1 例 1」活动安排 √√#10001 「一本通 1.1 例 2」种树 √√#10002 「一本通 ...

  6. CodeForces 508C Anya and Ghosts

     Anya and Ghosts Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  7. Codeforces Round #288 (Div. 2) C. Anya and Ghosts 模拟

    C. Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. Codeforces 767B. The Queue 模拟题

    B. The Queue time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  9. Codeforces Beta Round #14 (Div. 2) A. Letter 水题

    A. Letter 题目连接: http://www.codeforces.com/contest/14/problem/A Description A boy Bob likes to draw. ...

随机推荐

  1. 【c++】动态内存

    静态存储区:内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.它主要存放静态数据.全局数据和常量.注意:const常量在定义时必须初始化 栈区:在执行函数时,函数内局部变量的存储单 ...

  2. C#中list比数组效率低多少

    对于List,即长度不确定的数组而言,十万笔数据*12倍,就是120万笔数据,只需要93ms左右   换成了二维数组,效果也是差不多,78ms,可见list的效率只比double差一点点  

  3. Python - 多次检查后缀名(endwith)

    在通过后缀名查找类型文件的时候, 多次使用endwith, 使用元组(tuple), 简化操作. 此类方式, 也能够应用于if语句多次类似检測. 代码 # 列出目录内全部代码 def list_dic ...

  4. Angular2.x-服务

    heroes之旅HeroesComponent目前正在获取并显示虚假数据. 在本教程重构之后,HeroesComponent将会精益求精并专注于支持视图.用模拟服务进行单元测试也会更容易. 为什么服务 ...

  5. JS地区四级级联

    <script type="text/javascript" src="../js/jsAddress.js"></script> &l ...

  6. LoadRunner关联需要转义的常见字符

    转义字符总结 在做手动关联时,取边界值的时候,会经常用到转义字符,现将转义字符整理如下: \b 退格             \f 换页             \n 换行             \ ...

  7. 程序C++ to C#交互

    第一次用C#调用C/C++生成的DLL文件,感觉有点新鲜,事实上仅仅是实现了执行在公共语言执行库 (CLR) 的控制之外的"非托管代码"(执行在公共语言执行库(CLR)的控制之中的 ...

  8. AIX下RAC搭建 Oracle10G(五)安装oracle、建立监听

    AIX下RAC搭建系列 AIX下RAC搭建 Oracle10G(五)安装oracle.建立监听 环境 节点 节点1 节点2 小机型号 IBM P-series 630 IBM P-series 630 ...

  9. 网络知识: 物理层PHY 和 网络层MAC

    PHY模块简介 物理层位于OSI最底层,物理层协议定义电气信号.线的状态.时钟要求.数据编码和数据传输用的连接器. 物理层的器件称为PHY. 上图里的灰色方框图里的就是PHY芯片内部模块图. MAC器 ...

  10. iOS 用xib自定义View

      网上有很多关于实现用xib自定义View,那我为什么还要写呢?第一,我用他们的方法都没有实现.第二,用xib遇到了很多问题,想分享给大家.    用xib自定义View:FHCustomView ...