Question: calculate the number N at the top, and all the m values of 1 ≤ m ≤ n make gcd (m, n) less than 1 and gcd (m, n) less than M.
Analysis: number theory, prime screening method, Euler's function.
If k1 is the number of I-th prime factor whose pi is N, then:
1 ≤ m ≤ n, the number of M in gcd (m, n) = 1 is Euler's function;
Euler's function: Phi (n) = N * (1-1/P1) * (1-1/P2) * (1-1/P3 )*... * (1-1/Pt );
1 ≤ m ≤ n, gcd (m, n) = m the number of M is the number of all the factors of N;
Number of factors: F (n) = (K1 + 1) * (k2 + 1) *... * (Kt + 1 );
Here, we use the screening method to calculate the prime number within 150000, because the data range is within 20000000000,
Therefore, the number that cannot be divisible by the prime number in the first 150000 must also be a prime number, and each number N contains at most one;
Calculate the output N-phi (N)-f (n) + 1. {gcd (1, N) is calculated twice }.
Note: uva10299.
#include <iostream> #include <cstdlib> #include <cmath> using namespace std; int fac[32],num[32]; int prim[150000]; int used[150000]; int main() { for (int i = 0 ; i < 150000 ; ++ i) used[i] = 0; int save = 0; for (int i = 2 ; i < 150000 ; ++ i) if (!used[i]) { prim[save ++] = i; for (int j = 2*i ; j < 150000 ; 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]; num[count] = 0; while (n%prim[base] == 0) { n /= prim[base]; num[count] ++;}count ++; } base ++; } if (n > 1) {fac[count] = n;num[count] = 1;count ++;} long long ans = m; for (int i = 0 ; i < count ; ++ i) ans = ans/fac[i]*(fac[i]-1); //gcd(m,n) = m int s = 1; for (int i = 0 ; i < count ; ++ i) s = s*(num[i]+1); cout << m-ans-s+1 << endl; } return 0; }
Ultraviolet A 11064-Number Theory