CF 830C (Bamboo partition-satisfies the maximum value of D of \sum_{i=1}^n{D\lceil ai/d\rceil-a_i} \leq k) __ Number theory

Source: Internet
Author: User

Ask: Find the largest positive integer d, so that ∑ni=1d⌈ai/d⌉−ai≤k \sum_{i=1}^n{D\lceil ai/d\rceil-a_i} \leq K
observed as long as ⌈ai/d⌉ (i=1⋯n) \lceil AI/D\RC The Eil (i=1\cdots N) is invariant, the function is linear, and the
therefore solves each segment separately.
Segment Point total Nmax (AI) −−−−−−−√ n\sqrt {max (a_i)}

#include <bits/stdc++.h> using namespace std; #define for (i,n) for (int i=1;i<=n;i++) #define FORK (i,k,n) for (int i=k;i<=n;i++) #define REP (i,n) for (int i=0;i< n;i++) #define ForD (i,n) for (int i=n;i;i--) #define FORKD (i,k,n) to (int i=n;i>=k;i--) #define REPD (i,n) for (int i=n;i& gt;=0;i--) #define FORP (x) for (int p=pre[x];p, p=next[p]) #define FORPITER (x) for (int &p=iter[x];p; p=next[p]) #defin
E Lson (o<<1) #define Rson ((o<<1) +1) #define MEM (a) memset (A,0,sizeof (a));
#define MEMI (a) memset (A,127,sizeof (a));
#define MEMI (a) memset (A,128,sizeof (a)); #define INF (2139062143) #define F (1000000007) #define PB push_back #define MP Make_pair #define FI #define SE sec Ond #define VI vector<int> #define PI pair<int,int> #define SI (a) ((a). Size ()) #define PR (Kcase,ans) printf ("
Case #%d:%i64d\n ", Kcase,ans); #define PRI (A,n) for (i,n-1) cout<<a[i]<< ';
cout<<a[n]<<endl;
                   #define PRI2D (A,n,m) for (i,n) {\     for (j,m-1) cout<<a[i][j]<< '; \ cout<<a[i][m]<<endl; \} #pragma COMMENT (linker, "/stack:102400000,102400000") #define ALL (x) (x). The Begin (), (x). End () Ty
Pedef Long Long ll;
typedef long double LD;
typedef unsigned long long ull;  LL Mul (ll A,ll b) {return (a*b)%F;} ll Add (ll A,ll b) {return (a+b)%F;} ll Sub (ll A,ll b) {return ((a-b)%f+f)%F;} void Upd (LL
    &a,ll b) {a= (a%f+b%f)%F;} int read () {int x=0,f=1; char Ch=getchar ();
    while (!isdigit (CH)) {if (ch== '-') f=-1; Ch=getchar ();}
    while (IsDigit (ch)) {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f;
#define MAXN (a) ll n,a[maxn],k;
    LL Calc (ll d) {ll =;
    for (i,n) {p+= (a[i]%d+d)%d;
} return p;
    BOOL Check (ll D) {if (d<0) return 0;
    ll p =;
    for (i,n) {p+= (a[i]%d+d)%d;
    }//cout<<d<< ' <<p<< ' <<endl;
Return p<=k;
    ll work (ll L,ll R) {ll ans=-1; if (l>r) return ans;
        if (l==r) {if (check (l)) ans=l;
    return ans;
    ll P=calc (L), Delta=calc (l+1)-p;
    ll mm;
    if (!delta) {mm=r;
        else {ll c= (k-p)/(delta);

    Mm=min (L+C,R);

    } if (check (mm)) ans=mm;
return ans;
} vector<ll> S;
    int main () {//Freopen ("C.in", "R", stdin);//Freopen (". Out", "w", stdout);
    cin>>n>>k;
    for (I,n) a[i]=read ();
    Sort (a+1,a+1+n);
    for (I,n) a[i]=-a[i];

    ll ANS=0,NXT;
        for (I,n) {ll nxt=1;
            for (ll Pre=1;pre<=-a[i];p re=nxt+1) {nxt=a[i]/(a[i]/pre);
        S.PB (NXT);
    } S.PB (1);
    Sort (All (S));
    S.erase (Unique (All (S)), S.end ());
    Vector<ll>::reverse_iterator it;
    Work (35,36);
    ll Pre=*s.rbegin ();
        For (It=s.rbegin (); It!=s.rend (); it++) {if (check (pre)) {Ans=pre;
        ll Nxt=*it;
        ll P=work (Nxt+1,pre); if (p!=-1) {Ans=maX (ANS,P);
        Break
    } pre=nxt;

    } if (check (1)) Ans=max (ANS,1LL);
    ll Tot=-a[n]*n;
    for (I,n) tot-=-a[i];
        if (tot<=k) {ll p= (K-tot)/n;
    if (check (-a[n]+p)) Ans=max (ans,-a[n]+p);
    } cout<<ans<<endl;
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.