This documentation is automatically generated by online-judge-tools/verification-helper
View the Project on GitHub ngthanhtrung23/ACM_Notebook_new
#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=NTL_1_D" #include "../../template.h" #include "../Prime/EulerPhi.h" using ll = long long; void solve() { ll n; cin >> n; if (n < N) { assert(eulerPhi(n) == eulerPhi_lookup(n)); } cout << eulerPhi(n) << endl; }
#line 1 "Math/tests/aizu_ntl_1_d_euler_phi.test.cpp" #define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=NTL_1_D" #line 1 "template.h" #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++) #define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--) #define REP(i,a) for(int i=0,_a=(a); i<_a; i++) #define EACH(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it) #define DEBUG(x) { cout << #x << " = "; cout << (x) << endl; } #define PR(a,n) { cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl; } #define PR0(a,n) { cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl; } #define sqr(x) ((x) * (x)) // For printing pair, container, etc. // Copied from https://quangloc99.github.io/2021/07/30/my-CP-debugging-template.html template<class U, class V> ostream& operator << (ostream& out, const pair<U, V>& p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class Con, class = decltype(begin(declval<Con>()))> typename enable_if<!is_same<Con, string>::value, ostream&>::type operator << (ostream& out, const Con& con) { out << '{'; for (auto beg = con.begin(), it = beg; it != con.end(); it++) { out << (it == beg ? "" : ", ") << *it; } return out << '}'; } template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template<class ...U> ostream& operator << (ostream& out, const tuple<U...>& t) { return print_tuple_utils<0, tuple<U...>>(out, t); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long get_rand(long long r) { return uniform_int_distribution<long long> (0, r-1)(rng); } template<typename T> vector<T> read_vector(int n) { vector<T> res(n); for (int& x : res) cin >> x; return res; } void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); return 0; } #line 1 "Math/Prime/EulerPhi.h" long long eulerPhi(long long n) { // = n (1-1/p1) ... (1-1/pn) if (n == 0) return 0; long long ans = n; for (int x = 2; x*x <= n; ++x) { if (n % x == 0) { ans -= ans / x; while (n % x == 0) n /= x; } } if (n > 1) ans -= ans / n; return ans; } // LookUp Version const int N = 1000000; int eulerPhi_lookup(int n) { static int lookup = 0, p[N], f[N]; if (!lookup) { REP(i,N) p[i] = 1, f[i] = i; for (int i = 2; i < N; ++i) { if (p[i]) { f[i] -= f[i] / i; for (int j = i+i; j < N; j+=i) p[j] = 0, f[j] -= f[j] / i; } } lookup = 1; } return f[n]; } // Segmented sieve version, compute phi(i) for i in [l, r] // Tested: https://www.spoj.com/problems/ETFS/ namespace EulerPhiSegmented { vector<int> primes; // NOTE: must initialize this const int N = 100111; // >= r - l + 1 long long phi[N], val[N]; // phi[i-l] = euler_phi(i) void eulerPhi_segmentedSieve(long long l, long long r) { assert(!primes.empty()); // must precompute primes upto sqrt(r) for (auto i = l; i <= r; ++i) { phi[i-l] = i; val[i-l] = i; } for (auto p : primes) { if (p > r) break; long long first = (l / p) * p; if (first < l) first += p; while (first <= r) { phi[first - l] -= phi[first - l] / p; while (val[first - l] % p == 0) val[first - l] /= p; first += p; } } for (auto i = l; i <= r; ++i) { if (val[i-l] > 1) { phi[i-l] -= phi[i-l] / val[i-l]; } } } } #line 5 "Math/tests/aizu_ntl_1_d_euler_phi.test.cpp" using ll = long long; void solve() { ll n; cin >> n; if (n < N) { assert(eulerPhi(n) == eulerPhi_lookup(n)); } cout << eulerPhi(n) << endl; }