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

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define INF 0x7fffffff
#define LL_INF 0x7fffffffffffffff
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a, ll b) {
  // a >= b
  while (b != 0) {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}

ll power(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res *= a;
    }
    a *= a;
    n >>= 1;
  }
  return res;
}

vector<pair<int, int>> primeFactorization(int n) {
  // (소인수, 지수)
  vector<pair<int, int>> primes;
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      int cnt = 0;
      while (n % i == 0) {
        n /= i;
        cnt++;
      }
      primes.push_back(make_pair(i, cnt));
    }
  }
  if (n != 1)
    primes.push_back(make_pair(n, 1));
  return primes;
}

int main(void) {
  int n;
  cin >> n;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (n % 5 == 0 || n % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<pii> p = primeFactorization(n);
  vector<int> pp;
  for (auto pa : p) {
    if (pa.first == 7) {
      pa.second--;
      if (pa.second == 0)
        continue;
    }
    if (pa.first == 3) {
      pp.pb(power(pa.first, pa.second));
    }
    ll ten = 10;
    ll pm = power(pa.first, pa.second);
    ll n = 1;
    while (ten % pm != 1) {
      n++;
      ten = (ten * 10) % pm;
    }
    pp.pb(n);
  }
  if (pp.size() == 0) {
    cout << 1 << endl;
    return 0;
  }

  ll ans = pp[0];
  for (int i = 0; i < pp.size(); i++) {
    ll g = gcd(ans, pp[i]);
    ans = ans * pp[i] / g;
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define INF 0x7fffffff
#define LL_INF 0x7fffffffffffffff
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ll gcd(ll a, ll b) {
  // a >= b
  while (b != 0) {
    ll r = a % b;
    a = b;
    b = r;
  }
  return a;
}

ll power(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) {
      res *= a;
    }
    a *= a;
    n >>= 1;
  }
  return res;
}

vector<pair<int, int>> primeFactorization(int n) {
  // (소인수, 지수)
  vector<pair<int, int>> primes;
  for (ll i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      int cnt = 0;
      while (n % i == 0) {
        n /= i;
        cnt++;
      }
      primes.push_back(make_pair(i, cnt));
    }
  }
  if (n != 1)
    primes.push_back(make_pair(n, 1));
  return primes;
}

int main(void) {
  int n;
  cin >> n;
  if (n == 1) {
    cout << 1 << endl;
    return 0;
  }
  if (n % 5 == 0 || n % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<pii> p = primeFactorization(n);
  vector<int> pp;
  for (auto pa : p) {
    if (pa.first == 7) {
      pa.second--;
      if (pa.second == 0)
        continue;
    } /*
     if(pa.first == 3) {
             pp.pb(power(pa.first, pa.second));
     }*/
    ll ten = 10;
    ll pm = power(pa.first, pa.second);
    ll n = 1;
    while (ten % pm != 1) {
      n++;
      ten = (ten * 10) % pm;
    }
    pp.pb(n);
  }

  ll ans = pp[0];
  for (int i = 0; i < pp.size(); i++) {
    ll g = gcd(ans, pp[i]);
    ans = ans * pp[i] / g;
  }
  cout << ans << endl;

  return 0;
}