盼君勿忘:本题的快速幂单根号实现

· · 题解

前言

本来是想卡卡常,看快速幂 O(n\sqrt{n}\log{n}) 能否通过的,被卡成三秒 +eps,遂考虑奇怪优化。

然而发现可以证到单根号?题解区全是光速幂?快速幂比光速幂还要快?

更新:发现出现了一处不等号反向的问题,已修正。

简要解法

详细思路建议去考察其它题解,这里简单过一下。下面认为 n,q 同阶。

离线莫队,用链表维护桶的桶,这里由于性质特殊,维护起来比较容易。

查询时,利用链表遍历有效的位置,位置 p 的贡献为 (2^{len}-2^{len-p})val_p,其中 len 表示当前查询的区间长度,val_p 是出现次数为 p(针对原序列的数组)的数值的和。

由于桶的桶只有 O(\sqrt{n}) 个,所以暴力快速幂可以做到 O(n\sqrt{n}\log{n}),没卡过去。

于是用光速幂优化到 O(n\sqrt{n})。

比光速幂还快的快速幂?

:::info[注]{open} 基于本题数据测试。 :::

实现

只需要考虑算 2^{len-p}val_p 即可。我们知道,桶的桶只有根号个是因为:第一层桶的总和 \le n,由于 1+2+3+\cdots+\sqrt{2n}=n+\sqrt{\frac{n}{2}}>n,最多占据 \sqrt{2n}-1 个位置。下文忽略 -1 项。

考虑利用这个总和的性质,设第二层桶有 k 个有效位置,为 x_1<x_2<x_3<\cdots<x_k,则 \sum x_i\le n。设差分数组为 d_i,则 \sum_{i=1}^k(k-i+1)d_i\le n,d_i>0。

原本的快速幂实现中,每次指数都从 0 开始,太浪费了。考虑倒着做,这样 len-p 就是升序的(本题不保证有逆元),此时每次快速幂复杂度为 O(\log{d_i})。

这样,单次查询总复杂度为 O(\log{n}+\sqrt{n}+\sum_{i=1}^k\log{d_i}),其中 \log{n} 代表的是从 0 到 len-x_k 的快速幂。

证明

研究 \sum_{i=1}^k\log{d_i},条件是 \sum_{i=1}^k(k-i+1)d_i\le n,d_i>0。下面有一些是定义在实数上的。

不妨令 \sum_{i=1}^k(k-i+1)d_i=n,再令 g_i=(k-i+1)d_i,则 g_i>0,\sum g_i=n。

则 $\sum_{i=1}^k\log{d_i}\le k\log{n}-k\log{k}-\log{k!}=\frac{1}{\ln{2}}(k\ln{n}-k\ln{k}-\ln{k!})$。 $\ln{k!}=\sum_{j=1}^k1\times\ln{j}\ge\int_{1}^{k}\ln{x}\,dx=k\ln{k}-k+1$。 则 $\sum_{i=1}^k\log{d_i}\le\frac{1}{\ln{2}}(k\ln{n}-2k\ln{k}+k-1)$,令 $f(k)=k\ln{n}-2k\ln{k}+k$。 令导函数为零:$f^\prime(k)=\ln{n}+1-2\ln{k}-2=0$,则 $n=ek^2,k=\sqrt{\frac{n}{e}}\le\sqrt{2n}$,可以取到,是极大值点。 则 $\sum_{i=1}^k\log{d_i}\le\frac{2\sqrt{\frac{n}{e}}-1}{\ln{2}}\le 1.76\sqrt{n}$。 于是,我们证明了上述方法的复杂度仍为 $O(\sqrt{n})$。 # 实现 从测试结果来看,上述方法常数并不大,可能原因:很多等号根本取不到,因为取值范围是整数而不是证明时的实数;数据并没有卡满这种做法。 [快速幂洛谷最优解](https://www.luogu.com.cn/record/294532767),[光速幂洛谷次优解](https://www.luogu.com.cn/record/294609094),[快速幂 QOJ 最优解](https://qoj.ac/submission/2799994)。 :::info[光速幂实现] ```cpp #include<bits/stdc++.h> using namespace std; #define ull unsigned long long #define lll __uint128_t namespace fastIO{ #define gc() (rp1==rp2&&(rp2=(rp1=buf)+fread(buf,1,IO,stdin))==rp1?EOF:*rp1++) #define pc(a) ((wrp==obuf+IO&&(fwrite(obuf,1,IO,stdout),wrp=obuf)),(*wrp++)=a) const int IO=1<<18; char buf[IO+1],obuf[IO+1],*wrp=obuf,*rp1,*rp2; template<typename qwq>inline void read(qwq &a){ a=0;int c=gc(); while(!isdigit(c)) c=gc(); while(isdigit(c)) a=10*a+c-'0',c=gc(); } template<typename qwq>inline void write(qwq x,char c=' '){ int sk[20],top=0; do{ sk[++top]=x%10,x/=10; }while(x); while(top) pc(sk[top--]+'0'); pc(c); } #undef pc #undef gc };using namespace fastIO; const int N=100005,B=512; int n,m,up,mod,ret,pre[N],nxt[N]; int ans[N],cnt[N],A[N],Acnt[N]; ull val[N],F[B+1],G[B+1],sum; struct FastMod{ ull b,m; void init(ull b){this->b=b,m=(1ull<<63)/b;} inline ull operator()(ull a){ ull q=(ull)((lll(m)*a)>>63),r=a-q*b; return r>=b?r-b:r; } }M; struct node{ int l,r,p,id; inline bool operator <(const node &x)const{ if((l>>9)!=(x.l>>9)) return l<x.l; return ((l>>9)&1)?r>x.r:r<x.r; } }Q[N]; inline void add(int p){ int x=Acnt[p],y=++Acnt[p]; cnt[x]--,cnt[y]++,val[x]-=p,val[y]+=p;//注意带权 if(cnt[x]==0&&cnt[y]==1){ pre[nxt[y]=nxt[x]]=y,nxt[pre[y]=pre[x]]=y; } else if(cnt[x]==0) nxt[pre[y]=pre[x]]=y; else if(cnt[y]==1) pre[nxt[y]=nxt[x]]=y,nxt[x]=y,pre[y]=x; } inline void del(int p){ int y=Acnt[p],x=--Acnt[p]; cnt[x]++,cnt[y]--,val[x]+=p,val[y]-=p; if(cnt[y]==0&&cnt[x]==1){ pre[nxt[x]=nxt[y]]=x,nxt[pre[x]=pre[y]]=x; } else if(cnt[y]==0) pre[nxt[x]=nxt[y]]=x; else if(cnt[x]==1) nxt[pre[x]=pre[y]]=x,pre[y]=x,nxt[x]=y; } template<typename qwq>inline void inc(qwq &a,qwq b){a+=b,(a>=mod)&&(a-=mod);} inline ull qpow(int x){return M(F[x&511]*G[x>>9]);} inline int query(int len){ M.init(mod),ret=0,sum=0; for(int i=1;i<=B;i++) F[i]=F[i-1],inc(F[i],F[i]); for(int i=1;i<=up;i++) G[i]=M(F[B]*G[i-1]); for(int p=nxt[0];p<=n;p=nxt[p]){ ret=M(ret+qpow(len-p)*val[p]),sum+=val[p]; }//val[p] 虽然大于 mod,但是不超过 5e9 ret=M(qpow(len)*sum+mod-ret); return ret; } int main(){ read(n),read(m),up=n/B+1,F[0]=1,G[0]=1; for(int i=1;i<=n;i++) read(A[i]); for(int i=1;i<=m;i++){ read(Q[i].l),read(Q[i].r); Q[i].id=i,read(Q[i].p); } sort(Q+1,Q+1+m),cnt[0]=n+1,nxt[0]=n+1,cnt[n+1]=1; for(int i=1,l=1,r=0;i<=m;i++){ while(r<Q[i].r) add(A[++r]); while(Q[i].l<l) add(A[--l]); while(Q[i].r<r) del(A[r--]); while(l<Q[i].l) del(A[l++]); if((mod=Q[i].p)==1) continue;//答案一定为 0 ans[Q[i].id]=query(Q[i].r-Q[i].l+1); } for(int i=1;i<=m;i++) write(ans[i],'\n'); return fwrite(obuf,1,wrp-obuf,stdout),0; } /* 经典结论:桶的桶只有根号个 */ ``` ::: :::info[优化后的快速幂实现] ```cpp #include<bits/stdc++.h> using namespace std; #define ull unsigned long long #define lll __uint128_t namespace fastIO{ #define gc() (rp1==rp2&&(rp2=(rp1=buf)+fread(buf,1,IO,stdin))==rp1?EOF:*rp1++) #define pc(a) ((wrp==obuf+IO&&(fwrite(obuf,1,IO,stdout),wrp=obuf)),(*wrp++)=a) const int IO=1<<18; char buf[IO+1],obuf[IO+1],*wrp=obuf,*rp1,*rp2; template<typename qwq>inline void read(qwq &a){ a=0;int c=gc(); while(!isdigit(c)) c=gc(); while(isdigit(c)) a=10*a+c-'0',c=gc(); } template<typename qwq>inline void write(qwq x,char c=' '){ int sk[20],top=0; do{ sk[++top]=x%10,x/=10; }while(x); while(top) pc(sk[top--]+'0'); pc(c); } #undef pc #undef gc };using namespace fastIO; const int N=100005,B=512; int n,m,up,mod,ret,pre[N],nxt[N]; int ans[N],cnt[N],A[N],Acnt[N]; ull val[N],sum,tmp; struct FastMod{ ull b,m; void init(ull b){this->b=b,m=(1ull<<63)/b;} inline ull operator()(ull a){ ull q=(ull)((lll(m)*a)>>63),r=a-q*b; return r>=b?r-b:r; } }M; struct node{ int l,r,p,id; inline bool operator <(const node &x)const{ if((l>>9)!=(x.l>>9)) return l<x.l; return ((l>>9)&1)?r>x.r:r<x.r; } }Q[N]; inline void add(int p){ int x=Acnt[p],y=++Acnt[p]; cnt[x]--,cnt[y]++,val[x]-=p,val[y]+=p;//注意带权 if(cnt[x]==0&&cnt[y]==1){ pre[nxt[y]=nxt[x]]=y,nxt[pre[y]=pre[x]]=y; } else if(cnt[x]==0) nxt[pre[y]=pre[x]]=y; else if(cnt[y]==1) pre[nxt[y]=nxt[x]]=y,nxt[x]=y,pre[y]=x; } inline void del(int p){ int y=Acnt[p],x=--Acnt[p]; cnt[x]++,cnt[y]--,val[x]+=p,val[y]-=p; if(cnt[y]==0&&cnt[x]==1){ pre[nxt[x]=nxt[y]]=x,nxt[pre[x]=pre[y]]=x; } else if(cnt[y]==0) pre[nxt[x]=nxt[y]]=x; else if(cnt[x]==1) nxt[pre[x]=pre[y]]=x,pre[y]=x,nxt[x]=y; } template<typename qwq>inline void inc(qwq &a,qwq b){a+=b,(a>=mod)&&(a-=mod);} inline void qpow(int x){ for(ull a=2;x;a=M(a*a),x>>=1){ if(x&1) tmp=M(tmp*a); } } inline int query(int len){ M.init(mod),ret=0,sum=0,tmp=1;int lst=len; for(int p=pre[n+1];p>0;p=pre[p]){ qpow(lst-p),lst=p; ret=M(ret+tmp*val[p]),sum+=val[p]; }//val[p] 虽然大于 mod,但是不超过 5e9 qpow(lst),ret=M(tmp*sum+mod-ret); return ret; } int main(){ read(n),read(m),up=n/B+1; for(int i=1;i<=n;i++) read(A[i]); for(int i=1;i<=m;i++){ read(Q[i].l),read(Q[i].r); Q[i].id=i,read(Q[i].p); } sort(Q+1,Q+1+m),cnt[0]=n+1,nxt[0]=n+1,cnt[n+1]=1; for(int i=1,l=1,r=0;i<=m;i++){ while(r<Q[i].r) add(A[++r]); while(Q[i].l<l) add(A[--l]); while(Q[i].r<r) del(A[r--]); while(l<Q[i].l) del(A[l++]); if((mod=Q[i].p)==1) continue;//答案一定为 0 ans[Q[i].id]=query(Q[i].r-Q[i].l+1); } for(int i=1;i<=m;i++) write(ans[i],'\n'); return fwrite(obuf,1,wrp-obuf,stdout),0; } /* 经典结论:桶的桶只有根号个 */ ``` :::