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

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include "bits/stdc++.h"
#define int long long
#define endl '\\n'
using namespace std;

typedef long long ll;
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << endl;
  err(++it, args...);
}

#define read(a)                                                                \\
  int a;                                                                       \\
  cin >> a;
#define readb(a, b)                                                            \\
  int a, b;                                                                    \\
  cin >> a >> b;
#define readc(a, b, c)                                                         \\
  int a, b, c;                                                                 \\
  cin >> a >> b >> c;
#define readarr(a, n)                                                          \\
  int a[(n) + 1] = {};                                                         \\
  FOR(i, 1, (n)) { cin >> a[i]; }
#define readmat(a, n, m)                                                       \\
  int a[n + 1][m + 1] = {};                                                    \\
  FOR(i, 1, n) { FOR(j, 1, m) cin >> a[i][j]; }

#define print(a) cout << a << endl;
#define printarr(a, n)                                                         \\
  FOR(i, 1, n) cout << a[i] << " ";                                            \\
  cout << endl;
#define printv(v)                                                              \\
  for (int i : v)                                                              \\
    cout << i << " ";                                                          \\
  cout << endl;
#define printmat(a, n, m)                                                      \\
  FOR(i, 1, n) {                                                               \\
    FOR(j, 1, m) cout << a[i][j] << " ";                                       \\
    cout << endl;                                                              \\
  }                                                                            \\
  cout << endl;
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define vvi vector<vi>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e13;
const ll mod = 1e9 + 7;
const ll N = 2e4 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  read(n);
  if (n == 2) {
    cout << 1;
    return 0;
  }
  int sqr = sqrt(n);
  set<int> s;

  FOR(i, 2, sqr) {
    if ((n - 1) % i == 0)
      s.insert(i), s.insert(n / i);
  }
  s.insert(n - 1);
  s.insert(n);

  FOR(i, 2, sqr) {
    if (n % i == 0) {
      int t = i, icpy = i, x = inf / icpy;
      while (true) {
        if (t > x || (n % (t * i)))
          break;
        t *= i;
      }

      int cpy = n / t;
      if (cpy % icpy == 1)
        s.insert(i);

      t = n / i, icpy = n / i, x = inf / icpy;
      while (true) {
        if (t > x || (n % (t * icpy)))
          break;
        t *= icpy;
      }

      cpy = n / t;
      if (cpy % icpy == 1)
        s.insert(icpy);
    }
  }

  cout << sz(s);
}


Error: TLE

Fixed submission:

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include "bits/stdc++.h"
#define int long long
#define endl '\\n'
using namespace std;

typedef long long ll;
#define error(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cout << *it << " = " << a << endl;
  err(++it, args...);
}

#define read(a)                                                                \\
  int a;                                                                       \\
  cin >> a;
#define readb(a, b)                                                            \\
  int a, b;                                                                    \\
  cin >> a >> b;
#define readc(a, b, c)                                                         \\
  int a, b, c;                                                                 \\
  cin >> a >> b >> c;
#define readarr(a, n)                                                          \\
  int a[(n) + 1] = {};                                                         \\
  FOR(i, 1, (n)) { cin >> a[i]; }
#define readmat(a, n, m)                                                       \\
  int a[n + 1][m + 1] = {};                                                    \\
  FOR(i, 1, n) { FOR(j, 1, m) cin >> a[i][j]; }

#define print(a) cout << a << endl;
#define printarr(a, n)                                                         \\
  FOR(i, 1, n) cout << a[i] << " ";                                            \\
  cout << endl;
#define printv(v)                                                              \\
  for (int i : v)                                                              \\
    cout << i << " ";                                                          \\
  cout << endl;
#define printmat(a, n, m)                                                      \\
  FOR(i, 1, n) {                                                               \\
    FOR(j, 1, m) cout << a[i][j] << " ";                                       \\
    cout << endl;                                                              \\
  }                                                                            \\
  cout << endl;
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int, int>
#define vpi vector<pi>
#define vvi vector<vi>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e13;
const ll mod = 1e9 + 7;
const ll N = 2e4 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  read(n);
  int sqr = sqrt(n + 4);
  unordered_set<int> s;

  FOR(i, 2, sqr) {
    if ((n - 1) % i == 0)
      s.insert(i), s.insert(n / i);
  }
  s.insert(n - 1);
  s.insert(n);

  FOR(i, 2, sqr) {
    if (n % i == 0) {
      int t = i, icpy = i, x = inf / icpy;
      while (true) {
        if (t > x || (n % (t * i)))
          break;
        t *= i;
      }

      int cpy = n / t;
      if (cpy % icpy == 1)
        s.insert(i);

      t = n / i, icpy = n / i, x = inf / icpy;
      while (true) {
        if (t > x || (n % (t * icpy)))
          break;
        t *= icpy;
      }

      cpy = n / t;
      if (cpy % icpy == 1)
        s.insert(icpy);
    }
  }

  cout << sz(s);
}