Advertisement
newb_ie

Euler Totient Precompute

Mar 19th, 2021
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxN = 1e5;
  5. int64_t phi[maxN];
  6.  
  7. void preComputePhi () {
  8.     for (int i = 1; i < maxN; ++i) phi[i] = i;
  9.     for (int i = 2; i < maxN; ++i) {
  10.         if (phi[i] == i) {
  11.             //i is a prime number
  12.             //we need to subtract 1 from phi[i] because we know that phi[p] = p - 1
  13.             for (int j = i; j < maxN; j += i) {
  14.                 phi[j] -= phi[j] / i;
  15.             }
  16.         }
  17.     }
  18. }
  19.  
  20. int main () {
  21.      ios::sync_with_stdio(false);
  22.      cin.tie(nullptr);
  23.      cout.tie(nullptr);
  24.      int T = 1;
  25.      //~ cin >> T;
  26.      preComputePhi();
  27.      for (int test_case = 1; test_case <= T; ++test_case) {
  28.          for (int i = 1; i < 30; ++i) {
  29.              cout << phi[i] << ' ';
  30.          }
  31.      }
  32.      //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement