Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define print(A, n)                                                            \
  for (ll i = 0; i < n; ++i)                                                   \
    cout << A[i] << ' ';                                                       \
  cout << endl;
#define take(A, n)                                                             \
  for (ll i = 0; i < n; ++i)                                                   \
    cin >> A[i];
//    for(auto& it : m)
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
//  priority_queue<ll int> pq;
//  priority_queue <ll int, vector<ll int>, greater<ll int> > pq;
//  vector<ll int> v;
//  vector< pair<ll int,ll int> > v;
//  set<ll int>::iterator it=s.begin();
//  unordered_map<  pair<ll int,ll int>, ll int> mp;

ll int power(ll int x, ll int y, ll int p) {
  ll int res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  if (x == 0)
    return 0; // In case x is divisible by p;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

void computeLPSArray(string pat, int M, int *lps) {
  // length of the previous longest prefix suffix
  int len = 0;

  lps[0] = 0; // lps[0] is always 0

  // the loop calculates lps[i] for i = 1 to M-1
  int i = 1;
  while (i < M) {
    if (pat[i] == pat[len]) {
      len++;
      lps[i] = len;
      i++;
    } else // (pat[i] != pat[len])
    {
      // This is tricky. Consider the example.
      // AAACAAAA and i = 7. The idea is similar
      // to search step.
      if (len != 0) {
        len = lps[len - 1];

        // Also, note that we do not increment
        // i here
      } else // if (len == 0)
      {
        lps[i] = 0;
        i++;
      }
    }
  }
}
ll int nCrModp(ll int n, ll int r, ll int p) {
  // Optimization for the cases when r is large
  if (r > n - r)
    r = n - r;

  // The array C is going to store last row of
  // pascal triangle at the end. And last entry
  // of last row is nCr
  ll int C[r + 1];
  memset(C, 0, sizeof(C));

  C[0] = 1; // Top row of Pascal Triangle

  // One by constructs remaining rows of Pascal
  // Triangle from top to bottom
  for (ll int i = 1; i <= n; i++) {

    // Fill entries of current row using previous
    // row values
    for (ll int j = min(i, r); j > 0; j--)

      // nCj = (n-1)Cj + (n-1)C(j-1);
      C[j] = (C[j] + C[j - 1]) % p;
  }
  return C[r];
}
#define MAXN 1000005

// stores smallest prime factor for every number
int spf[MAXN];
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

int main() {
  fastio();

  ll int T;
  T = 1;
  while (T--) {
    ll int n, i, j;
    cin >> n;
    ll int a[n];
    map<ll int, ll int> mp;
    for (i = 0; i < n; i++) {
      cin >> a[i];
      mp[a[i]]++;
    }

    ll int cnt = 0;
    for (i = 0; i < n; i++) {
      ll int temp = a[i];
      int flag = 0;
      mp[a[i]]--;

      for (j = 1; j * j <= a[i]; j++) {
        if (a[i] % j == 0) {
          if (mp[j] > 0)
            flag = 1;
          if (flag)
            break;
          if (mp[a[i] / j] > 0)
            flag = 1;
          if (flag)
            break;
        }
      }
      if (flag == 0)
        cnt++;
      mp[a[i]]++;
    }
    cout << cnt << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define print(A, n)                                                            \
  for (ll i = 0; i < n; ++i)                                                   \
    cout << A[i] << ' ';                                                       \
  cout << endl;
#define take(A, n)                                                             \
  for (ll i = 0; i < n; ++i)                                                   \
    cin >> A[i];
//    for(auto& it : m)
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
//  priority_queue<ll int> pq;
//  priority_queue <ll int, vector<ll int>, greater<ll int> > pq;
//  vector<ll int> v;
//  vector< pair<ll int,ll int> > v;
//  set<ll int>::iterator it=s.begin();
//  unordered_map<  pair<ll int,ll int>, ll int> mp;

ll int power(ll int x, ll int y, ll int p) {
  ll int res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  if (x == 0)
    return 0; // In case x is divisible by p;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

void computeLPSArray(string pat, int M, int *lps) {
  // length of the previous longest prefix suffix
  int len = 0;

  lps[0] = 0; // lps[0] is always 0

  // the loop calculates lps[i] for i = 1 to M-1
  int i = 1;
  while (i < M) {
    if (pat[i] == pat[len]) {
      len++;
      lps[i] = len;
      i++;
    } else // (pat[i] != pat[len])
    {
      // This is tricky. Consider the example.
      // AAACAAAA and i = 7. The idea is similar
      // to search step.
      if (len != 0) {
        len = lps[len - 1];

        // Also, note that we do not increment
        // i here
      } else // if (len == 0)
      {
        lps[i] = 0;
        i++;
      }
    }
  }
}
ll int nCrModp(ll int n, ll int r, ll int p) {
  // Optimization for the cases when r is large
  if (r > n - r)
    r = n - r;

  // The array C is going to store last row of
  // pascal triangle at the end. And last entry
  // of last row is nCr
  ll int C[r + 1];
  memset(C, 0, sizeof(C));

  C[0] = 1; // Top row of Pascal Triangle

  // One by constructs remaining rows of Pascal
  // Triangle from top to bottom
  for (ll int i = 1; i <= n; i++) {

    // Fill entries of current row using previous
    // row values
    for (ll int j = min(i, r); j > 0; j--)

      // nCj = (n-1)Cj + (n-1)C(j-1);
      C[j] = (C[j] + C[j - 1]) % p;
  }
  return C[r];
}
#define MAXN 1000005

// stores smallest prime factor for every number
int spf[MAXN];
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

int main() {
  fastio();

  ll int T;
  T = 1;
  while (T--) {
    ll int n, i, j;
    cin >> n;
    ll int a[n];
    int mp[1000006] = {0};
    for (i = 0; i < n; i++) {
      cin >> a[i];
      mp[a[i]]++;
    }

    ll int cnt = 0;
    for (i = 0; i < n; i++) {
      ll int temp = a[i];
      int flag = 0;
      mp[a[i]]--;

      for (j = 1; j * j <= a[i]; j++) {
        if (a[i] % j == 0) {
          if (mp[j] > 0)
            flag = 1;
          if (flag)
            break;
          if (mp[a[i] / j] > 0)
            flag = 1;
          if (flag)
            break;
        }
      }
      if (flag == 0)
        cnt++;
      mp[a[i]]++;
    }
    cout << cnt << endl;
  }

  return 0;
}