二分法应用——求方程的根 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 ofn 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 dayi 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 andm(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



题意:有一个仓库,其容量记为m,每天有i只鸟来吃其中的粮食,i的数量每天加1,但是每天会向仓库中补粮,补粮数记为n.第几天后仓库首次变空?


思
路:首先分析m 与 n 
的关系,若m<=n,则只需m天就可以吃完。若m>n,那么前n天仓库一直是满的,直到n天之后开始变空,且n天以后每天减少的数量为
1,2,3....是一个等差数列,若想吃完,其本质就是求等差数列的前n项和S(n)当n为多少时可以刚好等于m-n;

题目的隐含信息:数据过大不便于直接暴力。

方案一:直接数学求解,计算n 的值,但是此方法涉及对较大数据的计算,过程中会产生误差。故不推荐。(我就是这样做了7.8遍。后来发现不行果断放弃用二分寻找的)
ps:(后来改进了方案一AC了题,)。
方案二:针对上面的问题,既然我们不能直接求解,只能循环寻找解,再考虑到数据的特殊性,果断采用二分查找。

方案一代码:
#include
#include
#include
using namespace std;
int main()
{
     long double k,sum=0,d;
     long long t,a,b;
    cin>>a>>b;
    if(a-b<=0)
    {
        printf("%lld",a);
        return 0;
    }
    else
    {
        k=2*((double)a-b);
    }
    d=4*k+1;
    d=(long double)sqrt(d);
    sum=(d-1)/2;
    t=(long long)sum;/*先对计算结果进行判断,看其是否为整数,如果不是整数,就对其加一。不要对求出来的sum先加b在进行判断其是否为整数,因为进行两个
    if(sum-t>0)        double计算时会产生误差。*/
    {
        t=t+1;
    }
    t+=b;
    printf("%lld",t);
    return 0;
}

方案二代码:

#include
#include
typedef long long  LL;
using namespace std;
int main()
{    LL m,n;    cin>>m>>n;
    if(m<=n)  
  {    
     printf("%lld",m);    
    return 0;    
}    
LL ans,l=1,r=3e9+7;
//右边界开大一点。(比较坑的一点,因为求x(x+1)/2=m-n,m和n最大是10的18次方,因此x的范围小于等于10的9次方)   
 ans=(l+r)/2;   
 while(l=m-n)//判断范围   
     {         
         r=ans;          
         ans=(l+r)>>1;     
     }     
   else      
   if(k==m-n)      
    {      
       break;      
     }      
  else       
   if(k的不等式。     
   {        
      l=ans+1;         
      ans=(l+r)>>1;       
   }    
}    
    printf("%lld",ans+n);    
    return 0;
}


总结; 1.关于查找法解等式,解不等式.
2. 对于计算题注意浮点数的计算不精确性,尽量避免对其进行不必要的计算。

你可能感兴趣的:(二分法应用——求方程的根 Anton and Fairy Tale)