Number Theory-Euler's function template question --- poj 2407: relatives

Source: Internet
Author: User
Relatives
Time limit:1000 ms   Memory limit:65536 K
Total submissions:11372   Accepted:5544

Description

Given n, a positive integer, how many positive integers less than N are relatively prime to n? Two integers A and B are relatively prime if there are no integers x> 1, Y> 0, z> 0 such that a = xy and B = xz Z.

Input

There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.

Output

For each test case there shocould be single line of output answering the question posed above.

Sample Input

7120

Sample output

64

Source

Waterloo local 2002.07.01

 

Mean: 

Enter a positive integer n to calculate the number of the numbers that are less than N and that are n.

Analyze:

The bare Euler's function has weak data and uses templates directly.

 

The Euler's function PHI (n) represents the number of positive integers (including 1) that are smaller than N and interlace with N ). For example:
Phi (1) = 1; PHI (2) = 1; PHI (3) = 2; PHI (4) = 2 ;... phi (9) = 6 ;...

 

The method for calculating the Euler's function of a number is as follows:

1) decomposition of this number. prime factor: N = p1 ^ k1 + p2 ^ k2 + P3 ^ K3 + P4 ^ k4 ....

2) PHI (n) = (P1 ^ K1-P1 ^ (K1-1) * (P2 ^ K2-P2 ^ (K2-1 ))*... * (PN ^ kn-PN ^ (kn-1 ))
= Mult {PI ^ Ki-Pi ^ (Ki-1 )};

The verification process is as follows:
1. It is easy to think: WHEN n is a prime number, Phi (n) = n-1. Because each positive integer smaller than N is equal to n. When N is the k power of prime p, Phi (n) = P ^ K-P ^ (k-1 ). Because the positive integers between 1 and N are only multiples of P and N are not mutually exclusive, such numbers have (P ^ K/P.
2. If m and n are mutually Prime, that is, gcd (m, n) = 1, then PHI (M * n) = PHI (m) * PHI (n ). We can use the Chinese Remainder Theorem to prove that the idea is to establish such a one-to-one correspondence relationship (a, B) <-> X, where the positive integer a is smaller than m and gcd (, m) = 1, positive integer B less than N and gcd (B, n) = 1, positive integer x less than M * n and gcd (M * n, x) = 1. The verification process is as follows:
1) according to the Chinese Remainder Theorem, if m and n are mutually unique, then X % m = A, X % N = B (0 <= A <m, 0 <= B <n), where 0 <= x <m * n exists and only one solution exists. It is easy to prove that if two such equations have the same M, N but a and B, their solutions x must be different.
2) First, use the arc method to prove that gcd (m, a) = 1 and gcd (n, B) = 1 are necessary conditions for gcd (M * n, x) = 1: assume that gcd (a, m) = k> 1, and a = A' * k is obtained; M = M' * k => X = K' * m + A = K' * K * m' + K * a' = K * (K' * m' + '); therefore, gcd (x, m) = k> 1. Likewise, if gcd (B, n)> 1, then gcd (x, n)> 1. Therefore, it is necessary for a and M * n to communicate with each other and B and N to communicate with each other.
3) Next we will prove the adequacy: from X % m = A, we can get x = K * m + A; from the Euclidean algorithm, we can find the process of maximizing the common number (no proof, huh, huh, you still have to think about it.) You can know that gcd (x, m) = gcd (m, a) = 1; likewise, if gcd (n, B) = 1, then gcd (x, n) = 1. Next we can easily obtain: gcd (M * n, x) = 1. This proves the adequacy.
4) The conclusion in the above three steps shows that the number pair (a, B) can have a one-to-one correspondence with X, so how many different (A, B) are there ), the number of different X.
3. After Dividing N into the product of prime numbers, it is clear that for any I, j (I! = J) the formula above is acceptable because PI ^ KI and PJ ^ kJ are mutually unique.

According to the above formula, we can obtain the recursive relationship of the Euler's function:
Assuming that prime p can divide N
If P can divide N/P, Phi (n) = PHI (N/P) * P;
If P cannot divide N/P, Phi (n) = PHI (N/P) * (p-1 );

 

Time Complexity:O (N)

 

Source code:

 

// Memory time // 1347 K 0 Ms //: snarl_jsb // 2014-09-12-21.18 # include <algorithm> # include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue >#include <stack> # include <map> # include <string> # include <climits> # include <cmath> # define n 1000010 # define ll long longusing namespace STD; int gcd (int A, int B) {return B? Gcd (B, A % B): A;} inline int lcm (int A, int B) {return a/gcd (a, B) * B ;} int Eular (int n) /// evaluate 1 .. n-1: the number of the numbers of mutex-N {int ret = 1, I; for (I = 2; I * I <= N; I ++) if (N % I = 0) {n/= I, RET * = I-1; while (N % I = 0) N/= I, RET * = I ;} if (n> 1) RET * = n-1; return ret;} int main () {// freopen ("C: \ Users \ Asus \ Desktop \ cin. CPP "," r ", stdin); // freopen (" C: \ Users \ Asus \ Desktop \ cout. CPP "," W ", stdout); int N; while (~ Scanf ("% d", & N), n) {int ans = Eular (n); cout <ans <Endl;} return 0 ;}

  

Number Theory-Euler's function template question --- poj 2407: relatives

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.