题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2820
题意:给定n,m。求1<=x<=n, 1<=y<=m且Gcd(x, y)为质数的(x, y)有多少对?
思路:
int prime[N],tag[N],cnt;
int u[N],g[N];
void init()
{
int i,j,k;
u[1]=1; g[1]=0;
for(i=2;i<N;i++)
{
if(!tag[i])
{
prime[++cnt]=i;
u[i]=-1;
g[i]=1;
}
for(j=1;i*prime[j]<N;j++)
{
k=i*prime[j];
tag[k]=1;
if(i%prime[j]==0)
{
u[k]=0;
g[k]=u[i];
break;
}
u[k]=-u[i];
g[k]=u[i]-g[i];
}
}
for(i=2;i<N;i++) g[i]+=g[i-1];
}
int n,m;
i64 cal()
{
int L,R;
i64 ans=0;
for(L=1;L<=n&&L<=m;L=R+1)
{
R=min(n/(n/L),m/(m/L));
ans+=(i64)(g[R]-g[L-1])*(n/L)*(m/L);
}
return ans;
}
int main()
{
init();
rush()
{
RD(n,m);
PR(cal());
}
}