Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

/*
using PAIR = pair<string, int>;
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long ll;

#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define debugA() cerr << "AAAAA" << endl
#define debug_() cerr << "-------------" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cout << #v << endl;                                                          \
  for (int i = 0; i < v.size(); i++) {                                         \
    cout << v[i] << " ";                                                       \
  }                                                                            \
  cout << endl;
#define debug_vec2(v)                                                          \
  cout << #v << endl;                                                          \
  for (int i = 0; i < v.size(); i++) {                                         \
    for (int j = 0; j < v[i].size(); j++) {                                    \
      cout << v[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }

using Graph = vector<vector<int>>;
using P = pair<int, int>;
using P1 = pair<int, pair<int, int>>; // クラスカル法とかで、{cost, {from,
                                      // to}}的に使う。
const int INF = 1001001001;
const double pi = acos(-1);

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> vec(1000000 + 1, 0);
  sort(a.begin(), a.end());
  for (int i = 0; i < n; i++) {
    for (int j = a[i]; j < 1000000 + 1; j += a[i]) {
      vec[j]++;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (vec[a[i]] == 1) {
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

/*
using PAIR = pair<string, int>;
sort(sp.begin(), sp.end(), [](PAIR l, PAIR r){
        return l.first<r.first || (l.first==r.first && l.second > r.second);
        });
*/

typedef long long ll;

#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define debugA() cerr << "AAAAA" << endl
#define debug_() cerr << "-------------" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \
  cout << #v << endl;                                                          \
  for (int i = 0; i < v.size(); i++) {                                         \
    cout << v[i] << " ";                                                       \
  }                                                                            \
  cout << endl;
#define debug_vec2(v)                                                          \
  cout << #v << endl;                                                          \
  for (int i = 0; i < v.size(); i++) {                                         \
    for (int j = 0; j < v[i].size(); j++) {                                    \
      cout << v[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }

using Graph = vector<vector<int>>;
using P = pair<int, int>;
using P1 = pair<int, pair<int, int>>; // クラスカル法とかで、{cost, {from,
                                      // to}}的に使う。
const int INF = 1001001001;
const double pi = acos(-1);

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> vec(1000000 + 1, 0);
  sort(a.begin(), a.end());
  for (int i = 0; i < n; i++) {
    if (vec[a[i]] == 0) {
      for (int j = a[i]; j <= a.back(); j += a[i]) {
        vec[j]++;
      }
    } else {
      vec[a[i]]++;
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (vec[a[i]] == 1) {
      ans++;
    }
  }
  cout << ans << endl;

  return 0;
}