Euler function --HYSBZ - 2818

Topic Link

Calculating gcd (x, y) is a prime number (x, y) of the number of

First, there is a necessary equation, gcd (x, y) = a prime number p => gcd (x / p, y / p) = 1

Therefore, if y> x, y is determined in a case, the number of all the numbers satisfying y / p coprime, i.e., y / p value of Euler function

So we can first screen again Euler function, and seek prefix and convenient after use

这样 sum [n / p] = non [1] + fees [2] + ... + fees [y / p] + ... + fees [n / p]

When you have prayed on behalf of Let y> x, gcd = p log of all

Then there are x> y case, it is sum [n / p] * 2

And then also note phi [1] this thing

It represents gcd (p, p) = p, though you seek phi [p] time will not count it, but you find that the phi [p / p]

phi here [1] is the default is 1, indicating that you are overcharged each prime time

Topic Code

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long LL;
const int maxn=1e7+7;
int phi[maxn],prime[maxn];
LL sum[maxn];
bool check[maxn];
int cnt=0;
void euler(){
    phi[1]=1;
    sum[0]=0;
    sum[1]=1;
    for(int i=2;i<maxn;i++){
        if(!check[i]){
            prime[cnt++]=i;
            phi[i]=i-1;
        }
        for(int j=0;j<cnt&&i*prime[j]<maxn;j++){
            check[i*prime[j]]=true;
            if(i%prime[j])
                phi[i*prime[j]]=phi[i]*(prime[j]-1);
            else {
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }
        }
        sum[i]=sum[i-1]+phi[i];
    }
}
LL n;
int main(){
    euler();
    scanf("%lld",&n);
    LL ans=0;
    for(int i=0;i<cnt&&prime[i]<=n;i++){
        ans+=2*sum[n/prime[i]];
        ans--;
    }
    printf("%lld\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/helman/p/11354161.html