codeforces 785 C. Anton and Fairy Tale (数学 二分搜索)

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的仓库,开始第一天粮食数量为n,从第一天开始每天都会往仓库送数量为m的粮食装入仓库,多余的话不全装入,同时第i天有鸟会吃走数量为i的粮食,如果当天不够吃的话也会吃完,问第几天仓库会为空

思路:如果m>=n,显然在吃完之前的每天仓库都是满的,第n天吃掉数量n的粮食,此时就为空了。

如果m<n,前m天仓库每天一定为满的,因为每天可供加入的粮食比鸟吃走的多或者相等,从第m天开始,有规律:

第m天  ,加入m后共有 n,鸟吃走后剩余 n-m

第m+1天,加入m后共有 n鸟吃走后剩余 n-m-1

第m+2天加入m后共有n-1鸟吃走后剩余 n-1-m-2

第m+3天,加入m后共有n-1-2,鸟吃走后剩余 n-1-2-m-3

........

如果第m+k天最后剩余的数量<=0,即   n-1-2-...-k-m<=0,  =>(1+k)*k/2<=n-m   当找到最小的k时,m+k就是解,二分搜索一下k

注意n m的值很大,r的范围 要使r^2在__int64范围内,因为这个wa了几次了

#include <iostream>
#include<cstdio>
using namespace std;
#define LL __int64
int main()
{
    LL n,m;
    scanf("%I64d%I64d",&n,&m);
    LL ans;
    if(m>=n)
        printf("%I64d\n",n);
    else
    {
        LL sum=n-m;
        LL l=1,r=2000000000,mid;//注意r的最大范围,r^2不能超出__int64 
        while(l<=r)
        {
            mid=(l+r)/2;
            LL num;
            if(mid%2==0)
                num=mid/2*(mid+1);
            else
                num=(mid+1)/2*mid;
            if(num >= (n-m))
            {
                ans=mid;
                r=mid-1;
            }
            else
                l=mid+1;
        }
        printf("%I64d\n",ans+m);
    }

    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值