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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (const auto &e : vec)
    os << e << (&e == &vec.back() ? "\\n" : " ");
  return os;
}

#ifdef _DEBUG
template <class T> void dump(const char *str, T &&h) {
  cerr << str << " = " << h << "\\n";
};
template <class Head, class... Tail>
void dump(const char *str, Head &&h, Tail &&...t) {
  while (*str != ',')
    cerr << *str++;
  cerr << " = " << h << "\\n";
  dump(str + (*(str + 1) == ' ' ? 2 : 1), t...);
}
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

template <class T> vector<T> divisor(const T n) {
  vector<T> res;
  for (T i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.emplace_back(i);
      if (i * i != n)
        res.emplace_back(n / i);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

int main() {

  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  lint N;
  cin >> N;

  int ans = (int)divisor(N - 1).size() - 1;

  auto div = divisor(N);
  for (const auto &e : div) {

    if (e == 1)
      continue;
    lint tmp = N;
    while (tmp >= e) {
      if (tmp % e) {
        tmp %= e;
      } else
        tmp /= e;
    }
    if (tmp == 1)
      ans++;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (const auto &e : vec)
    os << e << (&e == &vec.back() ? "\\n" : " ");
  return os;
}

#ifdef _DEBUG
template <class T> void dump(const char *str, T &&h) {
  cerr << str << " = " << h << "\\n";
};
template <class Head, class... Tail>
void dump(const char *str, Head &&h, Tail &&...t) {
  while (*str != ',')
    cerr << *str++;
  cerr << " = " << h << "\\n";
  dump(str + (*(str + 1) == ' ' ? 2 : 1), t...);
}
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

template <class T> vector<T> divisor(const T n) {
  vector<T> res;
  for (T i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      res.emplace_back(i);
      if (i * i != n)
        res.emplace_back(n / i);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

int main() {

  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  lint N;
  cin >> N;

  int ans = (int)divisor(N - 1).size() - 1;

  auto div = divisor(N);
  for (const auto &e : div) {

    if (e == 1)
      continue;
    lint tmp = N;
    while (tmp >= e) {
      if (tmp % e) {
        tmp -= e;
      } else
        tmp /= e;
    }
    if (tmp == 1)
      ans++;
  }

  cout << ans << endl;

  return 0;
}