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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define t(x) cerr << #x << ":" << x << '\\n';
#define t2(a, n)                                                               \\
  {                                                                            \\
    cerr << #a << " = ";                                                       \\
    for (int i = 0; i < n; ++i) {                                              \\
      cerr << a[i] << ' ';                                                     \\
    };                                                                         \\
    cerr << '\\n';                                                              \\
  }
#define t3(a)                                                                  \\
  {                                                                            \\
    cerr << #a << " = ";                                                       \\
    for (auto i : a) {                                                         \\
      cerr << i << " ";                                                        \\
    }                                                                          \\
    cerr << '\\n';                                                              \\
  }

unordered_set<ll> items;

void factors(ll x) {
  for (int i = 2; i <= sqrt(x) + 1; ++i) {
    if (x % i == 0) {
      items.insert(i);
      items.insert(x / i);
    }
  }
  items.insert(x);
}
set<int> visited;
unordered_map<int, int> dp;
bool check(ll n, ll item) {
  if (n < item) {
    return n == 1;
  }
  if (visited.find(item) != visited.end()) {
    return dp[item];
  }
  dp[item] = n % item == 0 ? check(n / item, item) : check(n % item, item);
  visited.insert(item);
  return dp[item];
}

void print(ll n) {
  factors(n - 1);
  factors(n);
  int ans = 0;
  for (auto i : items) {
    if (i >= 2) {
      ans += check(n, i);
    }
  }
  cout << ans << '\\n';
}

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

  ll n;
  cin >> n;
  print(n);

#ifdef LOL
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
#define t(x) cerr << #x << ":" << x << '\\n';
#define t2(a, n)                                                               \\
  {                                                                            \\
    cerr << #a << " = ";                                                       \\
    for (int i = 0; i < n; ++i) {                                              \\
      cerr << a[i] << ' ';                                                     \\
    };                                                                         \\
    cerr << '\\n';                                                              \\
  }
#define t3(a)                                                                  \\
  {                                                                            \\
    cerr << #a << " = ";                                                       \\
    for (auto i : a) {                                                         \\
      cerr << i << " ";                                                        \\
    }                                                                          \\
    cerr << '\\n';                                                              \\
  }

unordered_set<ll> items;

void factors(ll x) {
  for (ll i = 2; i <= sqrt(x) + 1; ++i) {
    if (x % i == 0) {
      items.insert(i);
      items.insert(x / i);
    }
  }
  items.insert(x);
}

set<ll> visited;
map<ll, ll> dp;

bool check(ll n, ll item) {
  if (n < item) {
    return n == 1;
  }
  if (visited.find(item) != visited.end()) {
    return dp[item];
  }
  if (item == 0) {
    return false;
  }
  dp[item] = n % item == 0 ? check(n / item, item) : check(n % item, item);
  visited.insert(item);
  return dp[item];
}

void print(ll n) {
  factors(n - 1);
  factors(n);
  int ans = 0;
  for (auto i : items) {
    ans += check(n, i);
  }
  cout << ans << '\\n';
}

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

  ll n;
  cin >> n;
  print(n);

#ifdef LOL
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif
  return 0;
}