Anton and Fairy Tale

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.

Example
Input
5 2
Output
4
Input
8 1
Output
5


题意:有一个谷仓,现在是装满了n粒谷子,但是每天都会有鸟来,每一只鸟每天吃一粒谷子,吃完谷子费走后谷仓每天都会增加k粒谷子,但是谷仓满了就装不下了。每天都会比前一天多一只鸟来吃谷子,问,在那一天,谷仓里面的谷子会被吃空?

由题意可知,在m天之前,谷仓是肯定是满的,因为在m天之前,减少的谷数少于每天补充的谷数,在m天之后,谷仓才开始减少.当m+1天的时候,有一只小鸟可以吃到谷仓里的谷,有m只小鸟是吃补充的谷粒.当m+2天的时候,有两只小鸟可以吃到谷仓里的谷,有m只小鸟是吃补充的谷粒.,相当于第m天之后,谷仓是没有补充的,而小鸟的个数是从1开始增加.小鸟的个数是递增的,递增数列之和为:(i+1)*i/2,而第m天之后,谷仓里谷粒数为n-m,所以m天之后,当小鸟吃谷仓里的谷的总数量>=(n-m),就是答案

#include <stdio.h>
#include <math.h>
int main()
{
    long long x,y,z,t,n,m,num;
    scanf("%lld%lld",&n,&m);
    if(n<=m)
    {
        printf("%lld",n);
    }
    else
    {
        t = n-m; 
        long long l = 1;
        long long r = 2e9; 
        long long mid;
        while(l<r)
        {
            mid = (l+r)/2;
            if((mid+1)*mid/2>=t)
            r=mid;
            else
            l=mid+1;
        }
        printf("%lld\n",l+m);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qecode/article/details/78545245