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 ...
随机推荐
- IIS7 配置SSL 绑定主机头
IIS7下面默认HTTPS绑定是无法指定主机头的,我们可以通过手工修改IIS配置来实现主机头绑定. 首先停止IIS服务. 然后打开C:/Windows/system32/inetsrv/config/ ...
- hibernate 注解不给提示
1.alt + / 会给提示 2.上面这种稍微麻烦一点,如果需要写了@就直接给提示,就需要设置一下: a)Window - preferences b)搜 content assist,选中 Java ...
- Azure 7 月新公布
Azure 7月新发布:Cosmos DB,事件中心捕捉功能,Hybrid Connections,流量管理器快速故障转移功能. 您现有的 DocumentDB 资源现已作为 Azure 门户上 Az ...
- MVC学习笔记:MVC实现用户登录验证ActionFilterAttribute用法并实现统一授权
在项目下新建一个文件夹来专门放过滤器类,首先创建一个类LoginFilter,这个类继承ActionFilterAttribute.用来检查用户是否登录和用户权限.: using System; us ...
- Vue--父组件传数据给子组件,子组件生命周期过程拿到数据的情况
需求: 在子组件渲染之前,我要修改数据的某个字段 结果是 组件在beforeUpdate,updated 的状态才能拿到父组件的数据 那么证明,我根本无法在beforeUpdate,updated两个 ...
- Django Reverse for 'artic_post' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] ...
- 用FileReader对象获取图片base64代码并预览
MDN中FileReader的详细介绍: https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader 用FileReader获取图片base ...
- 1801: [Ahoi2009]chess 中国象棋
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2520 Solved: 1524[Submit][Status][Discuss] Descripti ...
- UVA_10820_send a table
When participating in programming contests, you sometimes face the following problem: You know how t ...
- Linux中用户与用户组管理
1.基础知识 Linux作为一种多用户的操作系统(服务器系统),允许多个用户同时登陆到系统上,并响应每个用户的请求. 任何需要使用操作系统的用户,都需要一个系统账号,账号分为:管理员账号与普通用户账号 ...