题意:

有一个谷仓容量为\(n\),谷仓第一天是满的,然后每天都发生这两件事:

  1. 往谷仓中放\(m\)个谷子,多出来的忽略掉
  2. 第\(i\)天来\(i\)只麻雀,吃掉\(i\)个谷子

求多少天后谷仓会空

分析:

分类讨论:

1. \(n \leq m\)

每天都会把谷仓放满,所以第\(n\)后会变空

2. \(n > m\)

前\(m\)天后谷仓还一直都是满的

第\(m+1\)天后还剩\(n-m-1\),第\(m+2\)天后还剩\(n-m-1-2\)

第\(m+i\)天后还剩\(n-m-\frac{i(i+1)}{2}\)

所以可以二分求出答案

注意到比较\(n-m>\frac{i(i+1)}{2}\)时,中间计算结果会溢出

所以可以通过除法来比较大小,令\(t=\frac{2(n-m)}{i(i+1)}\)

  • \(t>1\),有\(n-m>\frac{i(i+1)}{2}\)
  • \(t=0\),有\(n-m<\frac{i(i+1)}{2}\)
  • \(t=1\),这时\(i(i+1)\)的值不会太大,可以通过直接计算比较大小
#include <cstdio>
#include <cstring>
using namespace std; typedef long long LL; LL n, m; bool ok(LL x) {
x -= m;
LL t = n - m; t <<= 1;
t /= x;
t /= x + 1; if(t > 1) return false;
if(!t) return true;
return n - m == x * (x + 1) / 2;
} int main()
{
scanf("%lld%lld", &n, &m);
if(n <= m) {
printf("%lld\n", n);
return 0;
} LL L = m + 1, R = n;
while(L < R) {
LL M = (L + R) / 2;
if(ok(M)) R = M;
else L = M + 1;
} printf("%lld\n", L); return 0;
}

CodeForces 785C Anton and Fairy Tale 二分的更多相关文章

  1. CodeForces 785C Anton and Fairy Tale

    二分. 如果$n≤m$,显然只能$n$天. 如果$n>m$,至少可以$m$天,剩余还可以支撑多少天,可以二分计算得到,也可以推公式.二分计算的话可能爆$long$ $long$,上了个$Java ...

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

    C. Anton and Fairy Tale 题目连接: http://codeforces.com/contest/785/problem/C Description Anton likes to ...

  3. 【codeforces 785C】Anton and Fairy Tale

    [题目链接]:http://codeforces.com/contest/785/problem/C [题意] 容量为n的谷仓,每一天都会有m个谷子入仓(满了就视为m);第i天 会有i只鸟叼走i个谷子 ...

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

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

  5. C. Anton and Fairy Tale

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

  6. Codeforces 734C. Anton and Making Potions(二分)

    Anton is playing a very interesting computer game, but now he is stuck at one of the levels. To pass ...

  7. C. Anton and Fairy Tale(数学推式子)

    \(数学题,式子并不难推,但边界是真的烦\) \(\color{Red}{Ⅰ.其实可以发现,当m>=n时,每次都可以粮食补到n,所以一定是在第n天消耗完毕}\) \(\color{Purple} ...

  8. ural 1343. Fairy Tale

    1343. Fairy Tale Time limit: 1.0 secondMemory limit: 64 MB 12 months to sing and dance in a ring the ...

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

随机推荐

  1. 安装office提示Office 16 Click-to-Run Extensibility Component

    今天安装office时,提示Office 16 Click-to-Run Extensibility Component或者Office 15 Click-to-Run Extensibility C ...

  2. python类的反射

    反射 通过字符串映射或者修改程序运行时的状态.属性.方法, 有一下4个方法 小例子--根据用户输入调用方法: class Dog(object): def __init__(self,name): s ...

  3. 算法练习-Palindrome Number

    判断回文整数 来源 https://leetcode.com/problems/palindrome-number/ 要求 判断一个整数是不是回文数,尽量减少内存暂用. 思路 可能的情况: 负数的应当 ...

  4. PyYAML使用

    install yum -y install PyYAML document http://www.showyounger.com/show/101586.html http://pyyaml.org ...

  5. vm中efi模式安装windows10

    选择dvd: 界面出现“Press any key to boot from CD or DVD”时,再迅速按下任意键就OK了.

  6. weight decay 和正则化caffe

    正则化是为了防止过拟合,因为正则化能降低权重 caffe默认L2正则化 代码讲解的地址:http://alanse7en.github.io/caffedai-ma-jie-xi-4/ 重要的一个回答 ...

  7. js实现弹窗一个ip在24小时只弹出一次的代码

    function cookieGO(name) { var today = new Date(); var expires = new Date(); expires.setTime(today.ge ...

  8. AngularJS 指循环数组对象

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. C# grid控件用数据库分页后台怎么写?

    C#grid控件使用数据库分页的写法如下: mySystem.GetDataa(gridName.PageIndex *gridName.PageSize + 1, (gridName.PageInd ...

  10. 浅谈PHP中的数组和JS中的数组

    最近在做前后端对接的时候,遇到一个问题,前端要求返回的数据格式是左边的,但是我通过json_encode返回到的数据格式是右边的   注意:数据格式从"[]"(数组)变成了&quo ...