Evaluate Euler's function values and filter prime numbers linearly

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.