题目链接:Gcd
题目大意:求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对
题目思路:先转化一下题意,变成求1 <=x,y <= N/prime[i]且Gcd(x,y) = 1的数对有多少,然后假设x >= y,y的枚举交给欧拉函数,又x,y大小不一定,所以答案乘以2,但是注意到这样一种情况,x == y的情况,这种情况答案是x本身,除开1之外都不是质数,所以不影响贡献,1 1这种情况枚举的两次,且贡献累加了两次,所以要减去一次,所以答案对于每一个素数来说就是两倍phi(x)*2-1,处理一下前缀和就好
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 1e7+10;
const int MAX = 1e6+10;
int tot,n,pri[MAX],phi[MAXN];
ll sum[MAXN],ans;
bool mark[MAXN];
void getphi()
{
phi[1]=1;
for(int i=2;i<=n;i++)
{
if(!mark[i]){phi[i]=i-1;pri[++tot]=i;}
for(int j=1;j<=tot;j++)
{
int x=pri[j];
if(i*x>n)break;
mark[i*x]=1;
if(i%x==0){phi[i*x]=phi[i]*x;break;}
else phi[i*x]=phi[i]*phi[x];
}
}
}
int main(){
while(~scanf("%d",&n)){
tot = 0;
getphi();
for(int i = 1;i <= n;i++) sum[i] = sum[i-1]+phi[i];
for(int i = 1;i <= tot;i++) ans += sum[n/pri[i]]*2-1;
printf("%lld\n",ans);
}
return 0;
}