B. Loan Repayment

B. Loan Repayment

https://codeforces.com/group/5yyKg9gx7m/contest/270203/problem/B

分析:

参考大佬题解:https://www.cnblogs.com/BrianPeng/p/12284076.html

首先要二分x,用函数judge判断是否满足条件。对每个数x,每次都把剩余的r除x,一直到m。而主要想法就是把几次的除法合成一块,用一个y来近似。就是在a天内依次给的牛奶为y1,y2...ya。然后把他们的和用y*a近似。从而减少计算。

代码:

#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll t,re,y;
ll n,m,k;
bool judge(ll x)
{
    t=k,re=n;
    while(t>0&&re>0)
    {
        y=re/x;
        ll a=re/y-x+1;
        if(a>=t) a=t;
        if(y>m) re-=y*a,t-=a;
        else re-=m*t,t=0;
    }
    return re<=0;
} 
int main()
{
    scanf("%lld%lld%lld",&n,&k,&m);
    ll l=1,r=n;
    ll mid=(l+r)>>1;
    while(l<r) 
    {
        mid=(l+r)>>1;
        if(judge(mid)) l=mid+1;
        else r=mid;
    }
    cout<<l-1<<endl;
    return 0;
}
 

猜你喜欢

转载自www.cnblogs.com/studyshare777/p/12404928.html