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

#include <bits/stdc++.h>
using namespace std;
#define int long long
int lcm(int x, int y) { return x / __gcd(x, y) * y; }
int mpow(int n, int x) {
  int res = 1LL;
  for (int i = 0; i < x; i++)
    res *= n;
  return res;
}
int mod_pow(int x, int n, int mod) {
  int res = 1;
  while (n > 0) {
    if (n & 1)
      (res *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return res;
}
int ker(int n) {
  int res = 1;
  map<int, int> m;
  for (int i = 2; i * i <= n; i++) {
    if (n % i)
      continue;
    while (n % i == 0)
      n /= i, m[i]++;
  }
  if (n != 1)
    m[n]++;
  for (auto p : m) {
    if (p.first == 2) {
      if (p.second == 1)
        res = lcm(res, 1);
      else if (p.second == 2)
        res = lcm(res, 2);
      else
        res = lcm(res, mpow(2, p.second - 2));
    } else {
      res = lcm(res, mpow(p.first, p.second - 1) * (p.first - 1));
    }
  }
  return res;
}

signed main() {
  int n, b;
  cin >> n;
  b = n;
  map<int, int> m;
  for (int i = 2; i * i <= n; i++) {
    if (n % i)
      continue;
    while (n % i == 0)
      n /= i, m[i]++;
  }
  if (n != 1)
    m[n]++;
  if (b == n) {
    cout << 1 << endl;
    return 0;
  }
  if ((int)m.size() > 2) {
    cout << -1 << endl;
    return 0;
  }
  int x = -1, y;
  int ans = 1LL;
  for (auto p : m) {
    if (p.second > 1) {
      cout << -1 << endl;
      return 0;
    }
    ans *= (p.first - 1);
    // cout<<ans<<endl;
    if (x < 0)
      x = p.first;
    else
      y = p.first;
  }
  if (__gcd(x + y - 1, (x - 1) * (y - 1)) != 1LL) {
    cout << -1 << endl;
    return 0;
  }
  // cout<<x<<" "<<y<<endl;
  // cout<<x+y-1<<" "<<(x-1)*(y-1)<<endl;
  int k = ker(ans);
  // cout<<k<<endl;
  int tmp = k;
  for (int i = 2; i * i <= k; i++) {
    if (k % i)
      continue;
    // cout<<i<<" "<<mod_pow(x,mod_pow(b,i,ans),b)<<endl;
    // cout<<k/i<<" "<<mod_pow(x,mod_pow(b,k/i,ans),b)<<endl;
    if (mod_pow(x, mod_pow(b, i, ans), b) == x &&
        mod_pow(y, mod_pow(b, i, ans), b) == y &&
        mod_pow(x + 1, mod_pow(b, i, ans), b) == x + 1 &&
        mod_pow(y - 1, mod_pow(b, i, ans), b) == y - 1)
      tmp = min(tmp, i);
    if (mod_pow(x, mod_pow(b, k / i, ans), b) == x &&
        mod_pow(y, mod_pow(b, k / i, ans), b) == y &&
        mod_pow(x + 1, mod_pow(b, k / i, ans), b) == x + 1 &&
        mod_pow(y - 1, mod_pow(b, k / i, ans), b) == y - 1)
      tmp = min(tmp, k / i);
  }
  cout << tmp << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int lcm(int x, int y) { return x / __gcd(x, y) * y; }
int mpow(int n, int x) {
  int res = 1;
  for (int i = 0; i < x; i++)
    res *= n;
  return res;
}
int ker(int n) {
  int res = 1;
  map<int, int> m;
  for (int i = 2; i * i <= n; i++) {
    if (n % i)
      continue;
    while (n % i == 0)
      n /= i, m[i]++;
  }
  if (n != 1)
    m[n]++;
  for (auto p : m) {
    // cout<<res<<endl;
    // cout<<p.first<<" "<<p.second<<endl;
    if (p.first == 2) {
      if (p.second == 1)
        res = lcm(res, 1);
      else if (p.second == 2)
        res = lcm(res, 2);
      else
        res = lcm(res, mpow(2, p.second - 2));
    } else {
      res = lcm(res, mpow(p.first, p.second - 1) * (p.second - 1));
    }
  }
  return res;
}

signed main() {
  int n, b;
  cin >> n;
  b = n;
  map<int, int> m;
  for (int i = 2; i * i <= n; i++) {
    if (n % i)
      continue;
    while (n % i == 0)
      n /= i, m[i]++;
  }
  if (n != 1)
    m[n]++;
  if (b == n) {
    cout << 1 << endl;
    return 0;
  }
  if ((int)m.size() > 2) {
    cout << -1 << endl;
    return 0;
  }
  if (m.count(2)) {
    cout << -1 << endl;
    return 0;
  }
  int ans = 1LL;
  for (auto p : m) {
    if (p.second > 1) {
      cout << -1 << endl;
      return 0;
    }
    ans *= (p.first - 1);
  }
  cout << (ker(ans) ? ker(ans) : -1) << endl;
  return 0;
}