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

#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> prime_fact(int n, vector<int> &spf) {
  vector<pair<int, int>> ans;
  int posi = spf[n];
  int copy = n;

  while (posi != 1) {
    int times = 0;
    int next = posi;
    while (next == posi) {
      copy = copy / posi;
      times++;
      next = spf[copy];
    }
    pair<int, int> add;
    add.first = posi;
    add.second = times;
    ans.push_back(add);

    posi = next;
  }

  // while(posi*posi<=n) {
  //   if(copy%posi==0) {
  //     int times = 0;
  //     while(copy%posi==0) {
  //       copy = copy/posi;
  //       times++;
  //     }

  //     pair<int, int> add;
  //     add.first = posi;
  //     add.second = times;
  //     ans.push_back(add);
  //   }

  //   posi++;
  // }

  // if(copy!=1) {
  //   pair<int, int> add;
  //   add.first = copy;
  //   add.second = 1;
  //   ans.push_back(add);
  // }

  return ans;
}

int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  sort(a, a + n);

  int ma = 1000000;
  bool isprime[ma + 1];
  for (int i = 0; i <= ma; i++)
    isprime[i] = true;
  isprime[0] = false;
  isprime[1] = false;

  int posi = 2;
  while (posi * posi <= ma) {
    if (isprime[posi]) {
      int times = posi;
      while (posi * times <= ma) {
        isprime[posi * times] = false;
        times++;
      }
    }
    posi++;
  }

  vector<int> spf(ma + 1);
  for (int i = 0; i <= ma; i++)
    spf[i] = i;

  int posi2 = 2;
  while (posi2 * posi2 <= ma) {
    if (isprime[posi2]) {
      int times = posi2;
      while (posi2 * times <= ma) {
        spf[posi2 * times] = min(posi2, spf[posi2 * times]);
        times++;
      }
    }
    posi2++;
  }

  int table[ma + 1];
  for (int i = 0; i < ma + 1; i++)
    table[i] = 0;

  for (int i = 0; i < n; i++) {
    if (a[i] == 1) {
      if (table[1] == 0)
        table[1] = 1;
      continue;
    }

    vector<pair<int, int>> fact = prime_fact(a[i], spf);
    int m = fact.size();
    for (int j = 0; j < m; j++) {
      int p = fact[j].first;
      table[p]++;
    }
  }

  int check = *max_element(table, table + ma + 1);
  if (check == 1)
    cout << "pairwise coprime" << endl;
  else if (check == n)
    cout << "not coprime" << endl;
  else
    cout << "setwise coprime" << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

vector<pair<int, int>> prime_fact(int n) {
  vector<pair<int, int>> ans;
  int posi = 2;
  int copy = n;
  while (posi * posi <= n) {
    if (copy % posi == 0) {
      int times = 0;
      while (copy % posi == 0) {
        copy = copy / posi;
        times++;
      }

      pair<int, int> add;
      add.first = posi;
      add.second = times;
      ans.push_back(add);
    }

    posi++;
  }

  if (copy != 1) {
    pair<int, int> add;
    add.first = copy;
    add.second = 1;
    ans.push_back(add);
  }

  return ans;
}

int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  sort(a, a + n);

  int ma = 1000000;
  bool isprime[ma + 1];
  for (int i = 0; i <= ma; i++)
    isprime[i] = true;
  isprime[0] = false;
  isprime[1] = false;

  int posi = 2;
  while (posi * posi <= ma) {
    if (isprime[posi]) {
      int times = posi;
      while (posi * times <= ma) {
        isprime[posi * times] = false;
        times++;
      }
    }
    posi++;
  }

  int table[ma + 1];
  for (int i = 0; i < ma + 1; i++)
    table[i] = 0;

  bool ispair = true;
  bool unknown = true;
  for (int i = 0; i < n; i++) {
    if (a[i] == 1) {
      unknown = false;
      if (table[1] == 0)
        table[1] = 1;
      continue;
    }

    vector<pair<int, int>> fact = prime_fact(a[i]);
    int m = fact.size();
    for (int j = 0; j < m; j++) {
      int p = fact[j].first;
      table[p]++;
    }
  }

  int check = *max_element(table, table + ma + 1);
  if (check == 1)
    cout << "pairwise coprime" << endl;
  else if (check == n)
    cout << "not coprime" << endl;
  else
    cout << "setwise coprime" << endl;

  return 0;
}