Problem p02660 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef long long LL;
const int e[] = {0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5,
                 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8};
const LL UnUseMethod = 1e9;
LL count[2] = {0};
LL IN(void) {
  LL x = 0;
  short f = 0, c = getchar();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = getchar();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = getchar();
  }
  return f ? -x : x;
}
LL GCD(LL a, LL b) { return !b ? a : GCD(b, a % b); }
LL Mult(LL x, LL y, LL mod) {
  LL temp = x * y - (LL)((long double)x / mod * y + 1e-8) * mod;
  return temp < 0 ? temp + mod : temp;
}
LL BinaryModularPower(LL base, LL pow, LL mod) {
  LL x = 1;
  while (pow) {
    if (pow & 1) {
      x = Mult(x, base, mod);
    }
    base = Mult(base, base, mod);
    pow >>= 1;
  }
  return x;
}
LL f(LL x, LL mod, LL c) { return (Mult(x, x, mod) + c) % x + 1; }
void Exp(LL x, LL y) {
  count[1] = 0;
  while (!(x % y)) {
    count[1]++;
    x /= y;
  }
  count[0] = x;
  return;
}
int MillerRabin(const LL a) {
  if (a == 2)
    return 1;
  if (a % 2 == 0 || a == 1)
    return 0;
  static int pr[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
  int c = 7;
  LL u = a - 1, t = 0, p = 1;
  while (!(u & 1)) {
    u >>= 1;
    t++;
  }
  for (int i = 0; i < c; ++i) {
    p = pr[i] % a;
    if (!p)
      return 1;
    LL x = BinaryModularPower(p, u, a);
    if (x == 1)
      continue;
    for (int j = 0; x != a - 1 && j < t; ++j) {
      x = Mult(x, x, a);
      if (x == 1)
        return 0;
    }
    if (x == a - 1)
      continue;
    return 0;
  }
  return 1;
}
LL PollardRho(LL n, LL a) {
  srand((unsigned int)time(NULL));
  LL x = rand() % n, y = x, gcd = 1, k = 0, i = 1;
  while (gcd == 1) {
    k++;
    x = Mult(x, x, n) + a;
    if (x >= n)
      x -= n;
    gcd = GCD(x > y ? x - y : y - x, n);
    if (k == i) {
      y = x;
      i <<= 1;
    }
  }
  if (gcd == n)
    return PollardRho(n, a + 1);
  return gcd;
}
int main(void) {
  LL a = 0, p = 1, q = -1, N = IN();
  if (N <= 1)
    return puts("0"), 0;
  Exp(N, 2);
  a = e[count[1]];
  N = count[0];
  Exp(N, 3);
  a += e[count[1]];
  N = count[0];
  while (q * q <= UnUseMethod) {
    p += 6, q += 6;
    Exp(N, p);
    a += e[count[1]];
    N = count[0];
    Exp(N, q);
    a += e[count[1]];
    N = count[0];
  }
  if (N <= UnUseMethod) {
    if (N > 1) {
      a++;
    }
    return printf("%lld\\n", a), 0;
  }
  if (MillerRabin(N)) {
    a++;
    return printf("%lld\\n", a), 0;
  }
  while (N >= UnUseMethod) {
    p = PollardRho(N, 0);
    while (!MillerRabin(p))
      p = PollardRho(p, p);
    Exp(N, p);
    a += e[count[1]];
    N = count[0];
  }
  if (MillerRabin(N)) {
    a++;
  }
  return printf("%lld\\n", a), 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef long long LL;
const int e[40] = {0, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5,
                   5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8};
const LL UnUseMethod = 4759123141;
LL count[2] = {0};
LL IN(void) {
  LL x = 0;
  short f = 0, c = getchar();
  while (c < 48 || c > 57) {
    f ^= c == 45, c = getchar();
  }
  while (c > 47 && c < 58) {
    x = x * 10 + c - 48, c = getchar();
  }
  return f ? -x : x;
}
LL GCD(LL a, LL b) { return !b ? a : GCD(b, a % b); }
void Exp(LL x, LL y) {
  count[1] = 0;
  while (!(x % y)) {
    count[1]++;
    x /= y;
  }
  count[0] = x;
  return;
}
LL BinaryModularPower(LL x, LL y, LL mod) {
  LL p = x, v = 1;
  for (; y; y >>= 1, p = (p * p) % mod) {
    if (y & 1)
      v = (v * p) % mod;
  }
  return v;
}
LL Mult(LL x, LL y, LL mod) {
  LL temp = x * y - (LL)((long double)x / mod * y + 1e-8) * mod;
  return temp < 0 ? temp + mod : temp;
}
int MillerRabin(const LL a) {
  if (a == 2)
    return 1;
  if (a % 2 == 0 || a == 1)
    return 0;
  static int pr[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
  LL u = a - 1, t = 0, p = 1;
  while (!(u & 1)) {
    u >>= 1;
    t++;
  }
  for (int i = 0; i < 7; ++i) {
    p = pr[i] % a;
    if (!p)
      return 1;
    LL x = BinaryModularPower(p, u, a);
    if (x == 1)
      continue;
    for (int j = 0; x != a - 1 && j < t; ++j) {
      x = Mult(x, x, a);
      if (x == 1)
        return 0;
    }
    if (x == a - 1)
      continue;
    return 0;
  }
  return 1;
}
LL PollardRho(LL n) {
  if (n == 1)
    return n;
  if (n % 2 == 0)
    return 2;
  srand(time(NULL));
  int j = 2;
  LL divisor = 1, x = rand() % (n - 2) + 2, y = x;
  for (int i = 1; divisor == 1; i++) {
    x = (Mult(x, x, n) + rand() % (n - 1) + 1) % n;
    divisor = GCD((x < y) ? y - x : x - y, n);
    if (divisor == n)
      return PollardRho(n);
    if (i == j) {
      y = x, j <<= 1;
    }
  }
  return divisor;
}
int main(void) {
  LL a = 0, p = 1, q = -1, N = IN();
  if (N <= 1)
    return puts("0"), 0;
  Exp(N, 2);
  a = e[count[1]];
  N = count[0];
  Exp(N, 3);
  a += e[count[1]];
  N = count[0];
  while (q * q <= UnUseMethod) {
    p += 6, q += 6;
    Exp(N, p);
    a += e[count[1]];
    N = count[0];
    Exp(N, q);
    a += e[count[1]];
    N = count[0];
  }
  if (N <= UnUseMethod) {
    if (N > 1) {
      a++;
    }
    return printf("%lld\\n", a), 0;
  }
  if (MillerRabin(N)) {
    a++;
    return printf("%lld\\n", a), 0;
  }
  while (N * N >= UnUseMethod) {
    p = PollardRho(N);
    while (!MillerRabin(p))
      p = PollardRho(p);
    Exp(N, p);
    a += e[count[1]];
    N = count[0];
  }
  if (MillerRabin(N)) {
    a++;
  }
  return printf("%lld\\n", a), 0;
}