Description
给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的
数对(x,y)有多少对.
Input
一个整数N
Output
如题
Sample Input
4
Sample Output
4
HINT
hint
对于样例(2,2),(2,4),(3,3),(4,2)
1<=N<=10^7
Source
湖北省队互测
题目真是简单粗暴…
∑p是素数∑i<=n∑j<=ngcd(i,j)=p
=∑p是素数∑i<=n/p∑j<=n/pgcd(i,j)=1
相当于问[1,n]中互质的数的对数。算个欧拉函数前缀和即可。因为(x,y)和(y,x)算两个,所以要乘2,然后要特判(1,1)。
枚举素数,然后累加,复杂度黄学长说是O(n)…反正能过
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int SZ = 10000010;
const int MAXN = 10000000;
bool vis[SZ];
int pri[SZ],phi[SZ];
LL sum[SZ];
int tot = 0;
int n;
void shai()
{
phi[1] = 1;
for(int i = 2;i <= n;i ++)
{
if(!vis[i]) pri[++ tot] = i,phi[i] = i - 1;
for(int j = 1,m;j <= tot && (m = i * pri[j]) <= n;j ++)
{
vis[m] = 1;
if(i % pri[j] == 0) { phi[m] = pri[j] * phi[i]; break; }
else phi[m] = phi[i] * phi[pri[j]];
}
}
for(int i = 1;i <= n;i ++)
sum[i] = sum[i - 1] + phi[i];
}
LL ask(int n)
{
LL ans = 0;
for(int i = 1;i <= tot && pri[i] <= n;i ++)
ans += ((LL)sum[n / pri[i]] << 1ll) - 1;
return ans;
}
int main()
{
scanf("%d",&n);
shai();
printf("%lld\n",ask(n));
return 0;
}