Problem p02900 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

long long GCD(long long a, long long b) {
  if (b == 0) {
    return a;
  }

  GCD(b, a % b);
}

bool IsPrime(long long n) {
  if (n == 1 || n == 2)
    return true;
  if (n % 2 == 0)
    return false;

  for (int ni = 3; ni <= sqrt(n); ni += 2) {
    if (n % ni == 0) {
      return false;
    }
  }

  return true;
}

long long CntPrime(long long n) {
  vector<long long> P(n + 1, 1);

  P[0] = 0;
  for (int ni = 2; ni <= sqrt(n); ni++) {
    for (int mi = 2; ni * mi <= n; mi++) {
      P[ni * mi] = 0;
    }
  }

  long long cnt = 0;
  for (int ni = 1; ni <= n; ni++) {
    if (n % ni == 0 && P[ni] > 0) {
      cnt++;
    }
  }

  return cnt;
}

int main() {
  long long A, B;

  cin >> A >> B;

  // long long gcd = GCD(max(A, B), min(A, B));

  long long gcd;

  long long a, b, c;
  a = max(A, B);
  b = min(A, B);
  while (1) {
    if (b == 0) {
      gcd = a;
      break;
    }
    c = a;
    a = b;
    b = c % b;
  }

  if (gcd == 1) {
    std::cout << 1;
    return 0;
  }

  /*
  vector<long long> P(gcd + 1, 1);

  P[0] = 0;
  for (int ni = 2; ni <= sqrt(gcd); ni++) {
          for (int mi = 2; ni * mi <= gcd; mi++) {
                  P[ni * mi] = 0;
          }
  }
  */

  vector<long long> PFs;
  PFs.push_back(1);

  long long ni = 2;
  long long tar = gcd;
  while (1) {

    /*
    if (IsPrime(tar)) {
            PFs.push_back(tar);
            break;
    }
    */

    if (tar % ni == 0) {
      PFs.push_back(ni);
      while (tar % ni == 0) {
        tar /= ni;
      }
      if (tar == 1)
        break;

      if (IsPrime(tar)) {
        PFs.push_back(tar);
        break;
      }
    }
    ni++;
  }

  long long ans = PFs.size();
  // printf("%.9f", ans);
  std::cout << ans;

  return 0;
}


Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

long long GCD(long long a, long long b) {
  if (b == 0) {
    return a;
  }

  GCD(b, a % b);
}

bool IsPrime(long long n) {
  if (n == 1 || n == 2)
    return true;
  if (n % 2 == 0)
    return false;

  for (int ni = 3; ni <= sqrt(n); ni += 2) {
    if (n % ni == 0) {
      return false;
    }
  }

  return true;
}

long long CntPrime(long long n) {
  vector<long long> P(n + 1, 1);

  P[0] = 0;
  for (int ni = 2; ni <= sqrt(n); ni++) {
    for (int mi = 2; ni * mi <= n; mi++) {
      P[ni * mi] = 0;
    }
  }

  long long cnt = 0;
  for (int ni = 1; ni <= n; ni++) {
    if (n % ni == 0 && P[ni] > 0) {
      cnt++;
    }
  }

  return cnt;
}

int main() {
  long long A, B;

  cin >> A >> B;

  // long long gcd = GCD(max(A, B), min(A, B));

  long long gcd;

  long long a, b, c;
  a = max(A, B);
  b = min(A, B);
  while (1) {
    if (b == 0) {
      gcd = a;
      break;
    }
    c = a;
    a = b;
    b = c % b;
  }

  if (gcd == 1) {
    std::cout << 1;
    return 0;
  }
  if (IsPrime(gcd)) {
    std::cout << 2;
    return 0;
  }

  /*
  vector<long long> P(gcd + 1, 1);

  P[0] = 0;
  for (int ni = 2; ni <= sqrt(gcd); ni++) {
          for (int mi = 2; ni * mi <= gcd; mi++) {
                  P[ni * mi] = 0;
          }
  }
  */

  vector<long long> PFs;
  PFs.push_back(1);

  long long ni = 2;
  long long tar = gcd;
  while (1) {

    /*
    if (IsPrime(tar)) {
            PFs.push_back(tar);
            break;
    }
    */

    if (tar % ni == 0) {
      PFs.push_back(ni);
      while (tar % ni == 0) {
        tar /= ni;
      }
      if (tar == 1)
        break;

      if (IsPrime(tar)) {
        PFs.push_back(tar);
        break;
      }
    }
    ni++;
  }

  long long ans = PFs.size();
  // printf("%.9f", ans);
  std::cout << ans;

  return 0;
}