HDU 1787 GCD Again

GCD Again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2109    Accepted Submission(s): 833

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 should 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
 
Recommend
lcy
 
思路:欧拉函数,在数论中,对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目。此函数以其首名研究者欧拉命名,它又称为Euler's totient function、
φ函数、欧拉商数等。 例如φ(8)=4,因为1,3,5,7均和8互质。 此题所找的是在1至n - 1的所有的与n具有公约数的数的数量,所以n - 1 - 欧拉数就是本题的答案,欧
拉函数的求解———设一个数A = a1(x1)/*表示a1的x1次方*/ * a2(x2) * a3(x3) * ....... * an(an);那么A的欧拉函数phi(A) = A *( (a1 - 1)  * (a2 - 1) * (a3 - 1) * ....
* (an - 1)) / (a1 * a2 * a3 * .... * a4);
 
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long int inta;
inta n;
inta phi(int x)
{
    inta temp = x;
    for(int i = 2;i * i <= x;i ++)
    {
        if(x % i == 0)
        {
            while(x % i == 0)
                x = x / i;
            temp = (temp * (i - 1)) / i;
        }
    }
    if(x != 1)
       temp = (temp * (x - 1)) / x;
    return temp;
}
int main()
{
    while(scanf("%lld",&n),n != 0)
    {
        inta sum = phi(n);
        printf("%lld
",n - 1 - sum);
    }
    return 0;
}


 

 
原文地址:https://www.cnblogs.com/GODLIKEING/p/3368172.html