求\(\sum_{i=1}^n\sum_{j=1}^m[(x, y)\)为质数\(]\)
\(T \le {10}^4, 1\le n, m \le {10}^7\)
假设\(p\)为\(M\)以内的质数
\[\begin{aligned}ans&=\sum_{p\in prime}\sum_{i=1}^n\sum_{j=1}^m[(i, j) == p]\&=\sum_{p\in prime}\sum_{x=1}^{\lfloor\frac{n}{p}\rfloor}\mu(x)\lfloor\frac{n}{xp}\rfloor\lfloor\frac{m}{xp}\rfloor\end{aligned}\]
令\(T=xp\)则
\[\begin{aligned}ans&=\sum_{T=1}^n\sum_{p\in prime \;\&\&\; p|T}{\mu(\frac{T}{p})\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor}\&=\sum_{T=1}^n\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor\sum_{p\in prime \;\&\&\; p|T}\mu(\frac{T}{q}) \end{aligned}
\]
只要筛出\(G(T)=\sum_{p\in prime \;\&\&\; p|T}\mu(\frac{T}{q})\)就可以\(O(\sqrt{N})\)算出
\[ans=\sum_{T=1}^n\lfloor\frac{n}{T}\rfloor\lfloor\frac{m}{T}\rfloor G(T)\]
这题除了\(ans\)都不能开\(long\;long\),不然会\(TLE\)
void init(){
miu[1]=1;
for(int i=2; i < Maxn; i++){
if(!p[i]) p[++ptot]=i, miu[i]=-1;
for(int j=1, x; j <= ptot && (x=p[j]*i) < Maxn; j++){
p[x]=1; if(i%p[j] == 0) break; miu[x]=-miu[i];
}
}
for(int i=1; i <= ptot; i++) for(int j=p[i]; j < Maxn; j+=p[i]) G[j]+=miu[j/p[i]];
for(int i=1; i < Maxn; i++) G[i]+=G[i-1];
}
void solve(){
init(); int T=read();
while(T--){
n=read(), m=read(); ll ans=0; if(n > m) swap(n, m);
for(int l=1, r=0; r < n; l=r+1){
r=min(n/(n/l), m/(m/l));
ans+=1ll*(G[r]-G[l-1])*(n/l)*(m/l);
}
printf("%lld\n", ans);
}
}
原文地址:https://www.cnblogs.com/zerolt/p/9307569.html