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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;

set<ll> divisor(ll n) {
  set<ll> ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.insert(i);
      if (i * i != n)
        ret.insert(n / i);
    }
  }

  /*
  for (auto it = ret.begin(); it != ret.end(); it++) {
          cout << (*it) << " ";
  }
  cout << " DIV " << n << endl;
*/

  return (ret);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  ll N;
  cin >> N;

  map<ll, list<ll>> sq;
  for (ll i = (ll)sqrt(N); i >= 2; i--) {
    ll j = i;
    while (j <= N) {
      sq[j].push_back(i);
      j *= i;
    }
  }
  //	for (auto it = sq.begin(); it != sq.end(); it++) {
  //		cout << (it->first) << " ";
  //	}
  //	cout << "SQ size = " << sq.size() << endl;

  set<ll> ans;
  ans.insert(N);

  // MOD=1
  set<ll> divNm1 = divisor(N - 1);
  for (auto it = divNm1.begin(); it != divNm1.end(); it++) {
    ans.insert(*it);
  }

  /*
  // 一乗
  for (auto it = divN.begin(); it != divN.end(); it++) {
          ll d = *it;
          if ((N / d) % d == 1) {
                  ans.insert(d);
          }
  }
  */
  // 三乗以上
  set<ll> divN = divisor(N);
  for (auto it = divN.begin(); it != divN.end(); it++) {
    auto f = sq.find(*it);
    if (f != sq.end()) {
      for (auto jt = f->second.begin(); jt != f->second.end(); jt++) {
        if ((N / (f->first)) % (*jt) == 1) {
          ans.insert(*jt);
        }
      }
    }
  }

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

  /*
          for (auto it = ans.begin(); it != ans.end(); it++) {
                  cout << (*it) << " ";
          }
          cout << " ANS " << endl;
          */
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

set<ll> divisor(ll n) {
  set<ll> ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.insert(i);
      if (i * i != n)
        ret.insert(n / i);
    }
  }
  ret.erase(1);
  return (ret);
}

int hantei(ll n, ll k) {
  if (n == k)
    return (1);
  ll kk = 1;
  while (kk * k <= n) {
    if (n % (kk * k) == kk) {
      return (1);
    }
    kk *= k;
  }
  return (0);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  ll N;
  cin >> N;

  map<ll, list<ll>> sq;
  for (ll i = (ll)sqrt(N); i >= 2; i--) {
    ll j = i * i * i;
    while (j < N) {
      auto it = sq.find(j);
      if (it == sq.end()) {
        sq[j] = list<ll>(i);
      } else {
        sq[j].push_back(i);
      }
      j *= i;
    }
  }
  //	for (auto it = sq.begin(); it != sq.end(); it++) {
  //		cout << (it->first) << " ";
  //	}
  //	cout << "SQ size = " << sq.size() << endl;

  set<ll> divNm1 = divisor(N - 1);
  //	for (auto it = divNm1.begin(); it != divNm1.end(); it++) {
  //		cout << (*it) << " ";
  //	}
  //	cout << "DIV N-1" << endl;

  set<ll> ans;
  ans.insert(N);
  for (auto it = divNm1.begin(); it != divNm1.end(); it++) {
    ans.insert(*it);
  }

  // 一乗
  set<ll> divN = divisor(N);
  //	for (auto it = divN.begin(); it != divN.end(); it++) {
  //		cout << (*it) << " ";
  //	}
  //	cout << "DIV N" << endl;
  for (auto it = divN.begin(); it != divN.end(); it++) {
    ll d = *it;
    if ((N / d) % d == 1) {
      ans.insert(d);
    }
  }

  // 二乗
  for (auto it = divN.begin(); it != divN.end(); it++) {
    ll d = (int)(sqrt(*it));
    if (d * d == (*it) && (N / (d * d)) % d == 1) {
      ans.insert(d);
    }
  }

  // 三乗以上
  for (auto it = divN.begin(); it != divN.end(); it++) {
    auto f = sq.find(*it);
    if (f != sq.end()) {
      for (auto jt = f->second.begin(); jt != f->second.end(); jt++) {
        if ((N / (f->first)) % (*jt) == 1) {
          ans.insert(*jt);
        }
      }
    }
  }

  cout << ans.size() << endl;
  //	for (auto it = ans.begin(); it != ans.end(); it++) {
  //		cout << (*it) << " ";
  //	}
  //	cout << endl;
}