Codeforces Round #404 (Div. 2) C题

  • Post author:
  • Post category:其他


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谷物,最最多几天谷仓会空。

解法:

首先,前m-1天,谷仓都会是满的,第m天吃完后剩下n-m。

接下来,谷物增加一定不会溢出谷仓容量。

所以每天的变化会是这样:

仓库:n-m+1m,n-m+2m, n-m+3m … n-m+km

吃掉: m+1, 2m+1+2, 3m+1+2+3 … km+1+2+3+…+k

所以就是解 n-m>=1+2+…+k=k*(k+1)/2

上述不等式可以通过二分来解决

另外,如果m大于等于n,直接输出n即可。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long

int main()
{
    ll n,m;
    cin>>n>>m;
    if(n<=m){
        cout<<n<<endl;
    }else{
        ll v = n-m;
        ll l=1,r=3000000000;
        ll ans=0;
        while(l<=r){
            ll mid = (l+r)>>(1ll);
            ll t = mid*(mid+1ll)/2ll;
            //找大于等于v的最小的mid
            if(t>=v){
                ans=mid;
                r=mid-1;
            }else{
                l=mid+1;
            }
        }
        cout<<ans+m<<endl;
    }

    return 0;
}



版权声明:本文为zt_1995原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。