Problem p02722 (C++) — buggy submission, verdict: Runtime Error

/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-04-04 22:48:48
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

template <class T>
using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
  os << '{';
  size_t n = vec.size();
  for (size_t i = 0; i < n; ++i) {
    os << vec[i];
    if (i != n - 1)
      os << ',';
  }
  os << '}';
  return os;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
  return os << '{' << p.first << " " << p.second << '}';
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::map<T, U> &mp) {
  os << '{';
  for (auto it = mp.begin(); it != mp.end(); ++it) {
    os << '{' << it->first << ':' << it->second << '}';
    if (it != --mp.end())
      os << ',';
  }
  os << '}';
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &st) {
  os << '{';
  for (auto it = st.begin(); it != st.end(); ++it) {
    os << *it;
    if (it != --st.end())
      os << ',';
  }
  os << '}';
  return os;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
  size_t n = vec.size();
  for (size_t i = 0; i < n; ++i)
    is >> vec[i];
  return is;
}

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...) 1728
#endif

void debug_out() { std::cerr << '\\n'; }

template <class Head, class... Tail>
void debug_out(Head &&head, Tail &&...tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0)
    std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

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

  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int64 n;
  cin >> n;

  vector<int64> v1 = divisor(n);
  vector<int64> v2 = divisor(n - 1);

  debug(v1, v2, v1.size(), v2.size());

  int ans = v2.size() - 1;

  auto check = [&](int64 x) -> bool {
    int64 t = n;
    while (t >= x) {
      if (t % x == 0) {
        t /= x;
      } else {
        t %= x;
      }
      debug(t);
    }
    return t == 1;
  };

  int m = v1.size();

  for (int i = 0; i < m; ++i) {
    if (v1[i] == 1)
      continue;
    ans += check(v1[i]);
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-04-04 22:48:48
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

template <class T>
using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
  os << '{';
  size_t n = vec.size();
  for (size_t i = 0; i < n; ++i) {
    os << vec[i];
    if (i != n - 1)
      os << ',';
  }
  os << '}';
  return os;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
  return os << '{' << p.first << " " << p.second << '}';
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::map<T, U> &mp) {
  os << '{';
  for (auto it = mp.begin(); it != mp.end(); ++it) {
    os << '{' << it->first << ':' << it->second << '}';
    if (it != --mp.end())
      os << ',';
  }
  os << '}';
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &st) {
  os << '{';
  for (auto it = st.begin(); it != st.end(); ++it) {
    os << *it;
    if (it != --st.end())
      os << ',';
  }
  os << '}';
  return os;
}
template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
  size_t n = vec.size();
  for (size_t i = 0; i < n; ++i)
    is >> vec[i];
  return is;
}

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...) 1728
#endif

void debug_out() { std::cerr << '\\n'; }

template <class Head, class... Tail>
void debug_out(Head &&head, Tail &&...tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0)
    std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

std::map<int64, int> prime_fact(int64 n) {
  std::map<int64, int> result;
  for (int64 i = 2; i * i <= n; ++i) {
    if (n % i != 0)
      continue;
    int cnt = 0;
    while (n % i == 0) {
      n /= i;
      ++cnt;
    }
    result[i] = cnt;
  }
  if (n != 1)
    result[n] = 1;
  return result;
}

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

  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int64 n;
  cin >> n;

  map<int64, int> mp1 = prime_fact(n);
  map<int64, int> mp2 = prime_fact(n - 1);

  // debug(mp1, mp2);

  vector<int64> v1 = divisor(n);
  vector<int64> v2 = divisor(n - 1);

  debug(v1, v2, v1.size(), v2.size());

  int ans = v2.size() + 1;

  for (int i = 1; i < (int)v1.size() - 1; ++i) {
    int64 t = v1[i];
    for (int j = 0; j <= n / t; ++j) {
      t *= t;
      if (t == n)
        ++ans;
    }
  }

  cout << ans << endl;

  return 0;
}