题意:给定n,求gcd(x,y)==p 的对数,其中(1<=x<y<n)
 
思路:
 求(x, y) = k, 1 <= x, y <= n的对数等于求(x, y) = 1, 1 <= x, y <= n/k的对数!所以,枚举每个质数p(线性筛素数的方法见:线性时间内筛素数和欧拉函数),然后求(x, y) = 1, 1 <= x, y <= n/p的个数。
 那(x, y) = 1的个数如何求呢?其实就是求互质的数的个数。在[1, y]和y互质的数有phi(y)个,如果我们令x < y,那么答案就是sigma(phi(y))。因为x, y是等价的,所以答案*2,又因为(1, 1)只有一对,所以-1。最终答案为sigma(sigma(phi(n/prime[i])) * 2 - 1)。
参考:https://oi.abcdabcd987.com/eight-gcd-problems/
 
code:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <set>
#include <cmath>
#include <vector>
using namespace std;
using namespace std;
typedef long long ll;
const int N = 10000005;
const ll maxn=1e18+100;
int n,len;
int p[N],phi[N];
bool vis[N];int main()
{scanf("%d",&n);len=0;for (int i=2;i<=n;i++){if (!vis[i]){p[len++]=i;phi[i]=i-1;}for (int j=0;j<len&&i*p[j]<=n;j++){vis[i*p[j]]=1;if (i%p[j]) phi[i*p[j]]=phi[i]*(p[j]-1);else{phi[i*p[j]] = phi[i] * p[j];break; }}}phi[1]=1;for (int i=2;i<=n;i++) phi[i]+=phi[i-1];ll ans=0;for (int i=0;i<len;i++) ans+=phi[n/p[i]]*2-1;printf("%lld",ans);
}