Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define cuttime ios::sync_with_stdio(false)
#define swap(x, y) x ^= y, y ^= x, x ^= y
#define INF 0x3f3f3f3f
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define dep(i, a, b) for (int i = (a); i >= (b); --i)
#define scan1(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanll1(x) scanf("%lld", &x)
#define scanll2(x, y) scanf("%lld%lld", &x, &y)
#define scanll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define lowbit(x) (x & (-x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define debug(x) cout << #x << ": " << x << endl
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 2e5 + 20;
const int MAX = 10000007;
inline ll read() {
  char c = getchar();
  ll x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0', c = getchar();
  }
  return x * f;
}
inline void out(ll x) {
  if (x > 9)
    out(x / 10);
  putchar(x % 10 + '0');
}

int q[N];
map<int, int> mp, mp1;
int main() {
  int n;
  n = read();
  rep(i, 1, n) {
    q[i] = read();
    mp[q[i]]++;
    mp1[q[i]] = 1;
  }
  int ans = 0;
  rep(i, 1, 1e6) {
    for (int j = 1; j <= 1e6 / i; ++j) {
      if (mp[i * j] != 0) {
        mp[i * j]--;
        if (mp[i] != 0)
          mp1[i * j] = 0;
        mp[i * j]++;
      }
    }
  }
  rep(i, 1, n) {
    if (mp1[q[i]])
      ans++;
  }
  cout << ans;
  return 0;
}
/*
 */

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define cuttime ios::sync_with_stdio(false)
#define swap(x, y) x ^= y, y ^= x, x ^= y
#define INF 0x3f3f3f3f
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define dep(i, a, b) for (int i = (a); i >= (b); --i)
#define scan1(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanll1(x) scanf("%lld", &x)
#define scanll2(x, y) scanf("%lld%lld", &x, &y)
#define scanll3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z)
#define lowbit(x) (x & (-x))
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define debug(x) cout << #x << ": " << x << endl
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int N = 2e5 + 20;
const int MAX = 10000007;
inline ll read() {
  char c = getchar();
  ll x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0', c = getchar();
  }
  return x * f;
}
inline void out(ll x) {
  if (x > 9)
    out(x / 10);
  putchar(x % 10 + '0');
}

int q[N];
int mp[1000005];
bool mp1[1000005];
int main() {
  int n;
  n = read();
  rep(i, 1, n) {
    q[i] = read();
    mp[q[i]]++;
    mp1[q[i]] = 1;
  }
  int ans = 0;
  rep(i, 1, 1e6) {
    for (int j = 1; j <= 1e6 / i; ++j) {
      if (mp[i * j] != 0) {
        mp[i * j]--;
        if (mp[i] != 0)
          mp1[i * j] = 0;
        mp[i * j]++;
      }
    }
  }
  rep(i, 1, n) {
    if (mp1[q[i]])
      ans++;
  }
  cout << ans;
  return 0;
}
/*
 */