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

                                    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

题意:有一个最多装有N个粮食的粮仓,每晚增加M的粮食,有一个最多装有n个粮食的粮仓,每天晚上会增加m的粮食,但是最多不超过n然后第i天早上都会被麻雀吃掉i个粮食。问第几天,这个粮仓会被吃空。(1 ≤ n, m ≤ 1018)。

题解:首先可以找到K的上界和下界,如果M>=N,那么一定是在第N天的时候能吃光,否则晚上都会补满粮仓。在第1-M天是不可能吃光的,因为你吃掉,晚上就是补M个,所以就可以找到一个上界和一个下界,然后二分天数即可。对于每晚补M可以在一开始减去M,然后在最后的天数加上M天即可。否则会爆longlong。

AC代码:

#include<cstdio>
using namespace std;
long long n,m;
int main()
{
    scanf("%I64d%I64d",&n,&m);
    if(m>n)printf("%I64d\n",n);
    else
    {
        n-=m;
        long long l = 0;
        long long r = 2e9;
        long long ans;
        while(l<r)
        {
            long long mid = (l+r)/2;
            if(mid*(mid+1)/2>=n)
            {
                r = mid;
            }
            else
            {
                l = mid + 1;
            }
            ans = r;
        }
        long long k = ans+m;
        printf("%I64d\n",k);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值