正题
题目链接:https://www.luogu.org/problemnew/show/P2568
题目大意
求有多少个数对满足 g c d ( x , y ) = p r i ( x , y ≤ n ) gcd(x,y)=pri(x,y\leq n) gcd(x,y)=pri(x,y≤n)
解题思路
首先对于
g
c
d
(
x
,
y
)
=
p
gcd(x,y)=p
gcd(x,y)=p
=
>
g
c
d
(
x
/
p
,
y
/
p
)
=
1
=>gcd(x/p,y/p)=1
=>gcd(x/p,y/p)=1
那么对数就是
(
∑
i
=
1
n
φ
(
i
)
)
∗
2
−
1
(\sum_{i=1}^n \varphi(i))*2-1
(i=1∑nφ(i))∗2−1
枚举就好了
c o d e code code
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const ll N=1e7+100;
ll n,phi[N],pri[N],tot,ans;
int main()
{
scanf("%lld",&n);
phi[1]=1;
for(ll i=2;i<=n;i++)
if(!phi[i]){
pri[++tot]=i;
for(ll j=i;j<=n;j+=i){
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]/i*(i-1);
}
}
for(ll i=1;i<=n;i++)
phi[i]+=phi[i-1];
for(ll i=1;i<=tot;i++)
ans+=phi[n/pri[i]]*2-1;
printf("%lld",ans);
}