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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI 3.141592653589793
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = a; i < (n); i++)
#define rrep(i, n, k)                                                          \\
  for (int i = (n); i >= (k); i--)                                             \\
    ;
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> vector<T> make_vector(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vector(size_t a, Ts... ts) {
  return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));
}
const int MOD = 1e9 + 7;
const int INF = 1e9;

int main() {
  ll N;
  cin >> N;
  set<ll> ans;
  auto check = [](ll base, ll temp) {
    while (base % temp == 0 && base > 1)
      base /= temp;
    return base % temp;
  };
  if (N == 2) {
    cout << 1 << endl;
    return 0;
  }
  {
    ll base = N - 1;
    for (ll i = 1; i * i <= base; i++) {
      if (base % i != 0)
        continue;
      if (i != 1 && check(N, i) == 1)
        ans.insert(i);
      if (check(N, base / i) == 1)
        ans.insert(base / i);
    }
  }
  for (ll i = 1; i * i <= N; i++) {
    if (N % i != 0)
      continue;
    if (i != 1 && check(N, i) == 1)
      ans.insert(i);
    if (check(N, N / i) == 1)
      ans.insert(N / i);
  }
  cout << ans.size() << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI 3.141592653589793
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = a; i < (n); i++)
#define rrep(i, n, k)                                                          \\
  for (int i = (n); i >= (k); i--)                                             \\
    ;
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> vector<T> make_vector(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vector(size_t a, Ts... ts) {
  return vector<decltype(make_vector<T>(ts...))>(a, make_vector<T>(ts...));
}
const int MOD = 1e9 + 7;
const int INF = 1e9;

int main() {
  ll N;
  cin >> N;
  set<ll> ans;
  auto check = [](ll base, ll temp) {
    while (base % temp == 0 && base > 1)
      base /= temp;
    return base % temp;
  };
  {
    ll base = N - 1;
    for (ll i = 1; i * i <= base; i++) {
      if (base % i != 0)
        continue;
      if (i != 1 && check(N, i))
        ans.insert(i);
      if (check(N, base / i))
        ans.insert(base / i);
    }
  }
  for (ll i = 1; i * i <= N; i++) {
    if (N % i != 0)
      continue;
    if (i != 1 && check(N, i) == 1)
      ans.insert(i);
    if (check(N, N / i) == 1)
      ans.insert(N / i);
  }
  cout << ans.size() << endl;
}