algorithm

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub gtnao/algorithm

:heavy_check_mark: src/math/euler_phi.hpp

Depends on

Verified with

Code

#pragma once

#include <bits/stdc++.h>
using namespace std;

#include "./prime_factor.hpp"

template <typename T = long long> T euler_phi(T n) {
  T res = n;
  for (auto &p : prime_factor(n)) {
    res -= res / p.first;
  }
  return res;
}
#line 2 "src/math/euler_phi.hpp"

#include <bits/stdc++.h>
using namespace std;

#line 2 "src/math/prime_factor.hpp"

#line 4 "src/math/prime_factor.hpp"
using namespace std;

template <typename T = long long> map<T, T> prime_factor(T n) {
  map<T, T> res;
  for (T i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      ++res[i];
      n /= i;
    }
  }
  if (n != 1) {
    res[n] = 1;
  }
  return res;
}
#line 7 "src/math/euler_phi.hpp"

template <typename T = long long> T euler_phi(T n) {
  T res = n;
  for (auto &p : prime_factor(n)) {
    res -= res / p.first;
  }
  return res;
}
Back to top page