Advertisement
tdttvd

09_24

Jan 16th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | None | 0 0
  1. /*
  2.  * Hàm phi Euler có một vài tính chất để giúp tính toán dễ hơn
  3.  * 1. Tính chất 1:
  4.  *   Nếu a và b nguyên tố cùng nhau thì: phi(ab) = phi(a) * phi(b)
  5.  * 2. Tính chất 2:
  6.  *   Nếu p là số nguyên tố: phi(p) = p - 1
  7.  * 3. Tính chất 3:
  8.  *   Nếu p là số nguyên tố: phi(p^a) = p^(a) - p^(a - 1)
  9.  * 4. Tính chất 4:
  10.  *   phi(1) = 0
  11.  *
  12.  * Từ các tính chất trên -> Hướng giải bài toán:
  13.  *   Phân tích thành thừa số nguyên tố, đếm số mũ của số nguyên tố
  14.  *   Ta có thể thấy tính chất 2 và 3 tương tự nhau, tính chất 2 là hệ quả của 3 với số mũ = 1
  15.  *   Từ đó, dùng tính chất 3 để tính kết quả của hàm phi Euler
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <stdbool.h>
  20. #include <math.h>
  21.  
  22. #define MAX 10000
  23. #define ll long long
  24.  
  25. // Những thứ cần chuẩn bị
  26. ll expo(ll a, ll b);                  // Hàm tính số mũ
  27. bool isPrime(ll n);                   // Hàm kiểm tra số nguyên tố
  28.  
  29. int main()
  30. {
  31.     ll n;
  32.     scanf("%lld", &n);
  33.  
  34.     ll uoc[MAX];
  35.     int so_mu[MAX];
  36.  
  37.     int index = 0;
  38.     ll prime_number = 2;
  39.  
  40.     while (n > 1)
  41.     {
  42.         if ((n % prime_number == 0) && isPrime(prime_number))
  43.         {
  44.             uoc[index] = prime_number;
  45.             so_mu[index] = 0;
  46.             while (n % prime_number == 0)
  47.             {
  48.                 n = n / prime_number;
  49.                 so_mu[index] += 1;
  50.             }
  51.             ++index;
  52.         }
  53.         ++prime_number;
  54.     }
  55.  
  56.     ll answer = 1;
  57.  
  58.     for (int i = 0; i < index; ++i)
  59.     {
  60.         answer *= (expo(uoc[i], so_mu[i]) - expo(uoc[i], so_mu[i] - 1));
  61.     }
  62.  
  63.     printf("%lld", answer);
  64. }
  65.  
  66. // Hàm lũy thừa
  67. ll expo(ll a, ll b)
  68. {
  69.     ll ans = 1;
  70.     while (b > 0)
  71.     {
  72.         if (b & 1) ans = ans * a;
  73.         a = a * a;
  74.         b = b >> 1;
  75.     }
  76.     return ans;
  77. }
  78.  
  79. // Hàm kiểm tra số nguyên tố
  80. bool isPrime(ll n)
  81. {
  82.     if (n < 2) return false;
  83.  
  84.     for (ll i = 2; i <= sqrt(n); ++i)
  85.         if (n % i == 0) return false;
  86.    
  87.     return true;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement