Pku2480 (Application of Euler's function, push formula, product function)

Source: Internet
Author: User

Http: // 162.105.81.212/judgeonline/problem? Id = 2480

Given N (INT), calculate Σ gcd (I, n) 1 <= I <= n.

Analysis: when we look at the data of this question, we know that it cannot be violent. We only need to use the Euler's Function Decomposition, but our number theory is not mature and we haven't been able to solve it for a long time;

Finally, I understood it only after reading the report on solving the problem;

This solution: For gcd (m, n) = I, there is a ci m. The answer to this formula is sigma (CI * I) gcd (m, n) = I <=> gcd (M/I, n/I) = 1 and calculate GCD (M/I, n/I) = 1. How many M/I values meet this is the definition of the Euler's function PHI (), so it is converted to Phi (N/I) enumeration of each M | n for Phi (N/I) The answer is Σ (PHI (N/I) * I). How can we enumerate each M | n?

It is easy to enumerate all integers from 1 to SQRT (N). All the approximate numbers are I | N and (n/I) | n.

# Include <iostream> <br/> # include <cmath> <br/> using namespace STD; <br/> bool s [50005]; <br/> int prime [50005]; <br/> void prime () // returns the prime number. <br/> {<br/> int I, J; <br/> for (I = 2; I <50005; I ++) <br/> If (! S [I]) <br/>{< br/> for (j = I <1; j <50005; j + = I) <br/> S [J] = 1; <br/>}< br/> for (I = 2; I <50005; I ++) <br/> If (! S [I]) <br/> prime [++ prime [0] = I; <br/>}< br/> int Euler (INT X) // evaluate the Euler's function value. <br/>{< br/> int I, Res = x; <br/> for (I = 1; I <= prime [0] & prime [I] <(INT) SQRT (x * 1.0) + 1; I ++) <br/> If (X % prime [I] = 0) <br/>{< br/> res = Res/prime [I] * (Prime [I]-1 ); <br/> while (X % prime [I] = 0) x/= prime [I]; <br/>}< br/> If (x> 1) res = Res/x * (X-1); <br/> return res; <br/>}< br/> int main () <br/>{< br/> int N, I, j; <br />__ Int64 ans; <br/> prime (); <br/> while (scanf ("% d", & N )! = EOF) <br/>{</P> <p> ans = 0; <br/> for (I = 1; I <= (INT) SQRT (N * 1.0); I ++) // optimized; <br/>{< br/> If (N % I = 0) <br/> {<br/> ans + = (N/I) * (_ int64) Euler (I); <br/> If (n! = I * I) <br/> ans + = I * (_ int64) Euler (N/I ); <br/>}< br/> printf ("% i64d/N", ANS); <br/>}< br/> return 0; <br/>} 

Other solutions are product functions and factorization;

 

(1) because of its nature

At that time

Therefore, gcd is a product function.

Because the sum of product functions is also a product function (p182 in elementary number theory and its application ),

Is a product function

The problem is simplified to the prime number stage and must be obtained. The Euler's formula is used in this case.

(It is easy to see because it represents the number of GCD numbers ranging from 1 to n and N with the largest common approx. d)

So, because it can be obtained.

(2)

 

It is easy to prove that gcd (I, n) is a product function, that is, if n = m1 * m2 and gcd (I, M1 * m2) = gcd (I, M1) * gcd (I, m2 ). then, according to the specific mathematical conclusion: the sum of the product functions is also the product, so if we want to answer f (N), then: F (n) = f (M1) * (m2) where m1 * M2 = N and M1 and M2 are mutually qualitative! After factorization, the product can be used to multiply all results to obtain the final answer as long as F (P ^ K) is obtained.

Another conclusion is that F (n) = sum (p * PHI (N/P) where p is the factor of N, Phi (N/P) the number of GCD numbers from 1 to n is P, which is a good proof.

So F (P ^ K) is converted to Phi (P ^ I) I = 0 .... k; and according to the formula PHI (P ^ I) = (p-1) * P ^ (I-1) can be obtained, so the whole problem is solved. In the specific algorithm process, prime numbers 1 to 50000 must be obtained to facilitate factorization.

 

 

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.