Problem p02722 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define lli long long int
#define fr(i, a, b) for (lli i = a; i < b; i++)
#define bfr(i, a, b) for (lli i = a; i >= b; i--)
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL), cout.tie(NULL);
#define tc                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define mod 1000000007LL
#define mod2 998244353LL
#define all(vec) vec.begin(), vec.end()
#define ld long double
#define pb push_back

#define ordered_set                                                            \
  tree<lli, null_type, less<lli>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fio

      set<lli>
          ans;
  lli n;
  cin >> n;
  ans.insert(n);

  for (lli i = 2; i <= sqrt(n); i++)
    if (n % i == 0) {
      lli x = n;
      while (x % i == 0)
        x /= i;
      if (x % i == 1)
        ans.insert(i);
    }

  n--;
  ans.insert(n);

  for (lli i = 2; i <= sqrt(n); i++)
    if (n % i == 0) {

      ans.insert(i);
      ans.insert(n / i);
    }

  ans.erase(1);
  cout << ans.size();

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define lli long long int
#define fr(i, a, b) for (lli i = a; i < b; i++)
#define bfr(i, a, b) for (lli i = a; i >= b; i--)
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL), cout.tie(NULL);
#define tc                                                                     \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define mod 1000000007LL
#define mod2 998244353LL
#define all(vec) vec.begin(), vec.end()
#define ld long double
#define pb push_back

#define ordered_set                                                            \
  tree<lli, null_type, less<lli>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

int main() {

  fio

      set<lli>
          ans;
  lli n;
  cin >> n;
  ans.insert(n);

  for (lli i = 2; i <= sqrt(n); i++)
    if (n % i == 0) {
      lli x = n;
      while (x % i == 0)
        x /= i;
      if (x % i == 1)
        ans.insert(i);
    }

  n--;
  ans.insert(n);

  for (lli i = 2; i <= sqrt(n); i++)
    if (n % i == 0) {

      ans.insert(i);
      ans.insert(n / i);
    }

  ans.erase(1);
  cout << ans.size();

  return 0;
}