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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n - 1);
  return ans * n;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
bool is_prime(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}
ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1)
    res = res * x % mod;
  return res;
}

int main() {
  ll n;
  cin >> n;
  set<ll> res;
  res.insert(n);
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ll tmp = n;
      while (!(tmp % i))
        tmp /= i;
      if (tmp % i == 1)
        res.insert(i);
      if (i != n / i) {
        ll tmp = n, div = n / i;
        while (!(tmp % div))
          tmp /= div;
        if (tmp % div == 1)
          res.insert(div);
      }
    }
  }
  for (ll i = 1; i * i <= n - 1; i++) {
    if ((n - 1) % i == 0) {
      res.insert(i);
      if (i != (n - 1) / i)
        res.insert((n - 1) / i);
    }
  }
  cout << res.size() - 1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n - 1);
  return ans * n;
}
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
bool is_prime(ll n) {
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return n != 1;
}
ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1)
    res = res * x % mod;
  return res;
}

int main() {
  ll n;
  cin >> n;
  set<ll> res;
  res.insert(n);
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      ll tmp = n;
      while (!(tmp % i))
        tmp /= i;
      if (tmp == 1 || tmp % i == 1)
        res.insert(i);
      if (i != n / i) {
        ll tmp = n, div = n / i;
        while (!(tmp % div))
          tmp /= div;
        if (tmp == 1 || tmp % div == 1)
          res.insert(div);
      }
    }
  }
  for (ll i = 1; i * i <= n - 1; i++) {
    if ((n - 1) % i == 0) {
      ll tmp = n - 1;
      while (!(tmp % i))
        tmp /= i;
      if (tmp == 1 || tmp % i == 1)
        res.insert(i);
      if (i != (n - 1) / i) {
        ll tmp = n - 1, div = (n - 1) / i;
        while (!(tmp % div))
          tmp /= div;
        if (tmp == 1 || tmp % div == 1)
          res.insert(div);
      }
    }
  }
  cout << res.size() << endl;
}