CodeForces 785C Anton and Fairy Tale 二分
题意:
有一个谷仓容量为\(n\),谷仓第一天是满的,然后每天都发生这两件事:
- 往谷仓中放\(m\)个谷子,多出来的忽略掉
- 第\(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 二分的更多相关文章
- CodeForces 785C Anton and Fairy Tale
二分. 如果$n≤m$,显然只能$n$天. 如果$n>m$,至少可以$m$天,剩余还可以支撑多少天,可以二分计算得到,也可以推公式.二分计算的话可能爆$long$ $long$,上了个$Java ...
- 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 ...
- 【codeforces 785C】Anton and Fairy Tale
[题目链接]:http://codeforces.com/contest/785/problem/C [题意] 容量为n的谷仓,每一天都会有m个谷子入仓(满了就视为m);第i天 会有i只鸟叼走i个谷子 ...
- 【二分】Codeforces Round #404 (Div. 2) C. Anton and Fairy Tale
当m>=n时,显然答案是n: 若m<n,在第m天之后,每天粮仓减少的量会形成等差数列,只需要二分到底在第几天,粮仓第一次下降到0即可. 若直接解不等式,可能会有误差,需要在答案旁边扫一下. ...
- C. Anton and Fairy Tale
链接 [https://codeforces.com/contest/785/problem/C] 题意 初始时有n,第1天先加m开始吃1,但总的不能超过n,第i天先加m开始吃i(如果不够或刚好就吃完 ...
- 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 ...
- C. Anton and Fairy Tale(数学推式子)
\(数学题,式子并不难推,但边界是真的烦\) \(\color{Red}{Ⅰ.其实可以发现,当m>=n时,每次都可以粮食补到n,所以一定是在第n天消耗完毕}\) \(\color{Purple} ...
- 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 ...
- 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 ...
随机推荐
- Struts_OGNL(Object Graph Navigation Language) 对象图导航语言
1.访问值栈中的action的普通属性: 请求: <a href="ognl.action?username=u&password=p">访问属性</a& ...
- Socket连接时,端口是怎么分配的
socket 客户端连接socket 的端口每个是唯一的,每个新的连接,端口号+1 从1024-65534 最大到65534 然后再开始循环 中间遇到已经使用的端口就跳过
- PHP : 封装跳转函数,实现三个页面的跳转
具体实现:有a,b两个页面,一个跳转页面c,在a执行完后先进行c页面的提示,再跳转到b 1.文件设计: 2.c页面封装方法内容(function.php): a页面内容(a.html): a页面的后台 ...
- Django Field lookups (字段查找)
字段查找是指定SQL WHERE子句的核心内容的方式. 它们被指定为QuerySet方法filter().exclude()和get()的关键字参数. 1.exact:精确查找.如果为比较提供的值为N ...
- QT OpenGL中文教程在QT4版本后的错误代码更改(一)
由于教程中说的已经够可以了,这里就不对代码进行分析了,有兴趣可以自己去看看.这个教程来源于原来的NeHeOpenGL中文教程 (http://www.yakergong.net/nehe/) ,但其有 ...
- selenium添加cookie切换到不同环境
背景:网站中需要切环境到预发布,在用谷歌浏览器可以使用工具,但是在selenium启动时,是不会带任何插件,向开发了解下,切换环境本质是添加cookie值,那么这个就简单了 1.使用selenium中 ...
- B. Qualifying Contest_排序
B. Qualifying Contest time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeblocks的常用Debug快捷键
1.在鼠标处开始Debug,F4. 2.逐步调试,F7. 3.进入函数,shift+F7. 4.结束Debug,shift+F8.
- Poj(1789),最小生成树,Prim
题目链接:http://poj.org/problem?id=1789 还是套路. #include <stdio.h> #include <string.h> #define ...
- 2017.11.12 web中JDBC 方式访问数据库的技术
JavaWeb------ 第四章 JDBC数据库访问技术 在JavaWeb应用程序中数据库访问是通过Java数据库连接(JavaDateBase Connectivity简称JDBC)数据库的链接一 ...