题意:

有一个谷仓容量为\(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)\)的值不会太大,可以通过直接计算比较大小
  1. #include <cstdio>
  2. #include <cstring>
  3. using namespace std;
  4. typedef long long LL;
  5. LL n, m;
  6. bool ok(LL x) {
  7. x -= m;
  8. LL t = n - m;
  9. t <<= 1;
  10. t /= x;
  11. t /= x + 1;
  12. if(t > 1) return false;
  13. if(!t) return true;
  14. return n - m == x * (x + 1) / 2;
  15. }
  16. int main()
  17. {
  18. scanf("%lld%lld", &n, &m);
  19. if(n <= m) {
  20. printf("%lld\n", n);
  21. return 0;
  22. }
  23. LL L = m + 1, R = n;
  24. while(L < R) {
  25. LL M = (L + R) / 2;
  26. if(ok(M)) R = M;
  27. else L = M + 1;
  28. }
  29. printf("%lld\n", L);
  30. return 0;
  31. }

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. selenium代理

    selenium.KeyDown("id=ctaskName", "d");            selenium.KeyPress("id=cta ...

  2. Azure 3月新公布(二)

    Azure 3月新发布:HDInsight 的 Apache Hadoop 以及 ExpressRoute 超高性能网关层正式发布,SQL Database Premium RS 层发布公共预览版 A ...

  3. Linux命令之添加权限Chmod的使用

    chmod是change mode的缩写,是修改文件权限的一个命令: 一个文件分别有三组权限:用户拥有者,用户组 第一个横杆-表示文件,如果是d表示目录.还有可能是l,表示链接. 第一组(rw-)表示 ...

  4. Spring Boot入门程序-STS

    使用Eclipse EE 中的 Spring Tool插件,完成 第一个Spring Boot应用程序的创建. 一.安装Spirng Tool插件 在 Eclipse EE Oxygen版本,安装“S ...

  5. vue-表单绑定

    表单数据绑定1.1你可以用 v-model 指令在表单控件元素上创建双向数据绑定.它会根据控件类型自动选取正确的方法来更新元素.尽管有些神奇,但 v-model 本质上不过是语法糖,它负责监听用户的输 ...

  6. TP5.0: 显示错误信息

    在TP5中,我们运行的代码有错误无法执行时,只显示页面错误,而不显示错误信息 对我我来讲是无法接受滴!!毕竟我还是个小渣渣,查看了百度,解决方案是: 在application/config,php中找 ...

  7. when 让你跳出异步回调噩梦 node.js下promise/A规范的使用

    其实关于promise 的博客,前端时间专门写了一篇关于 promise 规范的文章,promise规范 让 javascript 中的异步调用更加人性化. 简单回忆下: promise/A规范定义的 ...

  8. 5.2 Array类型

    ◆  创建数组的基本方式有两种. ①第一种是使用Array构造函数,new关键字可省略 var colors = new Array(); var colors = new Array(20); // ...

  9. tcp 的编程例子

    https://www.cnblogs.com/ylllove/p/6852125.html

  10. JAVA HTTP连接(HttpURLConnection)中使用代理(Proxy)及其验证(Authentication)

    public static void main(String[] args) { // TODO Auto-generated method stub try { URL url = new URL( ...