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



#include <array>
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define int long long
#define fast_cin                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define endl "\\n"
#define endl "\\n"
#define with_tests                                                             \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--) {                                                                \\
    solve();                                                                   \\
    cout << endl;                                                              \\
  }
#define without_tests                                                          \\
  solve();                                                                     \\
  cout << endl;
#define CEIL(x, y) (ll) ceil((long double)(x) / (long double)(y))
#define epsilon 1e-9
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e6 + 1;

int a[N], primes[N], pp[N];
int n, m, u, v;
vector<int> p;

bool pair1() {

  for (int i = 0; i < n; i++) {
    for (int pi : p) {
      if (pi * pi > a[i]) {
        break;
      }
      if (a[i] % pi == 0) {
        int sec = a[i] / pi;
        if (pp[sec] == 0 && primes[sec] == 1) {
          return false;
        }
        if (primes[pi] == 1) {
          return false;
        }
        primes[pi] = 1;
        if (pp[sec] == 0) {
          primes[sec] = 1;
        }
      }
    }
    if (pp[a[i]] == 0 && primes[a[i]] == 1) {
      return false;
    }
    if (pp[a[i]] == 0) {
      primes[a[i]] = 1;
    }
  }
  return true;
}

bool set1() {

  vector<int> final;
  for (int i = 0; i < n; i++) {
    for (int pi : p) {
      if (pi > a[i]) {
        break;
      }
      if (a[i] % pi == 0) {
        // int sec = a[i]/pi;
        // if(pi != sec && pp[sec] == 0){
        //     final.push_back(sec);
        // }
        final.push_back(pi);
      }
    }
    // if(pp[a[i]] == 0){
    //     final.push_back(a[i]);
    // }
    // for(int fi: final){
    //     cout << fi << " ";
    // }
    // cout << endl;
    if (final.size() == 0) {
      return true;
    }
    p = final;
    // cout << p.size() << endl;
    final.clear();
  }
  return false;
}

void solve() {
  pp[1] = 1;
  for (int i = 2; i < N; i++) {
    if (pp[i] == 0) {
      p.push_back(i);
      for (int j = i * 2; j < N; j += i) {
        pp[j] = 1;
      }
    }
  }
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (pair1()) {
    cout << "pairwise coprime";
  } else if (set1()) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}

int32_t main() {

  fast_cin;
  // with_tests;
  without_tests;

  return 0;
}


Error: TLE

Fixed submission:



#include <array>
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define int long long
#define fast_cin                                                               \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define endl "\\n"
#define endl "\\n"
#define with_tests                                                             \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--) {                                                                \\
    solve();                                                                   \\
    cout << endl;                                                              \\
  }
#define without_tests                                                          \\
  solve();                                                                     \\
  cout << endl;
#define CEIL(x, y) (ll) ceil((long double)(x) / (long double)(y))
#define epsilon 1e-9
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e6 + 1;

int a[N], primes[N];
int n, m, u, v;
vector<int> p;

bool pair1() {

  for (int i = 0; i < n; i++) {
    for (int pi : p) {
      if (pi > a[i]) {
        break;
      }
      if (a[i] % pi == 0) {
        if (primes[pi] == 1) {
          return false;
        }
        primes[pi] = 1;
      }
    }
  }
  return true;
}

bool set1() {

  vector<int> final;
  for (int i = 0; i < n; i++) {
    for (int pi : p) {
      if (pi > a[i]) {
        break;
      }
      if (a[i] % pi == 0) {
        final.push_back(pi);
      }
    }
    if (final.size() == 0) {
      return true;
    }
    p = final;
    final.clear();
  }
  return false;
}

void solve() {

  for (int i = 2; i < N; i++) {
    if (primes[i] == 0) {
      p.push_back(i);
      for (int j = i; j < N; j += i) {
        primes[j] = 1;
      }
    }
  }
  memset(primes, 0, sizeof(primes));
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (pair1()) {
    cout << "pairwise coprime";
  } else if (set1()) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }
}

int32_t main() {

  fast_cin;
  // with_tests;
  without_tests;

  return 0;
}