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. datasnap中间件如何控制长连接的客户端连接?

    ActiveConnections: TClientDataSet; ... 有客户端连接上来的时候 procedure TForm8.DSServer1Connect(DSConnectEventO ...

  2. Tomcat+Servlet登录页面实例

    概念   Tomcat server是一个免费的开放源码的Web 应用server,属于轻量级应用server,在中小型系统和并发訪问用户不是非常多的场合下被普遍使用,是开发和调试JSP 程序的首选. ...

  3. HadoopMapReduce运行机制

    1.map方法读取一个文件的一行记录进行分析,  输入:LongWritable(当前读取的文件位置), Text(内容) 2.map将读取到的信息进行分类,输入Context  (键值对)  ;作为 ...

  4. iOS远程推送原理

    远程推送 就是从远程server推送消息给client的通知.当然须要联网. 远程推送服务APNs (Apple Push NotificationServices) 为什么须要远程推送通知? 传统获 ...

  5. Android中AsyncTask使用具体解释

    在Android中我们能够通过Thread+Handler实现多线程通信.一种经典的使用场景是:在新线程中进行耗时操作.当任务完毕后通过Handler向主线程发送Message.这样主线程的Handl ...

  6. bug集合及其解决方法

    点击查看原文 1. java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 27 ...

  7. [转] logback logback.xml常用配置详解(一)<configuration> and <logger>

    转载文章:原文出处:http://aub.iteye.com/blog/1101260 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透彻的理解其配置 根节点< ...

  8. 欧莱雅浅谈OC中方法调用的顺序中的Category

    OC特有的分类Category,依赖于类.它可以在不改变原来的类内容的基础上,为类增加一些方法.分类的使用注意: (1)分类只能增加方法,不能增加成员变量: (2)在分类方法的实现中可以访问原来类中的 ...

  9. HashMap的数据机构是什么样的?

    参考:http://www.cnblogs.com/ITtangtang/p/3948406.html

  10. java Http post请求发送json字符串

    最近差点被业务逻辑搞懵逼,果然要先花时间思考,确定好流程再执行.目前最好用的jar包还是org.apache.http. public class HttpClientHelper { private ...