Gcd (x, y) (1 <= X, Y <= N) is the number of prime numbers (x, y) and (Y, X ).
<=> Gcd (x/K, Y/K) = 1. k is the number of prime factors of X.
<=> Σ PHI (x/K) (1 <= x <= n, k is the prime factor of X)
This complexity is unacceptable,
Then, we can consider enumerating K and calculating Σ PHI (Q/K) (k is the prime number within N, and Q is a multiple of K within N), that is, Σ [PHI (1) + PHI (2) + PHI (3) +... + PHI (p)] (P = N/K)
Prefix of PHI and can be preprocessed in rough.
However, (x, y) and (Y, x) are different. Therefore, when calculating the prefix and sum, we must multiply 2 and accumulate the numbers of (x) (x = 1, that is, Σ [PHI (1) + PHI (2) * 2 + PHI (3) * 2 +... + PHI (p) * 2] (P = N/K ).
For the prime number within each n, We can get its contribution to the answer in O (1.
The compaction time complexity is calculated based on the screening prime number and pre-processing Phi, and is O (n * log (N) or O (n) [linear Screening].
1 # include <cstdio> 2 using namespace STD; 3 typedef long ll; 4 int Phi [10000001], n; 5 bool unprime [10000001]; 6 LL ans, sum [10000001]; 7 void shai_prime () 8 {9 unprime [1] = 1; 10 for (ll I = 2; I <= N; I ++) if (! Unprime [I]) 11 {12 ans + = sum [N/I]; 13 for (ll j = I * I; j <= N; j + = I) 14 unprime [J] = 1; 15} 16} 17 void phi_table () 18 {19 Phi [1] = 1; // specify PHI (1) = 1; 20 For (INT I = 2; I <= N; I ++) 21 if (! Phi [I]) // If I is a prime number (similar to the screening method) 22 for (Int J = I; j <= N; j + = I) // I must be the prime factor of J 23 {24 if (! Phi [J]) Phi [J] = J; 25 Phi [J] = Phi [J]/I * (I-1); 26} 27} 28 void init_sum () 29 {30 sum [1] = Phi [1]; 31 for (INT I = 2; I <= N; I ++) sum [I] = (LL) (PHI [I] <1) + sum [I-1]; 32} 33 int main () 34 {35 scanf ("% d", & N); phi_table (); init_sum (); shai_prime (); 36 printf ("% LLD \ n", ANS); 37 return 0; 38}
[Number Theory] [screening method for prime number] [Euler's function] bzoj2818 GCD