具体怎么反演的请看POPOQQQ大爷的莫比乌斯反演论文TATATAT
然后注意,当用到函数F(i)的时候再将函数筛入树状数组里。。
时间复杂度O(n log^2 n+q√n log n)
如果不自然溢出会慢很多TAT
<span style="font-size:18px;">#include "stdio.h"
#include "iostream"
#include "algorithm"
#include "stdlib.h"
#include "string.h"
#include "memory.h"
#include "math.h"
#define rep(f,a,b) for(f=a;f<=b;f++)
#define run(f,to) for(f=1;f<=to;f++)
typedef long long ll;
using namespace std;
const int N=100005,Q=20005;
inline int read(){
int v=0; char ch=getchar();
while(ch>'9'||ch<'0') ch=getchar();
while(ch<='9'&&ch>='0')
v=v*10+ch-'0',ch=getchar();
return v;
}
struct Query{
int n,m,a,f,id;
bool operator <
(const Query&q)const{
return a<q.a;
}
} ask[Q];
inline bool cmp(const Query&p,const Query&q){
return p.id<q.id;
}
struct Ans{
int f,id;
bool operator <
(const Ans&q)const{
return f<q.f;
}
} save[N];
int mobius[N],pri[N],prl;
bool flag[N]; int F[N];
int val[N],sum[N],q;
const int n=100000;
void build(){
mobius[1]=1; int i,j;
rep(i,2,n){
if(flag[i]^1){
pri[++prl]=i;
mobius[i]=-1;
}
for(j=1;i*pri[j]<=n;j++){
flag[i*pri[j]]=true;
if(i%pri[j]==0){
mobius[i*pri[j]]=0;
break;
}
mobius[i*pri[j]]=-mobius[i];
}
}
rep(i,1,n){
for (j=i;j<=n;j+=i){
F[j]+=i;
}
}
rep(i,1,n){
save[i].f=F[i]&0x7fffffff;
save[i].id=i;
}
sort(save+1,save+n+1);
}
void init(){
int i; q=read(); rep(i,1,q){
ask[i].n=read();
ask[i].m=read();
ask[i].a=read();
ask[i].id=i;
} sort(ask+1,ask+q+1);
}
inline int lowbit(int x){
return x&-x;
}
inline int asksum(int l,int r){
int ret=0;
while(l<=r){
ret+=val[r];
for(r--;r-lowbit(r)>=l;r-=lowbit(r)){
ret+=sum[r];
}
}
return ret;
}
inline void add(int p,int value){
val[p]+=value;
for(int k=p;k<=n;k+=lowbit(k)){
sum[k]+=value;
}
}
void work(){
int i,j=1,k;
rep(i,1,q){
while(ask[i].a>=save[j].f){
for (k=1;k*save[j].id<=n;k++){
add(k*save[j].id,save[j].f*mobius[k]);
}
j++;
}
int x=ask[i].n,y=ask[i].m,a=0;
if(x>y) swap(x,y); int last;
for (k=1;k<=x;k=last+1){
last=min(x/(x/k),y/(y/k));
ask[i].f+=(x/k)*(y/k)*asksum(k,last);
}
}
sort(ask+1,ask+q+1,cmp);
rep(i,1,q){
printf("%d\n",ask[i].f&0x7fffffff);
}
}
int main(){
build(); init(); work();
return 0;
}</span><span style="font-size:24px;">
</span>