Gcd II

Title Description

Given an integer N, seeking 1 <= x, y <= N and Gcd (x, y) is a number of primes (x, y) the number of pairs.

Entry

An integer N (1≤N≤10 ^ 7)

Export

Sample input

4

Sample Output

4

prompt

For the sample (2,2), (2,4), (3,3), (4,2)

#include <bits/stdc++.h>

using namespace std;

const int N=1e7+1;
typedef long long ll;
int phi[N],prime[780000],n,tot;
ll ans,sum[N];
bool vis[N+1];
void get_euler(int n)
{
    phi[1]=1;
    for (int i=2; i<=n; i++)
    {
        if (!vis[i])
        {
            prime[tot++]=i;
            phi[i]=i-1;
        }
        for (int j=0; j<tot&&1ll*prime[j]*i<=n; j++)
        {
            vis[prime[j]*i]=1;
            if (i%prime[j]==0)
            {
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }
            phi[i*prime[j]]=phi[i]*phi[prime[j]];
        }
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    get_euler(n);
    for (int i=1;i<=n;i++){
        sum[i]=sum[i-1]+phi[i];
    }
    for (int i=0;i<tot;i++){
        ans+=sum[n/prime[i]]*2-1;
    }
    printf("%lld\n",ans);
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/Accpted/p/11335106.html
gcd
ii