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.
思路:对于m>=n的情况,前n-1天肯定都是满的,第n天来n只小鸟后就变成0了。
对于m<n的情况,第m天之前肯定都是满的,从第m+1天开始,假设x天吃完,那么小鸟吃了(m+1+m+x)*x/2粒谷子,一共增加了x *m粒谷子,之前还剩下n-m粒谷子,这样也就是(m+1+m+x)*x/2>=n-m+x *m.这是一个开口向上的二次函数,但是呢我们发现他的对称轴是在x轴左边的,也就是说,在正数范围内,是单调递增的。这样的话,就可以二分来做了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;ll n,m;int main()
{scanf("%lld%lld",&n,&m);if(m>=n) cout<<n<<endl;else{ll l=1,r=1e10,mid;ll ans=0;while(l<=r){mid=l+r>>1ll;if(mid*mid+mid-2ll*n+2ll*m<0) l=mid+1;else ans=mid,r=mid-1;}cout<<ans+m<<endl;}return 0;
}

努力加油a啊,(o)/~

Anton and Fairy Tale CodeForces - 785C(二分+思维)相关推荐

  1. Anton and Fairy Tale

    Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right ...

  2. CodeForces 785C Anton and Fairy Tale 二分

    题意: 有一个谷仓容量为\(n\),谷仓第一天是满的,然后每天都发生这两件事: 往谷仓中放\(m\)个谷子,多出来的忽略掉 第\(i\)天来\(i\)只麻雀,吃掉\(i\)个谷子 求多少天后谷仓会空 ...

  3. Anton and Fairy Tale(二分)

    去过很多西方国家的Hxx1喜欢给我们讲他的旅行见闻 有一天他讲了他与外国人谈笑风生时听到的故事 "很久很久以前,有一位国王突发奇想,想建一座大谷仓. 将所有的谷子都存在这个谷仓里.工匠们干了 ...

  4. C. Anton and Fairy Tale

    链接 [https://codeforces.com/contest/785/problem/C] 题意 初始时有n,第1天先加m开始吃1,但总的不能超过n,第i天先加m开始吃i(如果不够或刚好就吃完 ...

  5. codeforce Anton and Fairy Tale

    题意:有一个谷仓,它的容量为N.第i天先会增加M(不会超出它的容量),然后减少i, 问第几天后谷仓为空,且第一天谷仓是满的. 第i天 增加 减少 1 0 1 2 1 2 3 2 3 -- -- -- ...

  6. Fairy tale(BFS + 大模拟)

    一.题目链接: Fairy tale 二.题目大意: 给你一个N × N 的地图,图上的每个点有四种方向(E W S N),代表着移动方向. 在 t = 0 时,saya 在 (1, 1),treas ...

  7. 动漫推荐之 Fairy Tale 妖精的尾巴

    永远的Fairy Tale!

  8. Fairy Tale (《传奇》英文版)

    这首<Fairy Tale>的旋律应该是中国歌迷再熟悉不过的了,这是迈克学摇滚在2010年的中国巡演中给大家带来的惊喜.填上英文歌词后的<传奇>会有怎样的意境呢?我们一起来静静 ...

  9. Fairy Tale - 英文版《传奇》迈克学摇滚

    向大家推荐一首好听的歌,可以听听试试看 [url]http://www.tudou.com/programs/view/sxJ8uTsEp8A/[/url] 没有MV,只有音乐 Fairy Tale/ ...

最新文章

  1. 相对路径和绝对路径错误造成的漏洞
  2. image.helper.php,image.php
  3. delphichromiumembedded
  4. Chrome 控制台console的用法
  5. apache服务器_Apache的简介与配置(上)
  6. 总结一下矩阵的基本操作
  7. Java 堆内存是线程共享的!面试官:你确定吗?
  8. 周志华-机器学习西瓜书-第三章习题3.3 编程实现对率回归
  9. excel中的颜色代码(colorIndex)
  10. 和风天气OUC——通过搜索城市快速查询天气
  11. linux下添加三菱触摸屏usb驱动,华杰智控带USB口PLC、触摸屏实现远程上下载
  12. 利用谷歌浏览器模拟网速慢的情况
  13. c语言if函数嵌套公式例子,IF函数的嵌套使用案例
  14. 关键词优化推广需要怎么做?有哪些方法和技巧
  15. The server time zone value ‘� й ��� ׼ʱ ��‘ is unrecognized or represents more than one time zone.
  16. LoRa节点开发:5、代码详解LoRaWAN中的几种数据包(发送与接收数据)
  17. 众筹之家9月股权众筹行业简报
  18. (HDOJ)Vowel Counting-Java实现
  19. Pintos project2 实验报告
  20. Java媒体框架(JMF),个人很欣赏.... (转)

热门文章

  1. IOS开发基础之OC的Block入门_Day09-Block
  2. 天正建筑lisp编程接口_编程思想|面向过程的结构化、面向对象的抽象化、泛型编程...
  3. (个人总结)Linux命令——任意目录查看穿越
  4. foreach jdk8 递归_[Java 8] (8) Lambda表达式对递归的优化(上) - 使用尾递归 .
  5. 虚拟机vmnet8每次都要先禁用再启用_【零成本 amp; 超详细】使用Win10自带的Hyper-V管理工具搭建虚拟机...
  6. java打印整个向量_Java中Vector向量的用法
  7. virtualbox php mac,mac一体机通过Oracle VM VirtualBox装win8.1系统
  8. java颜色gui_Java gui颜色不加载
  9. php 小数点 乘法,js小数点数字相乘、把小数点四舍五入保留两位小数
  10. 洛谷 P3853 [TJOI2007]路标设置