C. Anton and Fairy Tale

题目连接:

http://codeforces.com/contest/785/problem/C

Description

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.

Sample Input

5 2

Sample Output

4

Hint

题意

有一个最多装有n个粮食的粮仓,每天晚上会增加m的粮食,但是最多不超过n

然后第i天早上都会被麻雀吃掉i个粮食。

问你第几天,这个粮仓会被吃空。

题解:

正面做比较难,于是我们就转化为判定性问题,二分来做。

首先显然前min(n,m)天是没用的,因为你吃了,晚上就会补回去。

那么从min(n,m)天开始,粮仓的上限也是没有用的了,因为你并不能补满。

然后我们就二分天数去做就好了,判断吃的,是否能够超过他补充的。

但是直接做会爆longlong,你得处理一下。

这里我们一开始都减去m,这样就没有超过longlong了。

代码

#include<bits/stdc++.h>
using namespace std; int main(){
long long n,m;
cin>>n>>m;
if(m>=n){
cout<<n<<endl;
return 0;
}
long long Ans = 0;
long long l = 0,r = 2e9;
n-=m;
while(l<=r){
long long mid = (l+r)/2;
if(mid*(mid+1)/2<n){
l=mid+1;
}else{
r=mid-1;
Ans=mid;
}
}
cout<<Ans+m<<endl;
}

Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale 二分的更多相关文章

  1. 【二分】Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale

    当m>=n时,显然答案是n: 若m<n,在第m天之后,每天粮仓减少的量会形成等差数列,只需要二分到底在第几天,粮仓第一次下降到0即可. 若直接解不等式,可能会有误差,需要在答案旁边扫一下. ...

  2. Codeforces Round #404 (Div. 2) D. Anton and School - 2 数学

    D. Anton and School - 2 题目连接: http://codeforces.com/contest/785/problem/D Description As you probabl ...

  3. Codeforces Round #404 (Div. 2) B. Anton and Classes 水题

    B. Anton and Classes 题目连接: http://codeforces.com/contest/785/problem/B Description Anton likes to pl ...

  4. Codeforces Round #404 (Div. 2) A - Anton and Polyhedrons 水题

    A - Anton and Polyhedrons 题目连接: http://codeforces.com/contest/785/problem/A Description Anton's favo ...

  5. 【组合数】【乘法逆元】 Codeforces Round #404 (Div. 2) D. Anton and School - 2

    http://codeforces.com/blog/entry/50996 官方题解讲得很明白,在这里我复述一下. 枚举每个左括号,考虑计算一定包含其的简单括号序列的个数,只考虑其及其左侧的左括号, ...

  6. Codeforces Round #404 (Div. 2) E. Anton and Permutation(树状数组套主席树 求出指定数的排名)

    E. Anton and Permutation time limit per test 4 seconds memory limit per test 512 megabytes input sta ...

  7. Codeforces Round #404 (Div. 2) D. Anton and School - 2

    题目链接 转自 给你一个字符串问你能构造多少RSBS. #include<bits/stdc++.h> #define LL long long #define fi first #def ...

  8. Codeforces Round #379 (Div. 2) C. Anton and Making Potions —— 二分

    题目链接:http://codeforces.com/contest/734/problem/C C. Anton and Making Potions time limit per test 4 s ...

  9. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 二分

    C. Anton and Making Potions time limit per test 4 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. 了解Linux操作系统的引导过程

    原文地址:http://os.51cto.com/art/200706/49690.htm 1.简介 Linux启动过程指的是从加电到看到shell提示的这一段时间. Linux启动的过程可以大概分为 ...

  2. 总有一些实用javascript的元素被人遗忘在角落-slice

    slice() 方法可从已有的数组中返回选定的元素. 好吧,我承认我竟然把它忘了! 这次我在回顾一下它 语法 arrayObject.slice(start,end) 数组.slice(起始,结束) ...

  3. [转载]Markdown——入门指南

    http://www.jianshu.com/p/1e402922ee32/ 转载请注明原作者,如果你觉得这篇文章对你有帮助或启发,也可以来请我喝咖啡. 导语: Markdown 是一种轻量级的「标记 ...

  4. iOS常用小功能

    CHENYILONG Blog 常用小功能 技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong  ...

  5. HDU 1869 六度分离 最短路

    解题报告: 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象(small world phenomenon)”的著名假说,大意是说,任何2个素不相识的人中间最多只隔着6个人, ...

  6. js基础知识:闭包,事件处理,原型

    闭包:其实就是js代码在执行的时候会创建变量对象的一个作用域链,标识符解析的时候会沿着作用域链一级一级的网上搜索,最后到达全局变量停止.所以某个函数可以访问外层的局部变量和全局变量,但是访问不了里层的 ...

  7. 360 / 小米 / 百度 随身wifi Ubuntu 下作为无线网卡使用

    这篇文章说得其实很好了,http://www.freemindworld.com/blog/2013/131010_360_wifi_in_linux.shtml 不过因为专利问题,官网貌似不直接提供 ...

  8. vb 中recordset提示对象关闭时不允许操作

    vb中执行查询后,一般要判断是否为空,只要执行的查询执行了select,都可以用rs.eof 或者 rs.recordcount来判断, 但是,如果执行的sql中加了逻辑判断,导致没有执行任何sele ...

  9. mybatis关联查询数据模型分析——(七)

    1.     数据模型分析思路 1.每张表记录的数据内容 分模块对每张表记录的内容进行熟悉,相当 于你学习系统 需求(功能)的过程. 2.每张表重要的字段设置 非空字段.外键字段 3.数据库级别表与表 ...

  10. 如何对xilinx FPGA进行bit文件加密

    记录背景:最近在用Vivado评估国外一个公司所提供的ISE所建的工程时,由于我并没有安装ISE工程,因此将其提供的所有v文件导入到Vivado中,对其进行编译.添加完之后成功建立顶层文件,但奇怪的是 ...