[CF830C]Bamboo Partition

题目

传送门

题解

对于这样一类体型,我们首要要做的都是推柿子:

\[\begin{aligned} \sum_{i=1}^n d-[(a_i-1)\bmod d]+1&=\sum_{i=1}^n d-[a_i-1-\left\lfloor\frac{a_i-1}{d}\right\rfloor d]+1 \\ &=\sum_{i=1}^n d-a_i+\left\lfloor\frac{a_i-1}{d}\right\rfloor d \\ &= nd-\sum_{i=1}^na_i+\sum_{i=1}^n\left\lfloor\frac{a_i-1}{d}\right\rfloor d \\ &=d\left( n+\sum_{i=1}^n\left\lfloor\frac{a_i-1}{d}\right\rfloor \right)-\sum_{i=1} ^na_i\end{aligned} \]

所以题目要求我们的是

\[\begin{aligned} d\left( n+\sum_{i=1}^n\left\lfloor\frac{a_i-1}{d}\right\rfloor \right)-\sum_{i=1}a_i&\le k \\ \Rightarrow d\left( n+\sum_{i=1}^n\left\lfloor\frac{a_i-1}{d}\right\rfloor \right)&\le k+\sum_{i=1}a_i \end{aligned} \]

由于右边 \(k+\sum_{i=1}^na_i\) 是已知的,而对于左边的下取整又最多只会有 \(\sqrt{\max (a_i)}\) 种取值,我们不妨暴力每种取值 \(l\),对于每种取值算出 \(\sum_{i=1}^n\left\lfloor\frac{a_i-1}{d}\right\rfloor\),然后计算对应的 \(d\) 是多少。

注意,代码实现中有很多地方使用除法,主要目的是避免乘法爆掉 long long,另一个原因是我们需要求的是最大的 \(d\),而 \(d\) 为整数,因此可知 \(d=\left\lfloor\frac{k+\sum_{i=1}^n a_i}{n+\sum_{i=1}^n\left\lfloor\frac{a_i-1}{l}\right\rfloor}\right\rfloor\),因为这里使用下取整,刚好为整形除法的法则。

代码

#include<bits/stdc++.h>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#ifdef _GLIBCXX_CSTDIO
#define cg (c=getchar())
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T qread(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
#undef cg
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
#endif
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=100;
const LL INF=(1ll<<60)-1;

LL a[MAXN+5],n,k,maxx,ans;

signed main(){
    ios::sync_with_stdio(false);
    cin>>n>>k;
    rep(i,1,n)cin>>a[i],k+=a[i],maxx=Max(maxx,a[i]-1);
    // printf("k == %lld, maxx == %lld\n",k,maxx);
    for(LL l=1,r,s,tmp;l<=maxx;l=r+1){
        // printf("Now l == %lld\n",l);
        r=INF,s=0;
        rep(i,1,n)if(a[i]-1>=l){
            s+=(a[i]-1)/l;
            r=Min(r,(a[i]-1)/((a[i]-1)/l));
            // printf("After a[%d] == %lld, r == %lld\n",i,a[i],r);
        }tmp=k/(s+n);
        if(l<=tmp)ans=Max(ans,Min(tmp,r));
        // printf("access : l == %lld\n",l);
    }
    if(maxx<k/n)ans=Max(ans,k/n);
    cout<<ans<<endl;
	return 0;
}
posted @ 2020-05-22 21:53  Arextre  阅读(149)  评论(0编辑  收藏  举报