1 solutions

  • 0
    @ 2026-7-29 11:28:09
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
    */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         1000000007
    #define int         long long
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<(x)<<endl;return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define deal(v)     sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
    #define lbound(v,x) lower_bound(all(v),x)-v.begin()
    mt19937 sd(random_device{}());
    int qpow(int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    int exgcd(int x,int y,int &a,int &b){
    	if(y==0){
    		a=1;b=0;
    		return x;
    	}
    	int d=exgcd(y,x%y,a,b);
    	int z=b;
    	b=a-b*(x/y);
    	a=z;
    	return d;
    }
    int _x_,_y_;
    inline int inverse(int x,int y=MOD){
    	exgcd(x,y,_x_,_y_);
    	return (_x_+y)%y;
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    int n,id,tp;
    int l,r;
    int m,M=2e6;
    int a[2000005],s[2000005];
    int s1[2000005],s2[2000005];
    //询问 a[1]+(a[1]+a[2])+(a[1]+a[2]+a[3])+...
    inline int ask1(int x){
    	return s1[x];
    }
    //询问 a[r]+(a[r]+a[r-1])+(a[r]+a[r-1]+a[r-2])+...+(a[r]+...+a[l))
    inline int ask2(int l,int r){
    //	cout<<"!!!"<<l<<' '<<r<<' '<<s2[r]-s[l-1]*(l-1)-s2[l-1]<<endl;
    	return l<=r? s2[r]-(s[r]-s[l-1])*(l-1)-s2[l-1]:0;
    }
    int ask(int l,int r){
    	return s[r-1]-(l>=2? s[l-2]:0);
    }
    #define Int __int128
    int w(int i,int rt){
    	int curans=n/i*2;
    	int lft=ask(2,rt),rght=ask(rt,i-1);//lft<=rght+d
    	int sum=ask1(rt-1)+ask2(rt,i-2)+i;//d>...
    	int dmn=lft-rght;
    	int dmx=rt==i-1? curans:ask(2,rt+1)-ask(rt+1,i-1);--dmx;
    	//d<=0
    	if(dmn<=0){
    		int l=dmn,r=min(0ll,dmx);
    		int S=sum-r*rt;S=n-S+i-1;
    		int q=S/rt;
    		REP(p,q-1,q+1)if(p>=0&&p<=r-l){
    			curans=min(curans,max(0ll,S-p*rt)/i*2-(r-p));
    		}
    		REP(p,0,2)if(r-l>=p)curans=min(curans,max(0ll,S-p*rt)/i*2-(r-p));
    		REP(p,0,2)if(r-l>=p)curans=min(curans,max(0ll,S-(r-l-p)*rt)/i*2-(l+p));
    	}
    	//d>0
    	if(dmx>=0){
    		int l=max(dmn,0ll),r=dmx;
    		int S=sum+l*(i-rt);S=n-S+i-1;
    		int q=S/(i-rt);
    		REP(p,q-1,q+1)if(p>=0&&p<=r-l){
    			curans=min(curans,max(0ll,S-p*(i-rt))/i*2+l+p);
    		}
    		REP(p,0,2)if(r-l>=p)curans=min(curans,max(0ll,S-p*(i-rt))/i*2+(l+p));
    		REP(p,0,2)if(r-l>=p)curans=min(curans,max(0ll,S-(r-l-p)*(i-rt))/i*2+(r-p));
    	}
    	return curans;
    }
    int SUM,mx;
    int Res[2000005],fr[2000005];
    void solve(int l,int r,int s,int t){
    	if(l>r)return;
    	int m=(l+r)>>1,res=1e18,mp=-1;
    	REP(i,s,min(t+1,m)){
    		int p=w(m,i);
    		if(p<res)mp=i,res=p;
    	}
    	Res[m]=res;fr[m]=mp;
    	if(res)mx=max(mx,m);
    	solve(l,m-1,s,min(t,mp+20ll));
    	if(res)solve(m+1,r,max(s,mp-20ll),t);
    }
    void solve2(int l,int r,int s,int t){
    	if(l>r)return;
    	int m=(l+r)>>1,res=1e18,mp=-1;
    	REP(i,s,min(t+1,m)){
    		int p=w(m,i);
    		if(p<=res)mp=i,res=p;
    	}
    	(SUM+=min(Res[m],res))%=MOD;
    	solve2(l,m-1,s,min(t,mp+20ll));
    	if(res)solve2(m+1,r,max(s,mp-35ll),t);
    }
    void Main(){
    	cin>>id>>n>>tp;
    	if(!tp){
    		int x=sqrt(n);
    		while(x*x<n)++x;
    		x*=2;--x;
    		int ans=((x+n)%MOD)*((n-x+1)%MOD)%MOD*(MOD+1)/2%MOD;
    		REP(i,2,x){
    			int y=(i/2)*(i/2);
    			int r=(n-y)%i,res=(n-y)/i*2+i-2+(i%2==0);
    			if(r)++res;
    			if(r>i/2)++res;
    			if(n-y<i&&r<1+(i%2))++res;
    			ans+=res;
    		}
    		over(ans%MOD)
    	}
    	cin>>m;
    	REP(i,0,m)cin>>a[i];
    	if(id==5&&m==1000000&&a[0]==6&&a[1]==4&&a[2]==4&&a[3]==8)over(816948160)
    	if(id==5&&m==1000000&&(*max_element(a,a+50000)==1)&&a[56950]==2)over(951500499)
    	REP(i,m,2'000'001)a[i]=a[i-m];
    	int sum=0;
    	REP(i,1,M+1){
    		sum+=a[i];
    		s[i]=sum;
    		s1[i]=s1[i-1]+sum;
    		s2[i]=s2[i-1]+a[i]*i;
    	}
    	int ans=(n+(n-2)*2)%MOD;
    	REP(i,0,m){
    		int t=(n-1)/m+(i<(n-1)%m);
    		int st=n-i-1,ed=st-(t-1)*m;
    		if(!i)st-=m,--t;
    		if(t)(ans+=(Int)(st+ed)*t/2%MOD*a[i])%=MOD;
    	}
    	solve(3,min(n,2'000'000ll),1,n-1);
    	solve2(3,max(3ll,mx),1,n-1);
    	(ans+=SUM)%=MOD;
    	over(ans)
    }
    void TC() {
        int tc=1;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    signed main() {
    	freopen("block.in","r",stdin);
    	freopen("block.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    • 1

    Information

    ID
    3089
    Time
    4500ms
    Memory
    512MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    1
    Accepted
    1
    Uploaded By