题意:

有一个谷仓容量为\(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. SQL SERVER 错误代码 0x534

    解决办法就是修改一下登陆名: ALTER LOGIN [G-PC\zqwang]   WITH NAME=[新的机器名\zqwang]; 然后查询一下 Service Broker 队列, 里面已经有 ...

  2. SpringBoot 很重要的一个类WebMvcConfigurerAdapter 的使用

    自定义资源映射addResourceHandlers 比如,我们想自定义静态资源映射目录的话,只需重写addResourceHandlers方法即可. @Configuration public cl ...

  3. python实现各种排序

    1.冒泡排序: # -*- coding: utf-8 -*- def BubbleSort(a): n=len(a) for i in range(0,n-1): swapped=False for ...

  4. 利用ASP.NET里自带的站点地图工具制作网站站点地图

    站点地图很方便能快速给我们导航我们要去访问的地址,能按层级关系分门别类,给用户一个很好的用户体验,很好的看到自己当前所在的网站位置 站点地图,又称网站地图,它就是一个页面,上面放置了网站上所有页面的链 ...

  5. 【2017-06-20】Linux应用开发工程师C/C++面试问题记录之一:Linux多线程程序的同步问题

    参考之一:Linux 线程同步的三种方法 链接地址:http://www.cnblogs.com/eleclsc/p/5838790.html 简要回答: Linux下线程同步最常用的三种方法就是互斥 ...

  6. leetcode: 复杂度

    1. single-number Given an array of integers, every element appears twice except for one. Find that s ...

  7. CRUD全栈式编程架构总结

    这里放出实例代码 github.com/SkyvenXiong/HCC

  8. MHA启动及关闭

    MHA启动及关闭 #masterha_manager --global_conf=/etc/masterha/masterha_default.conf --conf=/etc/masterha/ap ...

  9. uva题库爬取

    每次进uva都慢的要死,而且一步一步找到自己的那个题目简直要命. 于是,我想到做一个爬取uva题库,记录一下其中遇到的问题. 1.uva题目的链接是一个外部的,想要获取https资源,会报出SNIMi ...

  10. 【转】android gravity属性 和 weight属性

    有点忘记这两个属性了,复习一下. 来看这个布局文件 <?xml version="1.0" encoding="utf-8"?> <Linea ...