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

#include <bits/stdc++.h>
using namespace std;
// ofstream cout("output.out");

#define fast()                                                                 \\
  {                                                                            \\
    ios_base::sync_with_stdio(0);                                              \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }

using ll = long long;
const int mod = 1e9 + 7;
const int nax = 1e5 + 5;
int main() {
  fast();
  ll n;
  cin >> n;
  ll m = n - 1;
  map<ll, ll> a, b;
  ll tn = n, tm = m;
  for (ll i = 2; (i * i) <= n; ++i) {
    if (tn % i == 0) {
      a[tn / i]++;
      a[i]++;
      // cout <<"a: "<<tn / i <<" " <<  i <<"\\n";
    }
    if (tm % i == 0) {
      b[tm / i]++;
      b[i]++;
      // cout <<"b: "<< tm / i << " " << i <<"\\n";
    }
  }
  // o(tn) o(tm)
  if (tn > 1)
    a[tn]++;
  if (tm > 1)
    b[tm]++;
  int rs = 0;
  // vector<bool> ck(max((int)sqrt(n) +  5, (int)1e6 + 5), false);
  for (auto i : a) {
    ll num = i.first;
    ll temp = n;
    // o(num)
    if (num == 1)
      continue;
    int cnt = 0;
    while (temp != 1 && temp >= num) {
      if (temp % num == 0)
        temp /= num;
      else {
        if (((temp - 1) % num) == 0)
          temp = 1;
        else
          temp -= num;
      }
      cnt++;
      if (cnt >= 100)
        temp = 0;
    }
    if (temp == 1) {
      rs++;
    }
  }
  for (auto i : b) {
    ll num = i.first;
    ll temp = n;
    // o(num)
    if (num == 1)
      continue;
    int cnt = 0;
    while (temp != 1 && temp >= num) {
      if (temp % num == 0)
        temp /= num;
      else {
        if (((temp - 1) % num) == 0)
          temp = 1;
        else
          temp -= num;
      }
      // if(temp < 1e6 && ck[temp]) temp = 1;
      cnt++;
      if (cnt >= 100)
        temp = 0;
    }
    if (temp == 1) {
      rs++;
    }
  }
  cout << rs;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// ofstream cout("output.out");

#define fast()                                                                 \\
  {                                                                            \\
    ios_base::sync_with_stdio(0);                                              \\
    cin.tie(0);                                                                \\
    cout.tie(0);                                                               \\
  }

using ll = long long;
const int mod = 1e9 + 7;
const int nax = 1e5 + 5;
int main() {
  fast();
  ll n;
  cin >> n;
  ll m = n - 1;
  map<ll, ll> a, b;
  ll tn = n, tm = m;
  for (ll i = 2; (i * i) <= n; ++i) {
    if (tn % i == 0) {
      a[tn / i]++;
      a[i]++;
      // cout <<"a: "<<tn / i <<" " <<  i <<"\\n";
    }
    if (tm % i == 0) {
      b[tm / i]++;
      b[i]++;
      // cout <<"b: "<< tm / i << " " << i <<"\\n";
    }
  }
  // o(tn) o(tm)
  if (tn > 1)
    a[tn]++;
  if (tm > 1)
    b[tm]++;
  int rs = 0;
  // vector<bool> ck(max((int)sqrt(n) +  5, (int)1e6 + 5), false);
  for (auto i : a) {
    ll num = i.first;
    ll temp = n;
    // o(num)
    if (num == 1)
      continue;

    while (temp != 1 && temp >= num) {
      if (temp % num == 0)
        temp /= num;
      else {
        if (((temp - 1) % num) == 0)
          temp = 1;
        else
          temp -= num;
      }
    }
    if (temp == 1) {
      rs++;
    }
  }
  for (auto i : b) {
    ll num = i.first;
    ll temp = n;
    // o(num)
    if (num == 1)
      continue;

    while (temp != 1 && temp >= num) {
      if (temp % num == 0)
        temp /= num;
      else {
        if (((temp - 1) % num) == 0)
          temp = 1;
        else
          temp -= num;
      }
      // if(temp < 1e6 && ck[temp]) temp = 1;
    }
    if (temp == 1) {
      rs++;
    }
  }
  cout << rs;
}