http://www.lydsy.com/JudgeOnline/problem.php?id=1101
给定a,b,d,求有多少gcd(x,y)==d(1<=x<=a&&1<=y<=b)
思路:
Σgcd(x,y)==d (1<=x<=a,1<=y<=b)
=
Σgcd(x,y)==1 (1<=x<=a/d,1<=y<=b/d)
令G(i)=num(i|gcd(x,y))=n/i*m/i
g(i)=num(i=gcd(x,y))
g(i)=ΣG(d)*u(d/i) (i|d)
则答案就是g(1)
g(1)=ΣG(i)*u(i) (1<=i<=min(n,m))
=Σ(n/i)*(m/i)*u(i)
因此做出u(i)的前缀和,这样就可以一起处理n/i和m/i相同的i
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
int mul[],mark[],sum[],p[];
int read(){
char ch=getchar();int t=,f=;
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void init(){
mul[]=;
for (int i=;i<=;i++){
if (!mark[i]){
p[++p[]]=i;
mul[i]=-;
}
for (int j=;j<=p[]&&i*p[j]<=;j++){
mark[p[j]*i]=;
if (i%p[j]) mul[p[j]*i]=mul[i]*(-);
else {
mul[p[j]*i]=;
break;
}
}
}
sum[]=;
for (int i=;i<=;i++)
sum[i]=sum[i-]+mul[i];
}
int cal(int a,int b){
if (a>b) std::swap(a,b);
int res=,n=a,m=b;
for (int i=,j;i<=a;i=j+){
j=std::min(n/(n/i),m/(m/i));
res+=(a/i)*(b/i)*(sum[j]-sum[i-]);
}
return res;
}
int main(){
int Q=read();
init();
while (Q--){
int a=read(),b=read(),d=read();
printf("%d\n",cal(a/d,b/d));
}
}