把博客园图标替换成自己的图标
把博客园图标替换成自己的图标end

luogu P3312 [SDOI2014]数表

题面传送门
我也不知道这个怎么一只根号一只log过1e5的……
首先我们不考虑\(a\)这个限制。
\(F(x)\)\(x\)的约数和,这个可以枚举倍数\(O(nlogn)\)解决。
那么显然有式子\(\sum\limits_{i=1}^{n}{\sum\limits_{j=1}^{m}{F_{gcd(i,j)}}}\)
提前枚举\(gcd\)就有\(\sum\limits_{k=1}^{k}{F(k)\sum\limits_{i=1}^{\lfloor \frac{n}{k}\rfloor}{\sum\limits_{j=1}^{\lfloor \frac{m}{k}\rfloor}{[gcd(i,j)==1]}}}\)
随便反演一下就是
\(\sum\limits_{k=1}^{k}{F(k)\sum\limits_{d=1}^{\lfloor \frac{n}{k}\rfloor}{\mu(d)\lfloor \frac{n}{kd}\rfloor\lfloor \frac{m}{kd}\rfloor}}\)
\(T=kd\),内层枚举\(k\)\(\sum\limits_{T=1}^{n}{\lfloor \frac{n}{T}\rfloor\lfloor \frac{m}{T}\rfloor\sum\limits_{k|T}{F(k)\mu(\frac{T}{k})}}\)
那么你对于每个询问排序,然后对于每个\(F(k)\)排序,然后查询的时候树状数组维护一下偏序即可。时间复杂度\(O(q\sqrt nlogn+nlog^2n)\)
code:

#include <vector>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#define I inline
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define l(x) x<<1
#define r(x) x<<1|1
#define re register
#define ll long long
#define db double
#define N 100000
#define eps (1e-5)
#define mod (1<<31)
#define U unsigned int
using namespace std;
int T,Maxn,fl[N+5],pr[N+5],ph,r=1;U mu[N+5],D[N+5],G[N+5],Ans[N+5];
struct ques{int n,m,a,id;}Q[N+5];
struct Di{U D;int id;}F[N+5];
I bool cmp(ques x,ques y){return x.a<y.a;}
I bool cmp1(Di x,Di y){return x.D<y.D;}
I void swap(int &x,int &y){x^=y^=x^=y;}
I void get(int x,U w){while(x<=Maxn) G[x]+=w,x+=x&-x;}
I U find(int x){U ans=0;while(x) ans+=G[x],x-=x&-x;return ans;}
I void insert(int x){re int i;for(i=x;i<=Maxn;i+=x) get(i,D[x]*mu[i/x]);}
int main(){
	freopen("1.in","r",stdin);
	re int i,j,h;scanf("%d",&T);for(i=1;i<=T;i++) Q[i].id=i,scanf("%d%d%d",&Q[i].n,&Q[i].m,&Q[i].a),Q[i].n<Q[i].m&&(swap(Q[i].n,Q[i].m),0),Maxn=max(Maxn,Q[i].n);
	for(sort(Q+1,Q+T+1,cmp),mu[1]=1,i=2;i<=Maxn;i++){
		!fl[i]&&(pr[++ph]=i,mu[i]=-1);for(j=1;j<=ph&&i*pr[j]<=Maxn;j++){fl[i*pr[j]]=1;if(i%pr[j]==0) break;mu[i*pr[j]]=-mu[i];}
	}
	for(i=1;i<=Maxn;i++){
		for(j=i;j<=Maxn;j+=i) D[j]+=i;F[i]=(Di){D[i],i};
	}
	sort(F+1,F+Maxn+1,cmp1);
	for(i=1;i<=T;i++){
		while(r<=Maxn&&F[r].D<=Q[i].a) insert(F[r].id),r++; 
		for(j=1;j<=Q[i].m;j=h+1)
		h=min(Q[i].n/(Q[i].n/j),Q[i].m/(Q[i].m/j)),Ans[Q[i].id]+=(find(h)-find(j-1))*(Q[i].m/j)*(Q[i].n/j);
	}
	for(i=1;i<=T;i++)printf("%u\n",Ans[i]%mod);
}
posted @ 2021-06-08 20:47  275307894a  阅读(32)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end