Los solution to a problem Valley P2568 GCD

\ (update \ \ \ 2018/11/18 \ ) modify the \ (Letex \) , the other has not changed

Intermediate I \ (AFO \) , and fell to the success of the Blue name from the red name

\ (update 2019/7/22 \) plus a lot of sentiment own school culture lesson


There is a saying I heard only number theory \ (GCD \) , so I opened this question, but found that this problem is not so simple

So I would write a solution to a problem(Really more nonsense)

topic


Introduce a thing: Euler function

Definition: positive integer \ (n-\) , less than or equal \ (n-\) , and with (\ n-) \ number of positive integers coprime (including 1), referred to as \ (φ (n) \) . ( \ ([Phi] (. 1). 1 = \) )

Mathematical formula is written \ (φ (n) = \ displaystyle \ sum_ {i = 1} ^ n1 (gcd (i, n) = 1) \)

So what's the answer?

We consider each prime number \ (p \) contribution to the answer, for every prime number \ (p \)

\ (gcd (x, y) = 1 \) is equivalent to \ (gcd (a * p, b * p) = p \)

So \ (gcd (x, y) \) of \ (P \) number is the number of \ (1 <= a, b <= \ frac {n} {p} \) are relatively prime to \ ( a, b \) number, which might make \ (a <= b \)

For each \ (b, a \) have \ (φ (b) \) th argument makes \ (a, b \) prime, and \ (gcd (a * p, b * p) = p \)

Therefore, the answer to this question is \ (\ displaystyle \ sum_ {i = 1} ^ n φ (i) \)


Start with some \ (φ (n) \) character:

Multiplicative function (common with)

​ $ε(n)=[n==1]        d(n)=\displaystyle\sum_{d|n}1       σ(n)=\displaystyle\sum_{d|n}d $

​ $ μ(n)=[max(c_1,c_2,...c_m)<=1]*(-1)^m$

Of course, there \ (φ (n) \) myself

Properties of the product of the function: if \ (gcd (a, b) == 1 \) then \ (f (a \ times b ) = f (a) \ times f (b) \)

\ (φ (n) \) Other properties

1. If \ (P \) is a prime number \ (φ (p) = p -1 \)

2. If \ (p | n \) and \ (P ^ 2 | n-\) , then \ (φ (n) = φ (n / p) \ times p \)

3. If \ (p | n \) but does not satisfy the \ (P ^ 2 | n-\) , then \ (φ (n) = φ (n / p) \ times (p-1) \)

​ 4.\(\displaystyle\sum_{d|n}φ(d)=n\)


So now the problem is how to quickly converted to seek \ (φ (n) \)

Euler sieve can be associated, as may (O (n) \) \ determined prime number within all the time.

If for \ (I \) , if it is known all \ (1-i \) a \ ([Phi] \) , enumeration \ (<= i \) a prime number \ (P [J] \) , can be determined \ (φ (x \ times p [j]) \)

1. If \ (p [j] \) and \ (X \) prime \ (φ (x * p [ j]) = φ (x) * φ (p [j]) \)

2. If not prime, provided \ (x = t * p [ j] ^ k \)

\ (z (x * p [j]) = z (t * p [j] ^ {k + 1}) = z (t) * F (p [j] ^ {k + 1}) \) \ ( = f (t) * F (p [j] ^ k) * p [j] = f (x) * p [j] \)

Therefore, pretreatment \ (φ (i) \) code:

    for (int i=2;i<=n;++i){
        if (is_prime[i]) phi[i]=i-1,prime[++prime_num]=i;
        for (int j=1;j<=prime_num&&prime[j]*i<=n;++j){
            is_prime[prime[j]*i]=0;
            if (__gcd(prime[j],i)==1) phi[prime[j]*i]=phi[prime[j]]*phi[i];
                else phi[prime[j]*i]=prime[j]*phi[i];
            if (i%prime[j]==0) break;
        }
    }

Prefix and do something on it

Finally, a word of warning, \ (10 \) in \ (OI \) get the job, do not open \ (long \ long \) See ancestors


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=10000005;
bool is_prime[N];
int n,prime_num,prime[N],phi[N];
long long sum[N];
int main(){
    scanf("%d",&n);
    memset(is_prime,1,sizeof(is_prime));
    is_prime[1]=0;phi[1]=1;
    for (int i=2;i<=n;++i){
        if (is_prime[i]) phi[i]=i-1,prime[++prime_num]=i;
        for (int j=1;j<=prime_num&&prime[j]*i<=n;++j){
            is_prime[prime[j]*i]=0;
            if (__gcd(prime[j],i)==1) phi[prime[j]*i]=phi[prime[j]]*phi[i];
                else phi[prime[j]*i]=prime[j]*phi[i];
            if (i%prime[j]==0) break;
        }
    }
    for (int i=1;i<=n;++i) sum[i]=sum[i-1]+phi[i];
    long long ans=0;
    for (int i=1;i<=prime_num&&prime[i]<=n;++i) 
        ans+=(sum[n/prime[i]]<<1)-1;
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zhouykblog/p/11227818.html