您的位置:首页 > 大数据 > 人工智能

二分思路题Anton and Fairy Tale

2017-03-20 10:24 393 查看
题的链接http://codeforces.com/problemset/problem/785/C

                                          

C. Anton and Fairy Tale

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:

"Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained
a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."

More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains
was full. Then, every day (starting with the first day) the following happens:

m grains are brought to the barn. If m grains
doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).

Sparrows come and eat grain. In the i-th day i sparrows
come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.

Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write
a program that will determine the number of that day!

Input

The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) —
the capacity of the barn and the number of grains that are brought every day.

Output

Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.

Examples

input
5 2


output
4


input
8 1


output
5


Note

In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:

At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.

At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.

At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.

At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.

At the beginning of the third day two grains are brought. The barn becomes full again.

At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.

At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.

At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.

So the answer is 4, because by the end of the fourth day the barn becomes empty.
题意:n表示谷仓内能容纳的粮食,m表示每天放入谷仓多少粮食;
    每个第i天都会有i只鸟来吃i颗粮食,然后这天再放入m颗粮食,粮食溢出的不要了(粮食最多有n颗;问多少天能把粮食吃完;

思路:当i<=m时;每天结束后粮食都会被补满(有一种情况除外:即i=n时粮食直接吃完,就直接结束了)  所以当n<=m时  第n天粮食会一次被吃完;
当n>m时;前m天  每天过后粮食被补满(跟没吃一样)
设m+x天吃完了粮食,则(m+1)+(m+2)+'''''''+(m+x)-m*(x-1)>=n;从第m+1天开始到m+x天共吃的减去共补的(x-1)*m>=n表示吃完;
化简得  1+2+''''''+x>=n-m;即(x+1)*x/2>=n-m;
用二分法快速算出满足上式x的最小值。

       为了把l和1区分开,      l用大写L。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
using namespace std;
int main()
{
LL n,m,x,t,L,r;
scanf("%lld%lld",&n,&m);
if(n<=m)
{
printf("%lld",n);
}
else
{
n=n-m;
r=10000000002;
L=1;
while(L!=r)
{
x=(r+L)/2;
t=x*(x+1)/2;
if(t>=n)
r=x;
if(t<n)
L=x+1;
}
printf("%lld\n",m+L);
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息