Question: calculate an Euler's function of a given number (1 ~ N-1 number of n-1 and N mutual quality ).
Analysis: number theory, prime screening method, Euler's function.
Euler's function: Phi (n) = N * (1-1/P1) * (1-1/P2) * (1-1/P3 )*... * (1-1/Pt );
Here, we use the screening method to calculate the prime number within 50000, because the data range is within 1000000000,
Therefore, the number that cannot be divisible by the prime number in the first 50000 must also be a prime number, and each number N contains at most one;
Computing output.
Note: uva10179.
#include <iostream>#include <cstdlib>using namespace std;int fac[30];int prim[50000];int used[50000];int main(){for (int i = 0 ; i < 50000 ; ++ i)used[i] = 0;int save = 0;for (int i = 2 ; i < 50000 ; ++ i)if (!used[i]) {prim[save ++] = i;for (int j = 2*i ; j < 50000 ; j += i)used[j] = 1;}int n;while (cin >> n && n) {int count = 0,base = 0,m = n;while (n > 1 && base < save) {if (n%prim[base] == 0) {fac[count ++] = prim[base];while (n%prim[base] == 0)n /= prim[base];}base ++;}if (n > 1) fac[count ++] = n;long long ans = m;for (int i = 0 ; i < count ; ++ i)ans = ans/fac[i]*(fac[i]-1);if (n == 1) ans = 0LL;cout << ans << endl;}return 0;}
Ultraviolet A 10299-relatives