Advertisement
tien_noob

Phi Euler

May 1st, 2021 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N = 5e5;
  4. int n, p, phi, tmp;
  5. long long res = 0;
  6. map<long long, int> Map;
  7. long long power(long long a, long long b, long long j)
  8. {
  9.     if (b == 0)
  10.     {
  11.         return 1 % j;
  12.     }
  13.     long long t = power(a, b/2, j);
  14.     if (b % 2 == 0)
  15.     {
  16.         return (t * t) % j;
  17.     }
  18.     else
  19.     {
  20.         return (((t * t) % j) * a) % j;
  21.     }
  22. }
  23. long long inv(int a)
  24. {
  25.     return power(a, phi - 1, p);
  26. }
  27. void read()
  28. {
  29.    cin >> n >> p;
  30.    tmp = p, phi = p;
  31.    for (int i = 2; i * i <= tmp; ++ i)
  32.    {
  33.        if (tmp % i == 0)
  34.        {
  35.            while (tmp % i == 0)
  36.            {
  37.                tmp /= i;
  38.            }
  39.            phi -= phi/i;
  40.        }
  41.    }
  42.    if (tmp != 1)
  43.    {
  44.        phi -= phi/tmp;
  45.    }
  46. }
  47. void solve()
  48. {
  49.    int a;
  50.    for (int i = 1; i <= n; ++ i)
  51.    {
  52.        cin >> a;
  53.        a = (a - 1 + p) % p;
  54.        if (__gcd(a, p) == 1)
  55.        {
  56.            res += Map[inv(a)];
  57.        }
  58.        ++Map[a];
  59.    }
  60.    cout << res;
  61. }
  62. /// inv(x) = x ^ (phi - 1) % p
  63. /// x * inv(x) % p == 1
  64. int main()
  65. {
  66.     ios_base::sync_with_stdio(false);
  67.     cin.tie(nullptr);
  68.     read();
  69.     solve();
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement