【bzoj2818】Gcd 欧拉函数

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

湖北省队互测


题目真是简单粗暴…

pi<=nj<=ngcd(i,j)=p

=pi<=n/pj<=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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值