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

#include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, k, n) for (ll i = k; i <= n; i++)
#define repd(i, k, n) for (ll i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl "\\n"
#define INF (ll) ll_MAX

typedef long long ll;
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::vector<ll> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, ll>> vpp;

const ll N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long inf = 1e18;

using namespace std;

vector<int> primes;

void SieveOfEratosthenes(int n) {
  // Create a boolean array "prime[0..n]" and initialize
  // all entries it as true. A value in prime[i] will
  // finally be false if i is Not a prime, else true.
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }

  // Print all prime numbers
  for (int p = 2; p <= n; p++)
    if (prime[p]) {
      // cout << p << " ";
      primes.pb(p);
    }
}

void solve() {
  ll n;
  cin >> n;

  ll a[n];

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

  ll gcd = a[0];

  for (ll i = 1; i < n; i++) {
    gcd = __gcd(gcd, a[i]);
  }

  if (gcd != 1) {
    cout << "not coprime";
    return;
  }

  SieveOfEratosthenes(1000000);

  map<int, int> mm;
  map<int, int> mp;

  for (ll i = 0; i < primes.size(); i++) {
    mp[primes[i]] = 1;
  }

  for (int i = 0; i < n; i++) {
    int temp = a[i];

    for (int j = 0; j < primes.size(); j++) {
      if (temp == 1)
        break;
      if (mp[temp]) {
        mm[temp]++;
        if (mm[temp] > 1) {
          cout << "setwise coprime";
          return;
        }
        temp = 1;
      }
      bool ok = false;
      while (temp % primes[j] == 0) {
        temp /= primes[j];
        if (ok == false)
          mm[primes[j]]++;
        ok = true;
        if (mm[primes[j]] > 1) {
          cout << "setwise coprime";
          // cout<<endl<<primes[j];
          return;
        }
      }
    }
  }

  cout << "pairwise coprime";

  // vector <ll> p;

  // for(ll i=2;i<=1000;i++){
  //     if (isprime(i)){
  //         p.pb(i);
  //     }
  // }

  // cout<<a[n-1];
}

int main() {

  // #ifndef ONLINE_JUDGE
  //     // for getting input from input.txt
  //     freopen("input.txt", "r", stdin);
  //     // for writing output to output.txt
  //     freopen("output.txt", "w", stdout);
  // #endif

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  ll ctr = 0;
  while (t--) {
    ctr++;
    // cout<<"Case #"<<ctr<<": ";
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <chrono>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <vector>

#define rep(i, n) for (ll i = 0; i < n; i++)
#define repu(i, k, n) for (ll i = k; i <= n; i++)
#define repd(i, k, n) for (ll i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl "\\n"
#define INF (ll) ll_MAX

typedef long long ll;
typedef long double ld;
typedef std::pair<ll, ll> pii;
typedef std::vector<ll> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, ll>> vpp;

const ll N = 3 * 1e5 + 5;
const long long MOD = 1000000007;
const long long MOD2 = 998244353;
const long long inf = 1e18;

using namespace std;

vector<int> primes;

void SieveOfEratosthenes(int n) {
  // Create a boolean array "prime[0..n]" and initialize
  // all entries it as true. A value in prime[i] will
  // finally be false if i is Not a prime, else true.
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    // If prime[p] is not changed, then it is a prime
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }

  // Print all prime numbers
  for (int p = 2; p <= n; p++)
    if (prime[p]) {
      // cout << p << " ";
      primes.pb(p);
    }
}

void solve() {
  ll n;
  cin >> n;

  ll a[n];

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

  ll gcd = a[0];

  for (ll i = 1; i < n; i++) {
    gcd = __gcd(gcd, a[i]);
  }

  if (gcd != 1) {
    cout << "not coprime";
    return;
  }

  SieveOfEratosthenes(1000000);

  map<int, int> mm;

  for (int i = 0; i < n; i++) {
    int temp = a[i];

    for (int j = 0; j < primes.size(); j++) {
      if (temp == 1)
        break;
      bool ok = false;
      while (temp % primes[j] == 0) {
        temp /= primes[j];
        if (ok == false)
          mm[j]++;
        ok = true;
        if (mm[j] > 1) {
          cout << "setwise coprime";
          // cout<<endl<<primes[j];
          return;
        }
      }
    }
  }

  cout << "pairwise coprime";

  // vector <ll> p;

  // for(ll i=2;i<=1000;i++){
  //     if (isprime(i)){
  //         p.pb(i);
  //     }
  // }

  // cout<<a[n-1];
}

int main() {

  // #ifndef ONLINE_JUDGE
  //     // for getting input from input.txt
  //     freopen("input.txt", "r", stdin);
  //     // for writing output to output.txt
  //     freopen("output.txt", "w", stdout);
  // #endif

  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  ll t = 1;
  // cin>>t;
  ll ctr = 0;
  while (t--) {
    ctr++;
    // cout<<"Case #"<<ctr<<": ";
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}