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


// Problem : Div Game
// Contest : Virtual Judge - AtCoder
// URL : https://vjudge.net/problem/AtCoder-abc169_d
// Memory Limit : 1048 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#pragma GCC target("avx,avx2,fma")
// #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast") // disable
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define lll __int128
#define lp(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define vec(v, type, sz) vector<type> v(sz)
#define in(v)                                                                  \\
  for (auto &i : v)                                                            \\
  cin >> i
#define all(v) v.begin(), v.end()
#define prv(v)                                                                 \\
  for (auto i : v)                                                             \\
    cout << i << " ";                                                          \\
  cout << "\\n"
#define MOD 1000000007
#define INF 1000000000
#define INFL 1000000000000000000ll
#define fast_io                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define vi vector<int>
#define vl vector<ll>
#define vii(v, n, m, val) vector<vector<int>> v(n, vector<int>(m, val))
#define viii(v, i, j, k, val)                                                  \\
  vector<vector<vector<int>>> v(i, vector<vector<int>>(j, vector<int>(k, val)))
#define pii pair<int, int>
#define pb push_back
#define file_io                                                                \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define testcases                                                              \\
  int tcc;                                                                     \\
  cin >> tcc;                                                                  \\
  for (int tc = 1; tc <= tcc; tc++)
vector<string> vec_splitter(string s) {
  s += ',';
  vector<string> res;
  while (!s.empty()) {
    res.push_back(s.substr(0, s.find(',')));
    s = s.substr(s.find(',') + 1);
  }
  return res;
}
void debug_out(vector<string> __attribute__((unused)) args,
               __attribute__((unused)) int idx,
               __attribute__((unused)) int LINE_NUM) {
  cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
  if (idx > 0)
    cerr << ", ";
  else
    cerr << "Line(" << LINE_NUM << ") ";
  stringstream ss;
  ss << H;
  cerr << args[idx] << " = " << ss.str();
  debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef DB
#define debug(...)                                                             \\
  debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

int main() {
  fast_io;
  ll n;
  cin >> n;
  int cnt = 0;
  for (ll i = 2; i <= sqrt(n); i++) {
    int p = 0;
    while (n % i == 0) {
      p++;
      n /= i;
    }
    cnt += floor((sqrt(1 + 8 * p) - 1.0) / 2.0);
  }
  if (n != 1) {
    cnt++;
  }
  cout << cnt;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bitset<1000010> pk;
vector<int> udd;
void eos(int x) {
  ll g, h, f;
  pk.set();
  pk[0] = pk[1] = 0;
  for (h = 4; h <= x; h += 2)
    pk[h] = 0;
  udd.push_back(2);
  for (g = 3; g <= x; g += 2)
    if (pk[g]) {
      for (h = g * g, f = g << 1; h <= x; h += f)
        pk[h] = 0;
      udd.push_back(g);
    }
}
int main() {
  eos(1e6 + 7);
  int g, pre = 0, re = 0;
  ll q, x;
  scanf("%lld", &q);
  for (g = 0; udd[g] * udd[g] <= q; g++) {
    x = udd[g];
    while (q % x == 0) {
      re++;
      q /= x;
      x *= udd[g];
      pre = g;
    }
  }
  if (udd[pre] != q && q != 1) {
    for (g = pre + 1; udd[g] * udd[g] <= q; g++)
      if (q % udd[g] == 0) {
        printf("%d\\n", re);
        return 0;
      }
    printf("%d\\n", re + 1);
  } else
    printf("%d\\n", re);
  return 0;
}