P2568-GCD【欧拉函数,欧拉筛】

正题

题目链接: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,yn)


解题思路

首先对于
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=1nφ(i))21
枚举就好了


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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值