C. Anton and Fairy Tale----思维+二分答案

时间:2022-05-31 15:53:05

C. Anton and Fairy Taletime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard 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.

Examplesinput
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.


题目链接:http://codeforces.com/contest/785/problem/C



题目的意思是给你n和m,n代表谷仓中有多少粒谷物,m代表每天往里补充多少,麻雀从1只开始来,第一天一只,第二天两只.....问谷仓第一次空是在第几天,典型的二分答案,然而我整了一个小时没有整好,总是差1,然后强行把这个题转了思维题,过了,但是我手残写成了1e9,二分的时候写的2e9,思维的时候手残,还是码力不足啊,二分天数,做就好,不难。


二分代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long
using namespace std;
int main(){
LL n,m;
scanf("%I64d%I64d",&n,&m);
LL ll=m+1,rr=m+2e9;
while(ll<rr){
LL mid=(ll+rr)>>1;
LL val=mid-m;
LL ans=m;
ans+=(val*(val+1LL))/2LL;
if(ans<n){
ll=mid+1;
}
else{
rr=mid;
}
}
LL cnt=min(ll,n);
cout<<cnt<<endl;
return 0;
}

思维代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#define LL long long
using namespace std;
int main(){
LL n,m;
scanf("%I64d%I64d",&n,&m);
LL sum=m;
LL i;
if(n<=m){
return 0*printf("%I64d\n",n);
}
n=n-m;
n=max(0LL,n);
for(i=2e9;i>=0;i-=10000){//手残毁一生
if((1+i)*i/2<n)
break;
}
LL j;
for(j=max(i,0LL);j<=i+10000;j++){
if((1+j)*j/2>=n){
break;
}
}
printf("%I64d\n",sum+j);
return 0;
}