Hdu 1787 GCD Again Euler's function small water Number Theory

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): 1542 Accepted Submission (s): 578


Problem Description
Do you have spent some time to think and try to solve those unsolved problem after one ACM contest?
No? Oh, you must do this when you want to become a "Big Cattle ".
Now you will find that this problem is so familiar:
The greatest common divisor GCD (a, B) of two 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 be 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 is a simple version of problem "GCD" which you have done in 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!
 

Input
Input contains multiple test cases. Each test case contains an integers N (1 <N <100000000). A test case containing 0 terminates the input and this test case is not to be processed.
 

Output
For each integers N you shoshould output the number of integers M in one line, and with one line of output for each line in input.
 

Sample Input
2
4
0
 

Sample Output
0
1
 

Author
Lcy
 

Source
2007 provincial training team exercise session (10) _ Thanks to DOOMIII
 

Recommend
Lcy
Question:
Evaluate the number of gcd (I, n) greater than 1 with less than n
Idea: Use the Euler's function to directly calculate the number of gcd (I, n) = 1 minus n.
 
[Html]
# Include <stdio. h>
# Include <math. h>
_ Int64 euler (_ int64 x) // It is the formula.
{
_ Int64 I, res = x;
For (I = 2; I <(_ int64) sqrt (x * 1.0) + 1; I ++)
If (x % I = 0)
{
Res = res/(_ int64) I * (I-1 );

While (x % I = 0) x/= I; // ensure that I must be a prime number.
}
If (x> 1) res = res/(_ int64) x * (x-1); // be careful not to overflow here
Return res;
}
 
Int main ()
{
_ Int64 n;
While (scanf ("% I64d", & n )! = EOF)
{
If (! N) break;
Printf ("% I64d \ n", n-euler (n)-1 );
}
Return 0;
}

Related Article

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.