fork download
  1. /***
  2. ** Bismillahir Rahmanir Rahim
  3. ** ALLAHU AKBAR
  4. **
  5. ** Author: Khairul Anam Mubin (__Broly__)
  6. ** Bangladesh University of Business and Technology,
  7. ** Dept. of CSE.
  8. ***/
  9. #include <bits/stdc++.h>
  10. using namespace std;
  11.  
  12. int main() {
  13. ios_base::sync_with_stdio(0);
  14. cin.tie(0);
  15. cout.tie(0);
  16.  
  17. int n; cin >> n;
  18. int ar[n + 1];
  19. map <int , int> mp;
  20. for (int i = 0; i < n; i++) {
  21. cin >> ar[i];
  22. mp[ar[i]]++;
  23. }
  24. vector <int> ans;
  25. while (mp.size() != 0) {
  26. auto it = mp.end();
  27. it--;
  28. int val = it->first;
  29. ans.push_back(val);
  30. for (int i = 1; i * i <= val; i++) {
  31. if (val % i == 0) {
  32. int x = i;
  33. int y = val / i;
  34. mp[x]--;
  35. if (mp[x] == 0) {
  36. mp.erase(mp.find(x));
  37. }
  38. if (x != y) {
  39. mp[y]--;
  40. if (mp[y] == 0) {
  41. mp.erase(mp.find(y));
  42. }
  43. }
  44. }
  45. }
  46. }
  47. cout << ans.size() << "\n";
  48. for (int x : ans) cout << x << " ";
  49. return 0;
  50. }
Time limit exceeded #stdin #stdout 5s 5520KB
stdin
Standard input is empty
stdout
Standard output is empty