Hdoj 1787 GCD Again (Euler function)

Source: Internet
Author: User
Tags greatest common divisor

GCD Again

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2611 Accepted Submission (s): 1090


problem DescriptionDoes spent some time to think and try to solve those unsolved problem after one ACM contest?
No? Oh, you must does this if you want to become a "Big cattle".
Now you'll find the This problem are so familiar:
The greatest common divisor GCD (A, b) of both positive integers a and B, sometimes written (a, b), is the largest divisor Common to A and B. For example, (1, 2) = 1, (12, 18) =6. (A, B) can is easily found by the Euclidean algorithm. Now I am considering a little more difficult problem:
Given An integer N, please count the number of the integers M (0<m<n) which satisfies (n,m) >1.
This was a simple version of the problem "GCD" which you had done with a contest RECENTLY,SO I name This problem "GCD Again". If you cannot solve it still,please take a good think about your method of study.
Good luck!
 
InputInput contains multiple test cases. Each test case is contains an integers N (1<n<100000000). A test case containing 0 terminates, the input and this test are not processed.
 
Outputfor each integers N should output the number of integers M on one line, and with one line of output for each line In input.
 
Sample Input
240
 
Sample Output
01
  Knowledge Reserve: I. Concept of coprime:

1. Definition

Coprime ( relatively primeì) is also called the mutual vegetarian. If the maximum common factor of n integers is 1, it is called the n integer coprime.

For example, the maximum common factor of 8,10 is 2, not 1, so it is not an integer coprime.

The maximum common factor of 7,10,13 is 1, so this is an integer coprime.

5 and 5 are not coprime, since the male factor of 5 and 5 is 1, 5.

1 and any number are exponentially related, but and any number are coprime. Because the factor of 1 is only 1, and the principle of Inma is: As long as two of the public factor is only 1 o'clock, it is said that two number is Inma. 1 has only one factor (so 1 is neither prime (prime number) nor composite), 1 and the other number of other common factor can no longer be found, so 1 and any number are coprime (except 0).

Inma: such as C and M coprime, then writing (c,m) = 1.

The math textbook for primary schools defines Inma as: "The number of conventions is only 1 of two numbers, called Inma." ”

The "two numbers" mentioned here refers to the natural numbers.

"The number of conventions is only 1" and cannot be erroneously stated as "without the number of conventions." ”

Two. Euler functions:

1. Definition:

for a positive integer n, the Euler function is less than or equal to the number of n coprime in the number of numbers. For example Euler (8) = 4, because 1,3,5,7 are both and 8 coprime.

2. Description:

Euler function Expression formula: Euler (x) =x (1-1/P1) (1-1/P2) (1-1/P3) (1-1/P4) ... (1-1/PN), where P1,P2......PN is the all-factor of x, and X is an integer that is not 0. Euler (1) =1 (the number of unique and 1 coprime is 1 itself).


The extension of Euler's formula: the sum of all the mass factors of a number is Euler (n) *n/2.

Test instructions: gcd (i,n) less than n is more than 1 of the number of ideas: Euler function directly for gcd (i,n) ==1 the number of n minus, attention is less than n, so minus 1. The specific code is as follows:
#include <stdio.h>int Euler (int n)//Euler function {int res=n,i;for (i=2;i*i<=n;i++) {if (n%i==0)   res=res/i* (i-1); /First Division is to prevent overflow of intermediate data while (n%i==0)   n/=i;//guaranteed x must be prime}if (n>1)   res=res/n* (n-1); return res;} int main () {int n;while (scanf ("%d", &n) &&n!=0)    printf ("%d\n", N-euler (N)-1);//The topic requires less than n, so subtract 1 return 0 ;}


Hdoj 1787 GCD Again (Euler function)

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.