2818: GCD
Question:
Given the integer N, evaluate 1 <= X, Y <= N and gcd (x, y) as the prime number
Number of pairs (x, y). 1 <= n <= 10 ^ 7
Algorithm:
Solve the problem that G = gcd (X, Y) is a prime number, and convert it into X/g, Y/g ing. Therefore, you only need to find the logarithm of the interquality in [1, N/PI] (the number of the prime numbers between Pi is 1... n ). You can enumerate pi. Here we can use linear Euler's solution. The general Euler's is O (nlognlogn ).
/* Linear prime number plus Euler's sieve method O (n) Question: given an integer N, evaluate 1 <= X, Y <= N and gcd (x, y) number of (x, y) pairs of prime numbers. it is actually a conversion problem. Find that the logarithm of gcd (x, y) = k, 1 <= X, Y <= N is equal to: Find that gcd (x, y) = 1, 1 <= X, Y <= N/K logarithm. (How many ordered pairs (x, y) exist in [1, N/K] to enable interconnectivity) Then we just need to enumerate each prime number k = prime [I, then we can use the Euler's function to find out, Σ (2 * Σ (PHI [N/prime [I])-1 ). N <10 ^ 7 Euler's function: Phi [N] represents 1 ~ Number of interclasses in N and N */typedef long ll; const int maxn = 10000000 + 10; int top, PRIMES [700000]; ll Phi [maxn]; int N; // void phi_primes () {Top = 0; Phi [1] = 1; for (INT I = 2; I <= N; ++ I) {If (! Phi [I]) {primes [top ++] = I; Phi [I] = I-1 ;}for (Int J = 0; j <top & I * primes [J] <= N; ++ J) {if (I % primes [J]) phi [I * primes [J] = Phi [I] * (primes [J]-1 ); else {Phi [I * primes [J] = Phi [I] * primes [J]; break ;}}} int main () {scanf ("% d ", & N); phi_primes (); For (INT I = 2; I <= N; ++ I) Phi [I] + = Phi [I-1]; // 1... total number of I intermediates ll ans = 0; For (INT I = 0; I <top & primes [I] <= N; ++ I) {ans + = (PHI [N/primes [I] <1)-1; // one time excluding multiple prime numbers} printf ("% LLD \ n ", ans); Return 0 ;}
Evaluate Euler's function values and filter prime numbers linearly