• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

CodeforcesRound#404(Div.2)C.AntonandFairyTale二分

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

题目连接:

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;
}

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
快速福利叶变换C函数发布时间:2022-07-14
下一篇:
C#显示excel的最大数据行数发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap