Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define endl "\n"
#define p pair<int, int>
#define arrinp(i, a, start, end)                                               \
  ;                                                                            \
  for (ll i = start; i <= end; i++)                                            \
    cin >> a[i];
#define arrout(i, a, start, end)                                               \
  ;                                                                            \
  for (ll i = start; i <= end; i++)                                            \
    cout << a[i] << " ";
#define swap(a, b)                                                             \
  {                                                                            \
    a = a + b;                                                                 \
    b = a - b;                                                                 \
    a = a - b;
#define forfront(i, start, end) for (ll i = start; i <= end; i++)
#define forback(i, start, end) for (ll i = start; i >= end; i--)
#define out(x) cout << x << " "
#define outline(x) cout << x << endl;
int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int a[n];
  set<int> s;
  int count = 0;
  int maxele = 0;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    s.insert(a[i]);
    maxele = max(maxele, a[i]);
  }
  set<int> f;
  for (int x : s) {
    for (int i = x; i <= maxele; i += x) {
      if (i != x) {
        if (s.find(i) != s.end()) {
          f.insert(i);
        }
      }
    }
  }
  for (int x : s) {
    if (m[x] > 1) {
      if (f.find(x) != f.end())
        count += m[x] - 1;
      else
        count += m[x];
    }
  }
  int ans = count + f.size();
  // outline(count);
  // outline(f.size());
  ans = n - ans;
  outline(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define endl "\n"
#define p pair<int, int>
#define arrinp(i, a, start, end)                                               \
  ;                                                                            \
  for (ll i = start; i <= end; i++)                                            \
    cin >> a[i];
#define arrout(i, a, start, end)                                               \
  ;                                                                            \
  for (ll i = start; i <= end; i++)                                            \
    cout << a[i] << " ";
#define swap(a, b)                                                             \
  {                                                                            \
    a = a + b;                                                                 \
    b = a - b;                                                                 \
    a = a - b;
#define forfront(i, start, end) for (ll i = start; i <= end; i++)
#define forback(i, start, end) for (ll i = start; i >= end; i--)
#define out(x) cout << x << " "
#define outline(x) cout << x << endl;
int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int a[n];
  set<int> s;
  int count = 0;
  int maxele = 0;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    m[a[i]]++;
    s.insert(a[i]);
    maxele = max(maxele, a[i]);
  }
  set<int> f;
  for (int x : s) {
    for (int i = x; i <= maxele; i += x) {
      if (i != x) {
        if (s.find(i) != s.end()) {
          f.insert(i);
        }
      }
    }
    if (m[x] > 1) {
      if (f.find(x) != f.end())
        count += m[x] - 1;
      else
        count += m[x];
    }
  }
  int ans = count + f.size();
  // outline(count);
  // outline(f.size());
  ans = n - ans;
  outline(ans);
  return 0;
}