Problem p01821 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <random>
#include <set>
#include <vector>
using namespace std;
mt19937 mt(12231817);
int rand_rng(int l, int r) {
  uniform_int_distribution<int> p(l, r - 1);
  return p(mt);
}
vector<int> random_distinct_integers(int l, int r, int k) {
  assert((r - l) >= k);
  set<int> s;
  for (int i = 0; i < k; i++) {
    int x = rand_rng(l, r);
    while (s.find(x) != s.end())
      x = rand_rng(l, r);
    s.insert(x);
  }
  return vector<int>(s.begin(), s.end());
}
inline unsigned long long gcd(unsigned long long x, unsigned long long y) {
  while (y != 0) {
    unsigned long long r = x;
    x = y;
    y = r % y;
  }
  return x;
}
inline unsigned modpow(unsigned long long a, unsigned long long b, unsigned m) {
  unsigned ret = 1;
  a %= m;
  while (b) {
    if (b & 1)
      ret = 1ULL * ret * a % m;
    a = 1ULL * a * a % m;
    b >>= 1;
  }
  return ret;
}
std::map<unsigned long long, unsigned> prime_factorize(unsigned long long x) {
  std::map<unsigned long long, unsigned> ret;
  for (unsigned i = 2; 1ULL * i * i <= x; i++) {
    while (x % i == 0)
      x /= i, ret[i]++;
  }
  if (x != 1)
    ret[x]++;
  return ret;
}
std::vector<unsigned long long> prime_divisors(unsigned long long x) {
  std::map<unsigned long long, unsigned> res = prime_factorize(x);
  std::vector<unsigned long long> ret;
  for (std::pair<unsigned long long, unsigned> w : res)
    ret.push_back(w.first);
  return ret;
}
std::vector<unsigned long long> calc_divisors(unsigned long long x) {
  std::map<unsigned long long, unsigned> res = prime_factorize(x);
  std::vector<unsigned long long> ret;
  ret.push_back(1);
  for (std::pair<unsigned long long, unsigned> w : res) {
    int s = ret.size();
    unsigned long long mul = 1;
    for (int i = 0; i < w.second; i++) {
      mul *= w.first;
      for (int j = 0; j < s; j++)
        ret.push_back(ret[j] * mul);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}
unsigned long long totient_function(unsigned long long x) {
  std::vector<unsigned long long> v = prime_divisors(x);
  unsigned long long ret = x;
  for (unsigned long long w : v)
    ret = ret / w * (w - 1);
  return ret;
}
int n;
int main() {
  cin >> n;
  if (gcd(n, totient_function(n)) != 1)
    cout << -1 << endl;
  else if (n == 2)
    cout << 1 << endl;
  else {
    int e = totient_function(n);
    vector<unsigned long long> v1 = calc_divisors(totient_function(e));
    int w1 = -1;
    for (int i : v1) {
      if (modpow(n, i, e) == 1) {
        w1 = i;
        break;
      }
    }
    vector<int> sample = random_distinct_integers(0, n, min(n, 100));
    vector<unsigned long long> v2 = calc_divisors(w1);
    int ret = -1;
    for (int i : v2) {
      bool ok = true;
      int r = modpow(n, i, e);
      for (int j : sample) {
        if (modpow(j, r, n) != j)
          ok = false;
      }
      if (ok) {
        ret = i;
        break;
      }
    }
    cout << ret << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
inline unsigned long long gcd(unsigned long long x, unsigned long long y) {
  while (y != 0) {
    unsigned long long r = x;
    x = y;
    y = r % y;
  }
  return x;
}
inline unsigned modpow(unsigned long long a, unsigned long long b, unsigned m) {
  unsigned ret = 1;
  a %= m;
  while (b) {
    if (b & 1)
      ret = 1ULL * ret * a % m;
    a = 1ULL * a * a % m;
    b >>= 1;
  }
  return ret;
}
std::map<unsigned long long, unsigned> prime_factorize(unsigned long long x) {
  std::map<unsigned long long, unsigned> ret;
  for (unsigned i = 2; 1ULL * i * i <= x; i++) {
    while (x % i == 0)
      x /= i, ret[i]++;
  }
  if (x != 1)
    ret[x]++;
  return ret;
}
std::vector<unsigned long long> prime_divisors(unsigned long long x) {
  std::map<unsigned long long, unsigned> res = prime_factorize(x);
  std::vector<unsigned long long> ret;
  for (std::pair<unsigned long long, unsigned> w : res)
    ret.push_back(w.first);
  return ret;
}
unsigned long long totient_function(unsigned long long x) {
  std::vector<unsigned long long> v = prime_divisors(x);
  unsigned long long ret = x;
  for (unsigned long long w : v)
    ret = ret / w * (w - 1);
  return ret;
}
int n;
int main() {
  cin >> n;
  if (gcd(n, totient_function(n)) != 1)
    cout << -1 << endl;
  else {
    int e = totient_function(n);
    int a = n % e;
    for (int i = 1;; i++) {
      if (a == 1) {
        cout << i << endl;
        break;
      }
      a = 1LL * a * n % e;
    }
  }
  return 0;
}