Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 二分

时间:2021-07-21 18:37:04

C. Anton and Fairy Tale

题目连接:

http://codeforces.com/contest/785/problem/C

Description

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.

Sample Input

5 2

Sample Output

4

Hint

题意

有一个最多装有n个粮食的粮仓,每天晚上会增加m的粮食,但是最多不超过n

然后第i天早上都会被麻雀吃掉i个粮食。

问你第几天,这个粮仓会被吃空。

题解:

正面做比较难,于是我们就转化为判定性问题,二分来做。

首先显然前min(n,m)天是没用的,因为你吃了,晚上就会补回去。

那么从min(n,m)天开始,粮仓的上限也是没有用的了,因为你并不能补满。

然后我们就二分天数去做就好了,判断吃的,是否能够超过他补充的。

但是直接做会爆longlong,你得处理一下。

这里我们一开始都减去m,这样就没有超过longlong了。

代码

#include<bits/stdc++.h>
using namespace std;

int main(){
    long long n,m;
    cin>>n>>m;
    if(m>=n){
        cout<<n<<endl;
        return 0;
    }
    long long Ans = 0;
    long long l = 0,r = 2e9;
    n-=m;
    while(l<=r){
        long long mid = (l+r)/2;
        if(mid*(mid+1)/2<n){
            l=mid+1;
        }else{
            r=mid-1;
            Ans=mid;
        }
    }
    cout<<Ans+m<<endl;
}