[SDOI2014]数表
作者:互联网
- 题意:数表
有一个表,其中\(val(i,j)=\sigma(gcd(i,j))\)给出\(n,m,a\)求所有\(val\)不超过\(a\)的和。 思路:
假如没有\(a\)的限制:
\(\sum\limits_{d=1}^n\sigma(d)\sum\limits_{i=1}^n\sum\limits_{j=1}^m[gcd(i,j)==d]\)
\(\sum\limits_{d=1}^n\sigma(d)\sum\limits_{i=1}^{\lfloor\frac{n}{d}\rfloor}\sum\limits_{j=1}^{\lfloor\frac{m}{d}\rfloor}[gcd(i,j)==d]\)
\(\sum\limits_{d=1}^n\sigma(d)\sum\limits_{k=1}^{\lfloor\frac{n}{d}\rfloor}\lfloor\frac{n}{k*d}\rfloor\lfloor\frac{m}{k*d}\rfloor\mu(k)\)
令\(T=k*d\)
\(\sum\limits_{T=1}^n\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\sum\limits_{d|T}\sigma(d)\mu(\frac{T}{d})\)为什么不把\(val\)带进去枚举\(gcd(i,j)\)的因数,因为我们还是要保留完整的\(\sigma\),才能处理后面的\(a\)
令\(g(T)=\sum\limits_{d|T}\sigma(d)\mu(\frac{T}{d})\)
发现这里跟询问的\(n,m\)无关,于是考虑离线按\(a\)排序动态加入\(sigma(d)\),更新\(g(T)\)枚举倍数\(T\)即可。加入复杂度也是\(O(nlogn)\)不过考虑到数论分块要求前缀和+修改我们便用树状数组维护\(g()\),复杂度\(O(nlog^2n)\)。
- code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
const ll mod=(1ll)<<31;
ll as[N],sg[N],sm[N],c[N],g[N];
int ptot,p[N],miu[N];
bool is_p[N];
struct node {int n,m,a,id;}Q[N];
struct data {int p;ll val;}P[N];
bool cmp2(data u,data v) {return u.val<v.val;}
bool cmp(node u,node v) {return u.a<v.a;}
int lowbit(int u) {return u&(-u);}
void Update(int u,ll w) {for(;u<N;u+=lowbit(u))c[u]+=w;}
ll Sum(int u) {ll res=0;for(;u;u-=lowbit(u))res=(res+c[u])%mod;return res;}
void _xxs() {
is_p[1]=miu[1]=sg[1]=sm[1]=1;
for(int i=2;i<N;i++) {
if(!is_p[i])p[++ptot]=i,sg[i]=1,sm[i]=i+1,miu[i]=-1;
for(int j=1,x;j<=ptot&&(x=i*p[j])<N;j++) {
is_p[x]=1;
if(i%p[j]==0) {miu[x]=0;sg[x]=sg[i];sm[x]=sm[i]*p[j]+1;break;}
miu[x]=miu[i]*-1;sg[x]=sg[i]*sm[i];sm[x]=p[j]+1;
}
}
for(int i=2;i<N;i++) sg[i]=sg[i]*sm[i]%mod;
// for(int i=1;i<=10;i++) printf("%lld ",sg[i]);puts("");
}
void init() {
for(int i=1;i<N;i++)P[i]=(data){i,sg[i]};
sort(P+1,P+N,cmp2);
}
int main() {
_xxs();init();
int q;scanf("%d",&q);
for(int i=1;i<=q;i++)scanf("%d%d%d",&Q[i].n,&Q[i].m,&Q[i].a),Q[i].id=i;
sort(Q+1,Q+1+q,cmp);
int j=0;
for(int i=1;i<=q;i++) {
int n=Q[i].n,m=Q[i].m,a=Q[i].a;
if(n>m)swap(n,m);
while(j<N&&P[j+1].val<=a) {
j++;ll phi=P[j].val,x=P[j].p;
for(int k=x;k<N;k+=x) Update(k,phi*miu[k/x]%mod);
}
ll ans=0;
for(int l=1,r;l<=n;l=r+1) {
r=min(n/(n/l),m/(m/l));
ans=(ans+1ll*(n/l)*(m/l)%mod*(Sum(r)-Sum(l-1)))%mod;
}
as[Q[i].id]=(ans+mod)%mod;
}
for(int i=1;i<=q;i++)printf("%lld\n",as[i]);
return 0;
}
标签:lfloor,frac,limits,sum,rfloor,SDOI2014,数表,sigma 来源: https://www.cnblogs.com/bestime/p/15870600.html