Codeforces785C (二分)

本文探讨了一个关于容器取物的问题,初始容器内有一定数量的物品,每天取出一定数量后补充固定数量,目标是计算取空容器所需的天数。文章详细介绍了问题的解决思路,包括特殊情况的快速判断和一般情况下的二分查找算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题意:给出一个n,m,最初一个容器里面的数量是n,然后每天取1,2,3,4……数量的物品,每次取完之后就向里面加入m个物品,问最终取完了容器里面的时间。


思路:我们知道,当n<= m的时候,所得到的值一定是n,因为当为第n天的时候就可以取得是n,可以直接取完。

           当n > m的时候,我们可以把取的东西分为两个部分,第一部分是小于m的天数,因为这个时候取得物品数量小于m,所以容器里面的数量总是保持在n.

          在天数大于m的时候,我们发现每天减少的是1,2,3,4……,设m之后经过了x天,所以减少的是x *(x + 1)/2,因为每次是取完之后再加入m,所以要考虑这个m,所以经过x天之后容器被取走的数量是x *(x + 1)/2 + m ,如果满足条件>=n就是结束点。所以二分一下。


PS:注意一下,防止溢出。


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef set<int>::iterator ITER;
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x)

const int maxn = 1e5 + 10;
const int Mod = 1e9 + 7;
const int N = 2;
struct Matrix{Matrix(){clr(m,0);}int m[N][N];};

Matrix Mul(Matrix a,Matrix b){Matrix c;for(int i = 0; i < N; i ++){for(int k = 0; k < N; k ++){if(a.m[i][k] == 0)continue;for(int j = 0; j < N; j ++)c.m[i][j] += a.m[i][k] * b.m[k][j] % Mod,c.m[i][j] %= Mod;}}return c;}
ll Mul(ll a,ll b){a %= Mod;b %= Mod;ll ret = 0;while(b){if(b & 1){ret += a;if(ret > Mod)ret -= Mod;}b >>= 1;a = (a << 1) % Mod;}return ret;}
Matrix pows(Matrix x,ll n){Matrix ret;for(int i = 0; i < 2; i ++)ret.m[i][i] = 1;while(n){if(n & 1)ret = Mul(ret,x);x = Mul(x,x);n >>= 1;}return ret;}
ll pows(ll x,ll n){ll ret = 1;while(n){if(n & 1)ret = ret * x % Mod;x = x * x % Mod;n >>= 1;}return ret;}
ll powss(ll x,ll n){ll ret = 1;while(n){if(n & 1)ret = Mul(ret,x);x = Mul(x,x);n >>= 1;}return ret;}
ll gcd(ll x,ll y){return y ? gcd(y,x % y):x;}
ll euler(int n){int ret = n;for(int i = 2; i * i<= n; i ++)if(n % i == 0){ret = ret / i * (i - 1);while(n % i == 0)n /= i;}if(n > 1)ret = ret / n * (n - 1);return ret;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d = a;x = 1;y = 0;return;}ex_gcd(b,a % b,d ,y,x);y -=a/b * x;}
inline int lowbit(int x){return x &(-x);}

//void update(int x,int val){for(int i = x; x < maxn; i += lowbit(i))tree[i] += val;}
//void get(int x){int ret = 0;for(int i = x; i > 0 ;i -= lowbit(i))ret += tree[i];return ret;}
//int finds(int x){return fa[x] == x ? x : (fa[x] = finds(fa[x]));}

ll n,m;
ll calc(ll x)
{
    ll temp;
    if(x % 2)
        temp = (x + 1)/2 * x;
    else temp = x /2 * (x + 1);
//    if(x == 2)
//        cout << temp << " " << n << " " << " " << m << endl;
    return temp - n + m;
}
int main()
{
    while( ~ scanf("%I64d%I64d",&n,&m))
    {
        if(n <= m)
        {
            cout << n << endl;continue;
        }
        ll ans = m;
        ll t;
        ll l = 0,r = 2e9;
        while(l <= r)
        {

            ll mid = (l + r) >> 1;
//            cout << l << " " << r <<  " " << mid << " " << calc(mid) << endl;
            if(calc(mid) >= 0)
            {
                t = mid;r = mid - 1;
            }
            else l = mid + 1;
        }
//        int temp;
//        if(t % 2)
//            temp = (t - 1) /2 * t;
//        else temp = t /2 * (t - 1);
//        if(temp >= n - m)t --;
        cout << ans  + t << endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值