This documentation is automatically generated by online-judge-tools/verification-helper
#include "src/math/euler_phi.hpp"
ll euler_phi(ll n)
$n$ と互いに素な正の整数の個数を返します.
テーブルが必要な場合は,以下の式に従ってエラトステネスの篩のように計算するとよいです.
$\phi (n) = n \prod\limits_i (1 - \frac{1}{p_i})$ ( $p_i$ は $n$ の素因数)
計算量
#pragma once
#include "../template/template.hpp"
constexpr long long euler_phi(long long n) {
long long res = max(n, 0ll);
for(long long i = 2; i * i <= n; ++i) {
if(n % i == 0) {
res -= res / i;
while(n % i == 0) n /= i;
}
}
if(n > 1) res -= res / n;
return res;
}
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
SetupIO() {
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(30);
}
} setup_io;
#line 3 "src/math/euler_phi.hpp"
constexpr long long euler_phi(long long n) {
long long res = max(n, 0ll);
for(long long i = 2; i * i <= n; ++i) {
if(n % i == 0) {
res -= res / i;
while(n % i == 0) n /= i;
}
}
if(n > 1) res -= res / n;
return res;
}