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

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

// #define 	pi 				3.14159265358979
#define mod 1000000007
#define ll long long int
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repn(i, a, b) for (ll i = a; i > b; i--)
#define mod1 1000003
#define mod2 998244353
#define inf 2e18
#define hi "hello"
#define nl "\\n"
#define ws " "
#define sz size()
#define len length()
#define ff first
#define ss second
#define pb push_back
#define MP make_pair
#define B begin()
#define RB rbegin()
#define E end()
#define UB upper_bound
#define LB lower_bound
#define present(k, m) (m.find(k) != m.end()) ? 1 : 0
#define all(v) v.begin(), v.end()
#define reset(a, val) memset(a, val, sizeof(a))
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>
#define vs vector<string>
#define vc vector<char>
#define sqr(a) ((a) * (a))
#define print1(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  cout << "\\n"
#define print2(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i;                                                                 \\
  cout << "\\n"
#define print3(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i << nl;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

// ostringstream str1;
// str1 << n;
// string tmp = str1.str();
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

bool compare(const pair<string, int> &a, const pair<string, int> &b) {
  return a.second < b.second;
}

double logn(double n, double r) { return (n > r - 1) ? 1 + logn(n / r, r) : 0; }

// vll pwx(32);
// void cal(ll x){ pwx[0] = 1; rep(i, 1, 32)pwx[i] = x*pwx[i-1]; }

/* ################################################################ */

#define space 200005

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

  vll a(n);
  for (auto &i : a)
    cin >> i;

  ll g = a[0];

  rep(i, 1, n) g = __gcd(g, a[i]);

  if (g != 1) {
    cout << "not coprime\\n";
    return;
  }

  vll mp(1e6 + 1, 0);
  ll flag = 1;

  rep(i, 0, n) {
    ll num = a[i], tmp = 0;

    while (num % 2 == 0) {
      tmp++;
      num /= 2;
    }

    mp[2] += tmp > 0;

    for (ll i = 3; i * i <= num; i += 2) {
      tmp = 0;
      while (num % i == 0) {
        tmp++;
        num /= i;
      }

      mp[i] += tmp > 0;
    }

    if (num > 2)
      mp[num]++;
  }

  for (auto i : mp) {
    if (i > 1)
      flag = 0;
  }

  if (flag) {
    cout << "pairwise coprime\\n";
    return;
  }

  cout << "setwise coprime\\n";
}

int main() {
  // boost;
  // cal(2);
  int t = 1, k = 1;
  // cin>>t;
  while (t--) {
    // cout<<"Case #"<<k<<": ";
    solve();
    k++;
  }
}

Error: TLE

Fixed submission:

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

// #define 	pi 				3.14159265358979
#define mod 1000000007
#define ll long long int
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repn(i, a, b) for (ll i = a; i > b; i--)
#define mod1 1000003
#define mod2 998244353
#define inf 2e18
#define hi "hello"
#define nl "\\n"
#define ws " "
#define sz size()
#define len length()
#define ff first
#define ss second
#define pb push_back
#define MP make_pair
#define B begin()
#define RB rbegin()
#define E end()
#define UB upper_bound
#define LB lower_bound
#define present(k, m) (m.find(k) != m.end()) ? 1 : 0
#define all(v) v.begin(), v.end()
#define reset(a, val) memset(a, val, sizeof(a))
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpll vector<pll>
#define vs vector<string>
#define vc vector<char>
#define sqr(a) ((a) * (a))
#define print1(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  cout << "\\n"
#define print2(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i;                                                                 \\
  cout << "\\n"
#define print3(a)                                                              \\
  for (auto i : a)                                                             \\
    cout << i << nl;
#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

// ostringstream str1;
// str1 << n;
// string tmp = str1.str();
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

bool compare(const pair<string, int> &a, const pair<string, int> &b) {
  return a.second < b.second;
}

double logn(double n, double r) { return (n > r - 1) ? 1 + logn(n / r, r) : 0; }

// vll pwx(32);
// void cal(ll x){ pwx[0] = 1; rep(i, 1, 32)pwx[i] = x*pwx[i-1]; }

/* ################################################################ */

#define space 200005

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

  vll a(n);
  for (auto &i : a)
    cin >> i;

  ll g = a[0];

  rep(i, 1, n) g = __gcd(g, a[i]);

  if (g != 1) {
    cout << "not coprime\\n";
    return;
  }

  map<ll, ll> mp;
  ll flag = 1;

  rep(i, 0, n) {
    ll num = a[i], tmp = 0;

    while (num % 2 == 0) {
      tmp++;
      num /= 2;
    }

    mp[2] += tmp > 0;

    for (ll i = 3; i * i <= num; i += 2) {
      tmp = 0;
      while (num % i == 0) {
        tmp++;
        num /= i;
      }

      mp[i] += tmp > 0;
    }

    if (num > 2)
      mp[num]++;
  }

  for (auto i : mp) {
    if (i.second > 1) {
      flag = 0;
    }
  }

  if (flag) {
    cout << "pairwise coprime\\n";
    return;
  }

  cout << "setwise coprime\\n";
}

int main() {
  // boost;
  // cal(2);
  int t = 1, k = 1;
  // cin>>t;
  while (t--) {
    // cout<<"Case #"<<k<<": ";
    solve();
    k++;
  }
}