Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only one pipe with flowing water. Besides, Vova has several splitters.

A splitter is a construction that consists of one input (it can be connected to a water pipe) and x output pipes. When a splitter is connected to a water pipe, water flows from each output pipe. You can assume that the output pipes are ordinary pipes. For example, you can connect water supply to such pipe if there's water flowing out from it. At most one splitter can be connected to any water pipe.

 The figure shows a 4-output splitter

Vova has one splitter of each kind: with 2, 3, 4, ..., k outputs. Help Vova use the minimum number of splitters to build the required pipeline or otherwise state that it's impossible.

Vova needs the pipeline to have exactly n pipes with flowing out water. Note that some of those pipes can be the output pipes of the splitters.

Input

The first line contains two space-separated integers n and k (1 ≤ n ≤ 1018, 2 ≤ k ≤ 109).

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Output

Print a single integer — the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters, print -1.

Examples

Input

4 3

Output

2

Input

5 5

Output

1

Input

8 4

Output

-1

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; long long n,k;
bool f(long long mid) {
long long s;
s=(k+1)*k/2-mid*(mid-1)/2-(k-mid);
if(mid==k) s=mid;
if(s>=n) return true;
else return false;
}
int main() { scanf("%lld%lld",&n,&k);
long long sum=0;
sum=k*(k+1)/2-k+1;
long long ans=k-1;
long long l=2,r=k+1;
long long mid=(l+r)/2;
if(sum<n) {
printf("-1\n");
} else if(n==1) {
printf("0");
} else {
while(mid-l>0) {
//mid=(l+r)/2;
if(f(mid)) {
l=mid;
mid=l+(r-l)/2;
} else {
mid=l+(mid-l)/2;
}
}
long long ans=0;
ans=k+1-mid;
printf("%lld\n",ans);
}
return 0;
}

CodeForces - 287B-Pipeline(二分)的更多相关文章

  1. CodeForces 287B Pipeline

    思路:二分答案,时间复杂度O(nlgn). 若个数为x,那么算出这种情况下提供的水管的最大值和最小值与n比较即可,注意x个分离器需要减去x-1个水管. #include<cstdio> # ...

  2. CodeForces 287B Pipeline (水题)

    B. Pipeline time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  3. codeforces 1165F1/F2 二分好题

    Codeforces 1165F1/F2 二分好题 传送门:https://codeforces.com/contest/1165/problem/F2 题意: 有n种物品,你对于第i个物品,你需要买 ...

  4. codeforces 732D(二分)

    题目链接:http://codeforces.com/contest/732/problem/D 题意:有m门需要过的课程,n天的时间可以选择复习.考试(如果的d[i]为0则只能复习),一门课至少要复 ...

  5. CodeForces 359D (数论+二分+ST算法)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...

  6. CodeForces 163B Lemmings 二分

    Lemmings 题目连接: http://codeforces.com/contest/163/problem/B Descriptionww.co As you know, lemmings li ...

  7. CodeForces - 589A(二分+贪心)

    题目链接:http://codeforces.com/problemset/problem/589/F 题目大意:一位美食家进入宴会厅,厨师为客人提供了n道菜.美食家知道时间表:每个菜肴都将供应. 对 ...

  8. Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  9. CodeForces - 1059D(二分+误差)

    链接:CodeForces - 1059D 题意:给出笛卡尔坐标系上 n 个点,求与 x 轴相切且覆盖了所有给出点的圆的最小半径. 题解:二分半径即可.判断:假设当前二分到的半径是 R ,因为要和 x ...

  10. Letters CodeForces - 978C (二分)

    Time limit4000 ms Memory limit262144 kB There are nn dormitories in Berland State University, they a ...

随机推荐

  1. matlab之plot()函数

    是个画图函数: 语法: figure(1000);hold on;plot(x,y);axis equal; 其中,x和y是某两个长度相同的列向量.比如:x=[1;2;3;4];y=[1;2;3;4] ...

  2. BZOJ 1612 [Usaco2008 Jan]Cow Contest奶牛的比赛:floyd传递闭包

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1612 题意: 有n头牛比赛. 告诉你m组(a,b),表示牛a成绩比牛b高. 保证排名没有并 ...

  3. hdu 4704 sum(费马小定理+快速幂)

    题意: 这题意看了很久.. s(k)表示的是把n分成k个正整数的和,有多少种分法. 例如: n=4时, s(1)=1     4 s(2)=3     1,3      3,1       2,2 s ...

  4. Memcached HA架构探索

    https://code.google.com/p/memagent/ 标签:memcached magent 高可用 HA 架构原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者 ...

  5. 示例的libevent的程序

    著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:auxten 链接:http://zhuanlan.zhihu.com/auxten/20315482 来源:知乎 /* ...

  6. bootstrap框架日期时间之年月选择及汉化

    ==>上次介绍了bootstrap框架日期时间 开始日期和结束日期选择 http://www.cnblogs.com/wjqblogs/p/4043198.html 今天又做了个只选择年月的日期 ...

  7. kettle每天自动发送邮件总结_20161128

    kettle作为java开发的工具,很多功能在目前工作中还用不到,原来它也是支持java代码的,现在用到的也就是它从服务器导数到数据库,然后再进行数据处理的功能. 如何快速学会使用kettle发送邮件 ...

  8. 【LeetCode】312. Burst Balloons

    题目: Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented ...

  9. mysql数据库表分区详解(数量过大的数据库表通过分区提高查询速度)

    这篇文章主要介绍了MySQL的表分区,例如什么是表分区.为什么要对表进行分区.表分区的4种类型详解等,需要的朋友可以参考下 一.什么是表分区通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysq ...

  10. 变废为宝,用旧电脑自己DIY组建 NAS 服务器

    i17986 出品,必属佳作! 前言: 老外不喜欢升级硬件和软件,大家应该都知道.我昨天无意看到 FreeNAS 自述文件,这个系统可以让你使用旧的计算机硬件,于是我决定这么做.垃圾电脑你怎么能没有, ...