O - GCD - Extreme (II) (Eulerian function)

Topic link: https://vjudge.net/problem/UVA-11426

 

Roughly the meaning of the title: Let’s find a number G that satisfies a,b<=n,a<b,G+=gcd(a,b)

 

General idea: According to the Euler function, a, b are relatively prime (a<b), so gcd(a,b)=1, and gcd(2*a,2*b)=2, gcd(3*a,3 *b)=3...gcd(n*a,n*b)=n

 

 

 

#include <iostream>
#include <cstring>

using namespace std;

const long long maxn=4000001+100;

int n;
long long phi[maxn],a[maxn];

void Phi()
{
    memset(a,0,sizeof(a));
    for(int i=1;i<=maxn;i++) phi[i]=i;

    for(int i=2;i<=maxn;i++)
    {
        if(phi[i]==i)
        {
            for(int j=i;j<=maxn;j+=i)
            {
                phi[j] =phi[j]/i*(i- 1 );
            }
        }
        for(int j=1;j*i<=maxn;j++)
        {
            a[j *i]+=j* phi[i];
        }
    }
    for(int i=1;i<=maxn;i++)
    {
        a[i]+=a[i-1];
    }
}

intmain ()
{
    Phi();

    while(cin>>n&&n!=0)
    {
        cout<<a[n]<<endl;
    }

    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325102512&siteId=291194637
gcd